快速正弦的完整实现

国外源文: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之类的编译器

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值