利⽤泰勒公式求cosx的值(sinx类似)
如题:利⽤泰勒公式计算cos(x)的值,要求输⼊x(弧度值),当精度⼩于10的-7次⽅时程
#include
#include
#definejingdu1e-7
#definepi3.1415926
unsignedlongfac(unsignedlongn)
{
unsignedlongi,m;
for(i=0,m=1;i<=n;i++)
{
if(i!=0)
m=m*i;
}
returnm;
}
doublefcos(doublex)
{
doubletemp=0.0,t=5;
inti=0;
while(x>=2*pi)x=x-2*pi;//最多只有2π个弧度
while(t>=jingdu)
{
t=(pow(x,2*i))/fac(2*i);
temp+=pow(-1,i)*t;
i++;
//printf("%.5lfn",temp);
}
returntemp;
}
intmain()
{
doubler,temp;
printf("请输⼊弧度值,180°对应3.1415926弧度,以此类推...n");
printf("弧度(rad):");
scanf("%lf",&r);
temp=fcos(r);
printf("%.5lfn",temp);
printf("%.51fn",cos(r));//⽤系统函数进⾏对⽐
}
本文发布于:2022-11-12 18:37:46,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/88/5996.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |