习题5-7 使用函数求余弦函数的近似值

习题5-7 使用函数求余弦函数的近似值   (15分)

本题要求实现一个函数,用下列公式求\cos (x)cos(x)的近似值,精确到最后一项的绝对值小于ee

\cos (x) = x^0 / 0! - x^2 / 2! + x^4 / 4! - x^6 / 6! + \cdotscos(x)=x0/0!x2/2!+x4/4!x6/6!+

函数接口定义:

double funcos( double e, double x );

其中用户传入的参数为误差上限e和自变量x;函数funcos应返回用给定公式计算出来、并且满足误差要求的\cos (x)cos(x)的近似值。输入输出均在双精度范围内。

裁判测试程序样例:

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

double funcos( double e, double x );

int main()
{    
    double e, x;

    scanf("%lf %lf", &e, &x);
    printf("cos(%.2f) = %.6f\n", x, funcos(e, x));

    return 0;
}

/* 你的代码将被嵌在这里 */

输入样例:

0.01 -3.14

输出样例:

cos(-3.14) = -0.999899
#include <stdio.h>
#include <math.h>

double funcos( double e, double x );

int main()
{    
    double e, x;

    scanf("%lf %lf", &e, &x);
    printf("cos(%.2f) = %.6f\n", x, funcos(e, x));

    return 0;
}

double funcos( double e, double x ){

	double tmp1=1,tmp2=1,tmp3=1,sum=1;
	int i,k;

	k=-1;

	for(i=2;tmp1>e;i+=2){
		tmp2 = tmp2*x*x;
		tmp3 = tmp3*i*(i-1);
		sum = sum + k*tmp2/tmp3;
		tmp1=tmp2/tmp3;
		k=-k;
	}

	return sum;

}






  • 28
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值