北航计算机机试10泰勒公式cos(x)

33 篇文章 0 订阅

利用泰勒公式求cos(x)=1-x2/2!+x4/4!-……公式已给,重要的就是注意细节(比如阶乘的存储最好用double类型),二级C语言的难度。

学校给的答案,,题目也模糊,只要懂怎么写就行
看看 即可 以7的精度算的

#include <stdio.h>
#include <math.h>

#define PI 3.14159265358979323846
int main()
{
    int i=1;
    double x, x2, result=1, tmp1 = 1, tmp2 = 1, tmp3;

    printf("Input the times of PI:");
    scanf("%lf",&x); 
    x2 = PI*x*x*PI;

    do
    {
            tmp1 *= x2;
            tmp2 *= ((2*i-1)*2*i);
            tmp3 = (tmp1/tmp2);
            if(i%2==0)
                result += tmp3;
            else
                result -= tmp3;
            i++;
    }while(tmp3>0.0000001||tmp3<-0.0000001);

    printf("result=%lf\n", result);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值