泰勒级运算这个程序结果是错误的,但不知道哪里错了?
#include<stdio.h>#include<math.h>
long Fact(int n);
int main()
{
int count;
double x, Sn=0, an, a;//Sn前n项和,an第n项
printf ("Input x:\n");
scanf ( "%lf", &x);
an = x;
a=x;//a用来存放最后一项的值
for (count=1; a>=1e-5||a<=-1e-5; count++)
{
Sn = Sn + an;
a=an;
an=(an * (-x) * x)/Fact(2*(count+1)-1);
}
printf ("sin(x)=%.3f,count=%d\n",Sn,count);
return 0;
}
long Fact (int n)
{
int result, i;
for (i=2, result=1; i<=n; i++)
result = result * i;
return result;
}