我有一个科学代码,它使用同一参数的正弦和余弦(我基本上需要该参数的复指数)。我想知道是否可以比分别调用正弦和余弦函数更快。
而且我只需要大约 0.1% 的精度。那么有什么方法可以找到默认的三角函数并截断幂级数以提高速度?
我想到的另一件事是,有没有办法执行余数运算,使结果始终为正?在我自己的算法中我使用了x=fmod(x,2*pi);
但如果 x 为负数,我需要添加 2pi (较小的域意味着我可以使用较短的幂级数)
编辑:LUT 被证明是最好的方法,但是我很高兴我了解了其他近似技术。我还建议使用明确的中点近似。这就是我最终所做的:
const int N = 10000;//about 3e-4 error for 1000//3e-5 for 10 000//3e-6 for 100 000
double *cs = new double[N];
double *sn = new double[N];
for(int i =0;i<N;i++){
double A= (i+0.5)*2*pi/N;
cs[i]=cos(A);
sn[i]=sin(A);
}
以下部分近似(中点)sin cos(2*pi*(wc2+tj]*(corp*tj]-wc)))
double A=(wc2+t[j]*(cotp*t[j]-wc));
int B =(int)N*(A-floor(A));
re += cs[B]*f[j];
im += sn[B]*f[j];
另一种方法可能是使用切比雪夫分解。您可以使用正交性属性来查找系数。针对指数优化,它看起来像这样:
double fastsin(double x){
x=x-floor(x/2/pi)*2*pi-pi;//this line can be improved, both inside this
//function and before you input it into the function
double x2 = x*x;
return (((0.00015025063885163012*x2-
0.008034350857376128)*x2+ 0.1659789684145034)*x2-0.9995812174943602)*x;} //7th order chebyshev approx