问题
当浮点数很大时,作为三角函数的入参,计算出来的结果会让人大跌眼镜!

X64 Windows计算器计算的sin(110000),上面那个数值明显是不对的

分析
查看sinf
函数原型,是通过查表法来实现的,也就说芯片内部不可能维护一个巨大的表格,入参很大会有误差
C++
//! \brief Single-Precision Floating-Point Sine (radians)
//! \param[in] X single precision floating point argument
//! \return the sine of a floating-point argument X (in radians) using table
//! look-up and Taylor series expansion between the look-up table entries.
//!
//! \note
//! -# This is a standard C math function and requires "math.h" to be
//! included
//! -# For COFF executables float and double are both single precision
//! 32-bit floating point types, therefore, the double and float variant of
//! this function may be used interchangeably.
//!
//! <table>
//! <caption id="multi_row">Performance Data</caption>
//! <tr><th>Cycles <th> Comment
//! <tr><td> 38 <td> Cycle count includes the call and return
//! </table>
//
float32_t sinf (float32_t X);
解决
需要将角度值限制在±2π范围内(理论上说这个范围内的精度是最好的)
Plain
// 将角度归一化到[-2π, 2π]范围内
float normalize_angle(float angle)
{
const float two_pi = 2.0f * PI;
float normalized = fmodf(angle, two_pi);
if(normalized < -PI) normalized += two_pi;
if(normalized > PI) normalized -= two_pi;
return normalized;
}
调试看归一化后的函数值
