Skip to main content

atan

Function atan 

Source
pub const fn atan(x: Real) -> Real
Expand description

Computes the arctangent of x (atan(x)).

Returns the principal value in the range [-π/2, π/2] radians.

§Special cases

  • atan(NaN) returns NaN
  • atan(±0) returns ±0 (preserving the sign of zero)
  • atan(±∞) returns ±π/2

§Implementation notes

This is a const fn-compatible port of the FreeBSD libm implementation (s_atan.c). The algorithm reduces the argument based on the magnitude of |x| and evaluates it using either a polynomial approximation or an addition formula with precomputed constants (ATANHI/ATANLO).

Modifications for this crate:

  • Adapted to the generic Real type (which is f64 under the hood)
  • Made fully const fn compatible
  • Removed force_eval! (subnormal underflow side-effect, not relevant in const context)
  • Replaced the i! macro with direct array indexing
  • Used the .abs() method instead of the fabs helper

§Testing

This function is tested directly with:

  • Sanity checks for standard angles (π/6, π/4, π/3 and their negatives)
  • Special values: zero, ±infinity, NaN

It is also exercised by the full atan2 test suite (which calls atan internally). All tests pass and produce results matching the original libm implementation.