69,371
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
void main()
{
int n=0,i,d;
double x,a,b,z=0;
scanf("%lf",&x);
do
{
for(i=1,b=1;i<=n;i++)
{
b=b*i;
}
z+=(x*1en/b)*d;
n+=2;
d=-d;
}
while(x*1en/b<=1e-6);
printf("cos%d=%lf\n",x,z);
}
#include <iostream>
using namespace std ;
//get the an
double x_an(double x,int i)
{
double res = 1.0;
for(int t = i ; t > 0; t--)
{
res*=(x/t) ;
}
return res ;
}
//cos(x)=1-(x^2/2!)+(x^4/4!)-(x^6/6!)..
double x_cos(double x,double rate = 1e-6)
{
int i = 2 ;
int ct = 1 ;
double res = 1 ;
double sig = -1;
double an = 0 ;
while(1)
{
sig = (ct%2 == 0 ? 1.0 : -1.0);
i=2*ct;
an = x_an(x,i);
if(an < rate)break;
an *= sig;
res += an ;
ct++;
}
return res;
}
int main()
{
double x ;
while(cin>>x){
cout<<x_cos(x)<<endl;
}
return 0;
}