输入精度e,求cos(x)近似值,要求精确到最后一项的绝对值小于e,且定义并调用函数funcos(e,x),有式:cos(x)=pow(x,0)/0!-pow(x,2)/2!+pow(x,4)/4!-pow(x,6)/6!+………………#include#includemain(){double e,x,sq;double funcos(double e,double x);printf("Enter e,x:");scanf("%lf,%lf",&e,&x);sq=funcos(e,x);printf("cos(%.2f)=%f",x,sq);return 0;}double funcos(double e,double x){int flag=1,n=0;double fact(int n);double item,sum;do{item=flag*pow(x,2*n)/fact(2*n);sum+=item;n++;flag=-flag;}while((item/flag)>=e);retur
问题描述:
输入精度e,求cos(x)近似值,要求精确到最后一项的绝对值小于e,且定义并调用函数funcos(e,x),
有式:cos(x)=pow(x,0)/0!-pow(x,2)/2!+pow(x,4)/4!-pow(x,6)/6!+………………
#include
#include
main()
{
double e,x,sq;
double funcos(double e,double x);
printf("Enter e,x:");
scanf("%lf,%lf",&e,&x);
sq=funcos(e,x);
printf("cos(%.2f)=%f",x,sq);
return 0;
}
double funcos(double e,double x)
{
int flag=1,n=0;
double fact(int n);
double item,sum;
do{
item=flag*pow(x,2*n)/fact(2*n);
sum+=item;
n++;
flag=-flag;
}while((item/flag)>=e);
return sum;
}
double fact(int n)
{
int i,fact=1;
for(i=1;i
答