c++sinx函数出错,为什么,求解答
#include <iostream>
#include <math.h>
using namespace std;
double s_in(double x)
{int n=2;
double t=x;
double s=x;
do
{t=(-1)*pow(x,2)/((double)(2*n-1)*(double)(2*n-2));
s=s+t;n++;
}while(fabs(t)<0.000000001);
return s;
}
void main()
{
double x;
cout<<"Enter a number:";
cin>>x;
cout<<'\n';
cout<<"sinx="<<s_in(x);
system("pause");
}//能运行,但是并不能输出正确值,为什么?
[解决办法]
x是double值,求出来的就不会准确
[解决办法]
建议参考sin函数原型
[解决办法]
1.考虑程序算法有无问题!
2.考虑精度问题!
[解决办法]
while(fabs(t)<0.000000001) 是不是这里错了 应该是while(fabs(t)>0.000000001)吧 我输了个10显示sinx =-20.6851 超出-1到1范围了 do-while while语句真才能继续循环 第一轮fabs(t)基本都大于0的 按你的意思 直接退出循环了