Function ni_fast_atan2

Source
pub const fn ni_fast_atan2(y: f32, x: f32) -> f32
Expand description

Approximates the arctangent of y/x using a fast polynomial expansion.

  • Special cases:
    • atan2(0, 0)0.0 rad
    • atan2(1, 0)π/2 rad
    • atan2(0, -1)π rad

§Examples

// `atan2(1, 0)` -> `π/2`
assert!((ni_fast_atan2(1.0, 0.0) - core::f32::consts::FRAC_PI_2).abs() < 1e-6);
// `atan2(0, -1)` -> `π`
assert!((ni_fast_atan2(0.0, -1.0) - core::f32::consts::PI).abs() < 1e-6);

§Explanation:

This function uses a polynomial approximation of atan(z):

atan(z) ≈ z × (0.9998660 + z² × (-0.3302995 + z² × (0.1801410 +
           z² × (-0.0851330 + z² × 0.0208351))))

The nested form (Horner's method) is used for faster computation:

  • Reduces the number of multiplication operations.
  • Avoids explicit power calculations (e.g., z^3, z^5) by reusing .