C语言 计算cos(x)的近似值

Posted Aiden (winner)

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C语言 计算cos(x)的近似值相关的知识,希望对你有一定的参考价值。

精确要求:当通项的绝对值小于10^-6时为止。
公式如下:
在这里插入图片描述

#include <stdio.h>
#include <math.h>
double mycos(double x){
	int n=1;
	double s=0,t=1.0;
	while(fabs(t)>=1e-6){
		s+=t;
		t*=(-1)*x*x/(n*(n+1));
		n=n+2;
	}
	return s;
}
void main(){
	double x;
	printf("请输入通项:");
	scanf("%lf",&x);
	printf("fx(%lf)=%lf,%lf",x,mycos(x),cos(x));
}

在这里插入图片描述

以上是关于C语言 计算cos(x)的近似值的主要内容,如果未能解决你的问题,请参考以下文章