pxfm/tangent/
cotf.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 7/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::polyeval::f_polyeval5;
31
32/// Computes cotangent
33///
34/// Max found ULP 0.4999999
35#[inline]
36pub fn f_cotf(x: f32) -> f32 {
37    let x_abs = x.to_bits() & 0x7fff_ffffu32;
38    let xd = x as f64;
39
40    // |x| < pi/32
41    if x_abs <= 0x3dc9_0fdbu32 {
42        // |x| < 0.000244141
43        if x_abs < 0x3980_0000u32 {
44            if x_abs == 0 {
45                return 1. / x;
46            }
47
48            // When |x| < 2^-12, the relative error of the approximation cot(x)
49            // is:
50            let ddx = x as f64;
51            let dx = 1. / ddx;
52            // taylor order 3
53            return f_fmla(ddx, f64::from_bits(0xbfd5555555555555), dx) as f32;
54        }
55
56        let xsqr = xd * xd;
57
58        /*
59           Generated by Sollya:
60           f_cotf = x/tan(x);
61           Q = fpminimax(f_cotf, [|0, 2, 4, 6, 8|], [|1, D...|], [0, pi/32]);
62
63           See ./notes/cotf.sollya
64        */
65        let p = f_polyeval5(
66            xsqr,
67            f64::from_bits(0x3ff0000000000000),
68            f64::from_bits(0xbfd5555555555466),
69            f64::from_bits(0xbf96c16c16fb8937),
70            f64::from_bits(0xbf6156688756cd43),
71            f64::from_bits(0xbf2bce669d7cd742),
72        );
73        return (p / xd) as f32;
74    }
75
76    if x_abs >= 0x7f80_0000u32 {
77        return x + f32::NAN;
78    }
79
80    // For |x| >= pi/32, we use the definition of cot(x) function:
81    // cot(a+b) = (1 - tan(a)tan(b)) / (tan(a) + tan(b))
82    // tanf_eval returns:
83    // - rs.tan_y = tan(pi/32 * y)          -> tangent of the remainder
84    // - rs.tan_k = tan(pi/32 * k)          -> tan of the main angle multiple
85    let rs = crate::tangent::evalf::tanf_eval(xd, x_abs);
86
87    // Then computing tan through identities
88    // num = tan(k*pi/32) + tan(y*pi/32)
89    let num = rs.tan_y + rs.tan_k;
90    // den = 1 - tan(k*pi/32) * tan(y*pi/32)
91    let den = f_fmla(rs.tan_y, -rs.tan_k, 1.);
92    (den / num) as f32
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98
99    #[test]
100    fn cotf_test() {
101        assert_eq!(f_cotf(0.0010348097), 966.36084);
102        assert_eq!(f_cotf(0.0020286469), 492.93872);
103        assert_eq!(f_cotf(-0.0020286469), -492.93872);
104        assert_eq!(f_cotf(1.0020286469), 0.63923126);
105        assert_eq!(f_cotf(-1.0020286469), -0.63923126);
106        assert_eq!(f_cotf(0.0), f32::INFINITY);
107        assert_eq!(f_cotf(-0.0), f32::NEG_INFINITY);
108        assert!(f_cotf(f32::INFINITY).is_nan());
109        assert!(f_cotf(f32::NEG_INFINITY).is_nan());
110        assert!(f_cotf(f32::NAN).is_nan());
111    }
112}