1. 程式人生 > >近似計算sin(x)

近似計算sin(x)

#include<stdio.h>
#include<math.h>
main()
{
	double  x,eps,s,y=0,y0,t;
	int n,j;
	printf("Enter x & eps:");
	scanf("%lf%lf",&x,&eps);
	n=t=j=1;
	s=x;
	do
	{
		y0=y;
		if(n%2==0)
			y=y-s/t;
		else
			y=y+s/t;
		s*=x*x;
		t=t*(j+1)*(j+2);
		n++;
		j+=2;
	}while(fabs(y0-y)>eps);
	printf("%f   %lf\n",sin(x),y);
}