// cos(x) = x^0/0!-x^2/2!+x^4/4!-x^6/6!+……
#include
#include
const double PI = 3.14159265358979;
int main () {
double x,e;
double mycos(double x,double e);
printf("请输入精度e和度数α:");
scanf("%lf,%lf",&e,&x);
x = x*PI/180.0;
printf("cos(%.2lf) = %.13lf\n",180.0*x/PI,mycos(x,e));
return 0;
}
double mycos(double x,double e) {
int i = 2,flag = -1;
int denominator = 1;
double sum = 1.0,item = 10.0;
double numerator = 1.0;
double function(int i);
while(fabs(item) > e) {
numerator *= x*x;
denominator *= i*(i - 1);
item = flag*numerator/(double)denominator;
sum = sum + item;
flag = -flag;
i = i + 2;
}
return sum;
}
double function(int i) {
int a;
double fact = 1.0;
for(a = 2;a <= i;i++) {
fact = fact*a;
}
return fact;
}
#include
#include
main ()
{
double e,x;
double cosx(double x);
scanf("%lf%lf",&e,&x);
printf("%f\n",cosx(x));
return 0;
}
double cosx(double x)
{
int i,flag;
double e,sum,item;
double function(int i);
flag=1;
i=0;
sum=0;
while(fabs(item)>e){
item=flag*pow(x,i)/function(i);
sum=sum+item;
flag=-flag;
i=i+2;
}
return sum;
}
double function(int i)
{
int a;
double fact;
fact=1.00;
for(a=2;a<=i;i++){
fact=fact*a;
}
return fact;
}