pxfm/tangent/
tanpif.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 6/2025. All rights reserved.
3 * //
4 * // Redistribution and use in source and binary forms, with or without modification,
5 * // are permitted provided that the following conditions are met:
6 * //
7 * // 1.  Redistributions of source code must retain the above copyright notice, this
8 * // list of conditions and the following disclaimer.
9 * //
10 * // 2.  Redistributions in binary form must reproduce the above copyright notice,
11 * // this list of conditions and the following disclaimer in the documentation
12 * // and/or other materials provided with the distribution.
13 * //
14 * // 3.  Neither the name of the copyright holder nor the names of its
15 * // contributors may be used to endorse or promote products derived from
16 * // this software without specific prior written permission.
17 * //
18 * // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 * // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29use crate::common::f_fmla;
30use crate::sin_cosf::ArgumentReducerPi;
31use crate::tangent::evalf::tanpif_eval;
32
33/// Computes tan(PI*x)
34///
35/// Max found ULP 0.5
36#[inline]
37pub fn f_tanpif(x: f32) -> f32 {
38    let ix = x.to_bits();
39    let e = ix & (0xff << 23);
40    if e > (150 << 23) {
41        // |x| > 2^23
42        if e == (0xff << 23) {
43            // x = nan or inf
44            if (ix.wrapping_shl(9)) == 0 {
45                // x = inf
46                return f32::NAN;
47            }
48            return x + x; // x = nan
49        }
50        return f32::copysign(0.0, x);
51    }
52
53    let argument_reduction = ArgumentReducerPi { x: x as f64 };
54
55    let (y, k) = argument_reduction.reduce();
56
57    if y == 0.0 {
58        let km = (k.abs() & 31) as i32; // k mod 32
59
60        match km {
61            0 => return 0.0f32.copysign(x),               // tanpi(n) = 0
62            16 => return f32::copysign(f32::INFINITY, x), // tanpi(n+0.5) = ±∞
63            8 => return f32::copysign(1.0, x),            // tanpi(n+0.25) = ±1
64            24 => return -f32::copysign(1.0, x),          // tanpi(n+0.75) = ∓1
65            _ => {}
66        }
67    }
68
69    let ax = ix & 0x7fff_ffff;
70    if ax < 0x38d1b717u32 {
71        // taylor series for tan(PI*x) where |x| < 0.0001
72        let dx = x as f64;
73        let dx_sqr = dx * dx;
74        // tan(PI*x) ~ PI*x + PI^3*x^3/3 + O(x^5)
75        let r = f_fmla(
76            dx_sqr,
77            f64::from_bits(0x4024abbce625be53),
78            f64::from_bits(0x400921fb54442d18),
79        );
80        return (r * dx) as f32;
81    }
82
83    // tanpif_eval returns:
84    // - rs.tan_y = tan(pi/32 * y)          -> tangent of the remainder
85    // - rs.tan_k = tan(pi/32 * k)          -> tan of the main angle multiple
86    let rs = tanpif_eval(y, k);
87
88    // Then computing tan through identities
89    // num = tan(k*pi/32) + tan(y*pi/32)
90    let num = rs.tan_y + rs.tan_k;
91    // den = 1 - tan(k*pi/32) * tan(y*pi/32)
92    let den = f_fmla(rs.tan_y, -rs.tan_k, 1.);
93    (num / den) as f32
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn test_tanpif() {
102        assert_eq!(f_tanpif(3.666738e-5), 0.00011519398);
103        assert_eq!(f_tanpif(1.0355987e-25), 3.2534293e-25);
104        assert_eq!(f_tanpif(5.5625), -5.0273395);
105        assert_eq!(f_tanpif(-29.75), 1.0);
106        assert_eq!(f_tanpif(-21.5625), 5.0273395);
107        assert_eq!(f_tanpif(-15.611655), 2.7329326);
108        assert_eq!(f_tanpif(115.30706), 1.4426143);
109        assert!(f_tanpif(f32::INFINITY).is_nan());
110        assert!(f_tanpif(f32::NAN).is_nan());
111    }
112}