微电子22 发表于 2023-3-29 22:25:07

利用公式求 cosx 的近似值 (精度为 1e-6)

#include <stdio.h>

double main()
{
double cosx=1,x,s,x1=1;
int n,i,j1,j2;
printf("请输入弧度和整数:");
scanf("%lf%d",&x,&n);
for(i=1;i<=n;i++)
{
s=1;
for(j1=1;j1<=2*i;j1++)
{
   s=s*j1;
}
for(j2=1;j2<=2*i;j2++)
{
   x1=x1*x;
}
if(i%2==0)
{
   cosx=cosx+x1/s;
}
else
{
   cosx=cosx-x1/s;
}
}
printf("cos%lf=%lf",x,cosx);

return 0;
}
页: [1]
查看完整版本: 利用公式求 cosx 的近似值 (精度为 1e-6)