C 中的高效三角函数

问题描述 投票:0回答:1

我正在实现一些算法,我需要使用

cos()
sin()
atan2()
。特别是在循环中,这些函数非常慢。对于
atan2()
,我使用
atan()
并进行了一些修改:

double atan2_custom(double y, double x) {
    if (x > 0) {
        return atan(y / x);
    } else if (x < 0 && y >= 0) {
        return atan(y / x) + M_PI;
    } else if (x < 0 && y < 0) {
        return atan(y / x) - M_PI;
    } else if (x == 0 && y > 0) {
        return M_PI / 2;
    } else if (x == 0 && y < 0) {
        return -M_PI / 2;
    } else {
        return 0; // x and y are both zero
    }
}

有没有比这些数学函数更有效的替代方法?预先感谢。

c performance runtime trigonometry
1个回答
0
投票

有没有比这些数学更有效的替代方案 功能?预先感谢。

方法之一:

使用表格组合计算三角正弦函数 查找和线性插值。

Calculation of the nearest integer table index
Compute the fractional portion (fract) of the table index.
The final result equals (1.0f-fract)*a + fract*b;

哪里

b = 表[索引];
c = 表[索引+1];

来源:CMSIS-DSP 文档

以及一些示例实现:https://www.keil.com/pack/doc/CMSIS/DSP/html/arm_linear_interp_example_f32_8c-example.html#a13

© www.soinside.com 2019 - 2024. All rights reserved.