我一直在试图编写使用三角函数的程序cmath,sin()并cos()特别.我知道这些函数使用弧度,所以我添加* 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;
}
Run Code Online (Sandbox Code Playgroud)
为什么这段代码显示错误的数字?
sin(50 / pi * 180);
Run Code Online (Sandbox Code Playgroud)
每次通过循环使用完全相同的角度,因此你将打印sin(50°)十次.也许你的意思
sin(x * pi / 180.0);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
350 次 |
| 最近记录: |