pub const fn atan2(y: Real, x: Real) -> RealExpand description
Computes the four-quadrant arctangent of y / x (atan2(y, x)).
Returns the angle in radians between the positive x-axis and the point
(x, y), in the range [-π, π]. This function correctly handles all
special cases (including signed zeros, infinities, and NaNs) as required
by IEEE 754-2008.
§Special cases
atan2(±0, ±0)returns±0or±πaccording to the sign ofxatan2(±y, ±0)returns±π/2atan2(±y, ±∞)returns±0or±πatan2(±∞, ±x)returns±π/2atan2(±∞, ±∞)returns±π/4or±3π/4- Any NaN argument produces NaN
§Implementation notes
This is a const fn-compatible port of the FreeBSD libm implementation
(e_atan2.c). The original algorithm uses range reduction and calls to
atan for the core computation, with careful handling of the PI_LO
correction for negative x to ensure correct rounding.
Modifications for this crate:
- Adapted to the generic
Realtype (which isf64under the hood) - Made fully
const fncompatible using method calls (.abs(),.is_nan()) instead of thefabshelper - Uses the
const fnversion ofatanfromsuper::atan - Removed
no_panicattribute and anyf64-specific hardcoding
§Testing
This function has been validated with a comprehensive test suite that includes:
- Sanity checks across all four quadrants and axis angles
- All IEEE 754 special values (NaN, signed zeros, infinities)
- Extreme ratio cases (
|y/x| > 2⁶⁴and|y/x| < 2⁻⁶⁴) - Explicit verification of the
PI_LOcorrection path - Fast-path for
x == 1.0
All tests pass with bit-exact or correctly-rounded results matching the
original libm implementation.