使用arm-linux-gcc链接的时候无法找到sin和cos函数。
自己就干脆实现了这两个函数。
在0<x<π/2时,sin x 的泰勒级数是sin x = x - x^3/3! + x^5/5! - x^7/7! ……
而cos x = sin (π/2 - x)
所以只需完成求sinx的函数即可
- #define PI (float)3.1415926
- #define _2PI (float)6.28318
- #define PI_DIV_2 (float)1.570796
- float _sin(float x)
- {
- int sign=1; //符号
- int itemCnt=4; //泰勒技级数展开多项式的项数
- int k=0;
- float result=0;
- float tx; //展开式中各项的x^(2k)
- int factorial=1; //展开式中各项的(2k+1)!
-
- //标准化为正值 sin(-x)=-sin(x)
- if(x<0)
- {
- x=-x;
- sign *=-1;
- }
- //标准化x值到0~2π
- if(x>_2PI)
- x-=_2PI;
- //把0~2π标准到0~π
- if(x>PI)
- {
- x-=PI; //sin(π+x)=-sin(x)
- sign*=(-1);
- }
- //把0~π标准到0~π/2 sin(π-x)=sin(x)
- if(x>PI_DIV_2)
- {
- x=PI-x;
- }
-
-
- tx=x;
- for(k=0;k<itemCnt;k++)
- {
- if(k%2==0)
- {
- result += (tx / factorial) ;
- }
- else
- {
- result -= (tx / factorial) ;
- }
-
- tx *= (x*x);
- factorial *= (2*(k+1));
- factorial *= (2*(k+1)+1);
- }
- if(sign == 1)
- return result;
- else
- return -result;
- }
-
- float _cos(float x)
- {
- return _sin(PI_DIV_2 - x);
- }
|