无意中发现的一条求sin公式,总结之。
测试平台:Win7 64b + VS2012
克劳林公式:
算法描述:
1 #include <iostream> 2 #include <cmath> 3 using namespace std; 4 5 inline int factorial(int x) 6 { 7 if ( x==1 ) 8 return 1; 9 return factorial(x-1)*x; 10 } 11 12 inline double square (double x, int n) 13 { 14 double ret=1.0; 15 16 for (int i = 0; i<n; i++) 17 ret *= x; 18 19 return ret; 20 } 21 22 inline double _sin(double x) 23 { 24 double y = x/180*3.1415926; 25 return ( y - 1.0/factorial(3) * square(y, 3) + 1.0/factorial(5)*square(y, 5) ); 26 } 27 28 int main(void) 29 { 30 int x = 30; 31 32 cout << "sin (30) :" << endl; 33 cout << _sin (30) << endl; 34 cout << sin (30* 3.1415926/180) << endl; 35 36 cout << "sin (60) :" << endl; 37 cout << _sin(60) << endl; 38 cout << sin (60 * 3.1415926/180) << endl; 39 40 cout << "sin (90) :" << endl; 41 cout << _sin(90) << endl; 42 cout << sin(90*3.1415923/180) << endl; 43 return 0; 44 }
测试结果:
sin (30) : 0.500002 0.5 sin (60) : 0.866295 0.866025 sin (90) : 1.00452 1