pub const fn tan(x: Real) -> RealExpand 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)returnsNaNtan(±∞)returnsNaNtan(±0.0)returns±0.0tan(k·π)returns0.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
Realtype (which isf64under the hood) - Made fully
const fncompatible - Uses the crate’s own
rem_pio2andk_tanimplementations
§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
constevaluation checks
All tests pass and produce results matching std::f64::tan within 1 ULP.