返回错误值的trig函数

trig functions returning incorrect values

本文关键字:trig 函数 错误 返回      更新时间:2023-10-16

我一直在尝试编写一个程序,该程序特别使用cmathsin()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;
    }

这个代码为什么显示错误的数字?

sin(50 / pi * 180);

每次循环使用完全相同的角度,因此您将打印sin(50°)十次。也许你的意思是

sin(x * pi / 180.0);