Skip to main content

tan

Function tan 

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

Computes the tangent of x in radians (tan(x)).

Returns a value in (-∞, +∞). The function has vertical asymptotes (poles) at odd multiples of π/2, where the result approaches ±∞ with the correct sign.

§Special cases

  • tan(NaN) returns NaN
  • tan(±∞) returns NaN
  • tan(±0.0) returns ±0.0
  • tan(k·π) returns 0.0 (approximately, after argument reduction)

§Implementation notes

This is a const fn-compatible port of the FreeBSD libm implementation (s_tan.c). It uses a fast path for |x| ≤ π/4 (via k_tan), returns the input exactly for |x| < 2⁻²⁷, handles infinities/NaNs, and otherwise performs argument reduction with rem_pio2 before calling k_tan with the appropriate parity flag.

§Modifications for this crate

  • Adapted to the generic Real type (which is f64 under the hood)
  • Made fully const fn compatible
  • Uses the crate’s own rem_pio2 and k_tan implementations

§Testing

The function is validated by a comprehensive test suite that includes:

  • Special values (NaN, infinities, zeros, multiples of π)
  • Tiny arguments (exact fast-path for |x| < 2⁻²⁷)
  • Odd symmetry (tan(-x) == -tan(x))
  • Small arguments (fast path)
  • Values approaching poles (±∞ behavior)
  • Medium and very large arguments (argument reduction stress)
  • Subnormal numbers
  • Deterministic pseudo-random inputs
  • Explicit const evaluation checks

All tests pass and produce results matching std::f64::tan within 1 ULP.