【问题描述】
使用麦克劳林公式求sin(x)得近似值,使其截断误差<0.5*10-13
【输入形式】
输入x,其中x为任意实数。
【输出形式】
输出sin(x)的近似值,保留13位小数。
【样例输入】
30000
【样例输出】
-0.8026659533203
【样例说明】
截断误差是指最后一项的绝对值<0.5*10-13时即满足条件,在此题中假定圆周率π的值为3.1415926535。
【特别提示】
由于本题要求精度较高,所有浮点数必须使用long double类型
#include <bits/stdc++.h>
using namespace std;
#define pi 3.1415926535
int main(){
long double x;
cin>>x;
while(x>2*pi){
x-=2*pi;
}
while (x<-2*pi){
x+=2*pi;
}
long double sinx = x;
long double v = x;
int a=-1,b=0;
while (fabs(v)>0.5e-13){
b+=2;
v = v*a*(x*x/(b*(b+1)));
sinx +=v;
}
cout<<fixed<<setprecision(13)<<sinx;
return 0;
}