fixed_math

Sine Function in FixedMath

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.

Implementation Details

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:

[ \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.

Key Functions

Example Usage

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.

Optimization Process: Reduction of Divisions

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:

  1. 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) ]

  2. 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) ]

  3. 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) ]

  4. 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) ]

  5. 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) ]

  6. 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) ]

  7. 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) ]

  8. 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.