C语言编程计算sinx的近似值

如题所述

#include "stdio.h"
int main(int argc,char *argv[]){
double x,s,t,eps;
int i;
printf("Please enter x & eps(R:0<eps<1)...\n");
if(scanf("%lf%lf",&x,&eps)!=2 || eps<=0 || eps>=1){
printf("Input error, exit...\n");
return 0;
}
printf("sin(%g)≈",x);
for(s=t=x,x*=x,i=1;t>=eps;i++){
(t*=x)/=((i*i<<2)+i+i);
s += i&1 ? -t : t;
}
printf("%f\n",s);
return 0;
}

运行样例:

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-01-02
#include<stdio.h>
#include<math.h>
void main()
{
long float x,s=0;
int n,i,k,a=1,b;
printf("Input x,n:");
scanf("%lf,%d",&x,&n);
for(i=1;i<=n;i++)
{

a*=2*n-1;
b=pow(-1,i-1);
s=s+b*pow(x,2*i-1)/(double)a;
a*=2*n;
}
printf("sinx=%lf\n",s);
}
相似回答