The FixedMath library provides an implementation for the sine function using Maclaurin series expansion, a form of the Taylor series centered at zero. This method is chosen for its efficiency in computing trigonometric functions within a fixed range of input values, ensuring both accuracy and performance.
The sine function’s implementation involves normalizing the input angle to a specific range and then calculating the sine using a truncated series expansion. Here’s an overview of the process:
Normalization: The input angle (rad
) is normalized to the range (-\frac{\phi}{2}) to (3\frac{\phi}{2}), where (\phi) represents the value of pi in the fixed-point format. This normalization ensures the angle is within the optimal range for the Maclaurin series calculation.
Maclaurin Series for Sine: The sine of the angle is computed as:
[ \sin(x) = x - \frac{x^3}{3!} + \frac{x^5}{5!} - \frac{x^7}{7!} + \cdots ]
However, to optimize performance, the series is simplified and truncated, with the error kept below a certain threshold. The implementation uses a series expansion with a reduced number of divisions for efficiency:
[ x \left( 1 - x^2 \left( \frac{1}{6} - \frac{x^2}{120} \left( 1 - \frac{x^2}{42} \right) \right) \right) ]
This approximation not only simplifies the computation but also significantly reduces the computational overhead by minimizing the number of division operations required.
sin_range(fixed_t rad) noexcept
: Normalizes the input angle into the correct range for the sine calculation.
sin(fixed_t rad) noexcept
: Calculates the sine of the given angle in radians using the optimized Maclaurin series expansion.
fixed_t rad = ...; // some angle in radians
fixed_t sin_value = sin(rad); // calculates sine of rad
This sine function implementation ensures high accuracy for angles within the normalized range, making it suitable for various applications requiring trigonometric calculations in fixed-point arithmetic.
The implementation details reveal an innovative approach to reducing the computational cost associated with division operations in the series expansion. By reorganizing the equation and strategically applying mathematical properties, the number of divisions is significantly reduced, enhancing the function’s performance while maintaining accuracy. This optimization is particularly beneficial in environments where division operations are costly in terms of processing time.
The sine function’s optimization involves a step-by-step transformation of the Maclaurin series to reduce the number of division operations required.
The series of transformations applied to the sine function calculation is as follows:
Initial Equation: [ x \left( 1 - x^2 \left( 1 - x^2 \left( 1 - x^2 \cdot rac{1}{42} ight) \cdot rac{1}{20} ight) \cdot rac{1}{6} ight) ]
Reduction of Division Operations 1: [ x \left( 1 - x^2 \left( 1 - x^2 \left( 1 - x^2 \cdot \left( rac{1}{2} \cdot rac{1}{21} ight) ight) \cdot \left( rac{1}{4} \cdot rac{1}{5} ight) ight) \cdot \left( rac{1}{2} \cdot rac{1}{3} ight) ight) ]
Reduction of Division Operations 2: [ x \left( 1 - x^2 \left( 1 - x^2 \cdot rac{1}{21} \left( 21 - x^2 \cdot rac{1}{2} ight) \cdot rac{1}{4} \cdot rac{1}{5} ight) \cdot rac{1}{2} \cdot rac{1}{3} ight) ]
Reduction of Division Operations 3: [ x \left( 1 - x^2 \left( 1 - x^2 \cdot rac{1}{105} \left( 21 - x^2 \cdot rac{1}{2} ight) \cdot rac{1}{4} ight) \cdot rac{1}{2} \cdot rac{1}{3} ight) ]
Consolidation of Terms: [ x \left( 1 - x^2 \cdot rac{1}{630} \left( 105 - x^2 \left( 21 - x^2 \cdot rac{1}{2} ight) \cdot rac{1}{4} ight) \cdot rac{1}{2} ight) ]
Further Consolidation: [ x \left( rac{3 \cdot 5 \cdot 21 - x^2 \left( 5 \cdot 21 - x^2 \left( 21 - x^2 \cdot rac{1}{2} ight) \cdot rac{1}{4} ight) \cdot rac{1}{2} }{3 \cdot 5 \cdot 21} ight) ]
Final Equation before Simplification: [ x \left( rac{315 - x^2 \left( 105 - x^2 \left( 21 - x^2 \cdot rac{1}{2} ight) \cdot rac{1}{4} ight) \cdot rac{1}{2} }{315} ight) ]
Simplified Final Equation: [ x \left( rac{315 - x^2 \left( 105 - x^2 \left( 42 - x^2 ight) \cdot rac{1}{8} ight) \cdot rac{1}{2} }{315} ight) ]
This step-by-step approach highlights the methodical reduction in division operations, leading to the final, optimized equation used for calculating the sine function in the FixedMath library.