满意答案
a510628839
2016.03.22
采纳率:49% 等级:9
已帮助:1867人
/*
利用泰勒展式近似求cos(x)的值
*/
#include
#define E 1e-10//精度要求
int main()
{
double x;
double cos(double x);
printf("please input an radian:");
scanf("%lf",&x);
printf("cos (%lf)=%.16lf\n",x,cos(x));
return 0;
}
/*
泰勒展式
cos(x) = x^0/0!-x^2/2!+x^4/4!-x^6/6!+…+(-1)^(n-1)*x^(2n)/(2n)!+…
*/
double cos(double x)
{
double cos=0,item=1.0,temp;
int n=0,sign=1;
do
{
temp=item;
for(int i=1;i<=2*n;i++)temp/=i;
cos +=sign*temp;
item*=x*x;//item=x^(2n);
sign *=-1;//sign=-sign;
n++;
} while (temp>E);
return cos;
}
01分享举报