C++ trig函数返回不正确的值

C++ trig函数返回不正确的值,c++,C++,我一直在尝试编写一个程序,特别是使用cmath,sin()和cos()中的trig函数。 我知道这些函数使用弧度,所以我添加了*pi/180,但是程序仍然显示不正确的数字。下面是一些代码 #include<iostream> #include<cmath> using namespace std; const double pi = 3.1415926; int main() { int x = 0;

我一直在尝试编写一个程序,特别是使用
cmath
sin()
cos()
中的trig函数。 我知道这些函数使用弧度,所以我添加了
*pi/180
,但是程序仍然显示不正确的数字。下面是一些代码

    #include<iostream>
    #include<cmath>
    using namespace std;

    const double pi = 3.1415926;

    int main()
    {
        int x = 0;
        double d;
        for(int forl=0;forl < 10;forl++)
        {
           d = sin(50 / pi * 180);
           cout << d << endl;
           x += 5;
        }
        cin >> x;
        return 0;
    }
#包括
#包括
使用名称空间std;
常数双pi=3.1415926;
int main()
{
int x=0;
双d;
for(int-forl=0;forl<10;forl++)
{
d=sin(50/pi*180);
cout x;
返回0;
}
为什么此代码显示的数字不正确

sin(50 / pi * 180);
每次通过循环时使用完全相同的角度,因此您将打印sin(50°)十次。也许您的意思是

sin(x * pi / 180.0);

你能提供一个示例输出吗?“不正确的数字”没有多大帮助。sin(50°)=sin(50*pi/180)(弧度)。看起来更像是一个关于数学的问题:“我在下面的代码中搞砸了什么,以至于标准库给了我一个意外的答案?”@SchighSchagh:
M_pi
是非标准的。
4*atan(0.0)
可能更好。@Keith Thompson:或者更确切地说,4*atan(1.0)@t皮尔斯:很可能是的