Copyright (c) 2016,
All rights reserced.
文件名称:main.cpp
作者:李鑫完成日期:2016.3.27
问题描述:用sin泰勒展示编写程序,求sin(TT/2),sin(56`)得值。
#include <iostream>
using namespace std;
const double pi=3.1415926;
double mysin(double x);
double myabs(double x);
int main( )
{
cout<<"sin(π/2)的值为"<<mysin(pi/2)<<endl;
cout<<"sin(56°)的值为"<<mysin((56.0/180)*pi)<<endl;
return 0;
}
double mysin(double x)
{
double sum=x,x_pow=x,item;
int n=1,fact=1,sign=1;
do
{
fact=fact*(n+1)*(n+2);
x_pow*=x*x;
sign=-sign;
item =x_pow/fact*sign;
sum+=item;
n+=2;
}while(myabs(item)>1e-6);
return sum;
}
double myabs(double x)
{
return ((x>=0)?x:-x);
}