国外源文:http://devmaster.net/forums/topic/4648-fast-and-accurate-sinecosine/
译文:http://www.cnblogs.com/sun11086/archive/2009/03/20/1417944.html
程序里给的实现没有考虑-pi、pi以外的情况,这里给完善一下。为了提高运算速度,将浮点除法运算转成了整形。
#define SIN_PARA_A 1.27323954f
#define SIN_PARA_B 0.405284735f
inline
float mySin(float x)// -pi--pi
{
if (x< -PI){
x -= ( (int)((x+PI)*10000) /62832)*TWO_PI;
}
else if (x> PI){
x -= ( (int)((x-PI)*10000) /62832+1)*TWO_PI;
}
//compute sine
if (x< 0)
return ((SIN_PARA_A + SIN_PARA_B * x)* x);
else
return ((SIN_PARA_A - SIN_PARA_B * x)* x);
}
inline
float myCos(float x)
{
//compute cosine: sin(x + PI/2) = cos(x)
x+= HALF_PI;
if (x< -PI){
x -= ( (int)((x+PI)*10000) /62832)*TWO_PI;
}
else if (x> PI){
x -= ( (int)((x-PI)*10000) /62832+1)*TWO_PI;
}
if (x< 0)
return ((SIN_PARA_A + SIN_PARA_B * x)* x);
else
return ((SIN_PARA_A - SIN_PARA_B * x)* x);
}
在VS2005中测试,速度提高30%左右,并不是特别明显,可能是编译器本身已经做了优化。在其它没有优化的编译器中也许提升较明显,如单片机、DSP之类的编译器