Coverage Report

Created: 2025-11-05 08:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.25/src/tangent/cot.rs
Line
Count
Source
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
 */
29
use crate::bits::EXP_MASK;
30
use crate::common::f_fmla;
31
use crate::double_double::DoubleDouble;
32
use crate::sin::range_reduction_small;
33
use crate::sincos_reduce::LargeArgumentReduction;
34
use crate::tangent::tan::{tan_eval, tan_eval_dd};
35
use crate::tangent::tanpi_table::TAN_K_PI_OVER_128;
36
37
#[cold]
38
0
fn cot_accurate(y: DoubleDouble, tan_k: DoubleDouble) -> f64 {
39
    // Computes tan(x) through identities
40
    // tan(a+b) = (tan(a) + tan(b)) / (1 - tan(a)tan(b)) = (tan(y) + tan(k*pi/128)) / (1 - tan(y)*tan(k*pi/128))
41
0
    let tan_y = tan_eval_dd(y);
42
43
    // num = tan(y) + tan(k*pi/64)
44
0
    let num_dd = DoubleDouble::full_dd_add(tan_y, tan_k);
45
    // den = 1 - tan(y)*tan(k*pi/64)
46
0
    let den_dd = DoubleDouble::mul_add_f64(tan_y, -tan_k, 1.);
47
48
0
    let cot_x = DoubleDouble::div(den_dd, num_dd);
49
0
    cot_x.to_f64()
50
0
}
51
52
/// Cotangent in double precision
53
///
54
/// ULP 0.5
55
0
pub fn f_cot(x: f64) -> f64 {
56
0
    let x_e = (x.to_bits() >> 52) & 0x7ff;
57
    const E_BIAS: u64 = (1u64 << (11 - 1u64)) - 1u64;
58
59
    let y: DoubleDouble;
60
    let k;
61
62
0
    let mut argument_reduction = LargeArgumentReduction::default();
63
64
0
    if x_e < E_BIAS + 16 {
65
        // |x| < 2^16
66
0
        if x_e < E_BIAS - 7 {
67
            // |x| < 2^-7
68
0
            if x_e < E_BIAS - 27 {
69
                // |x| < 2^-27, |cot(x) - x| < ulp(x)/2.
70
0
                if x == 0.0 {
71
                    // Signed zeros.
72
0
                    return if x.is_sign_negative() {
73
0
                        f64::NEG_INFINITY
74
                    } else {
75
0
                        f64::INFINITY
76
                    };
77
0
                }
78
79
0
                if x_e < E_BIAS - 53 {
80
0
                    return 1. / x;
81
0
                }
82
83
0
                let dx = DoubleDouble::from_quick_recip(x);
84
                // taylor order 3
85
0
                return DoubleDouble::f64_mul_f64_add(x, f64::from_bits(0xbfd5555555555555), dx)
86
0
                    .to_f64();
87
0
            }
88
            // No range reduction needed.
89
0
            k = 0;
90
0
            y = DoubleDouble::new(0., x);
91
0
        } else {
92
0
            // Small range reduction.
93
0
            (y, k) = range_reduction_small(x);
94
0
        }
95
    } else {
96
        // Inf or NaN
97
0
        if x_e > 2 * E_BIAS {
98
0
            if x.is_nan() {
99
0
                return f64::NAN;
100
0
            }
101
            // tan(+-Inf) = NaN
102
0
            return x + f64::NAN;
103
0
        }
104
105
        // Large range reduction.
106
0
        (k, y) = argument_reduction.reduce(x);
107
    }
108
109
0
    let (tan_y, err) = tan_eval(y);
110
111
    // Computes tan(x) through identities.
112
    // tan(a+b) = (tan(a) + tan(b)) / (1 - tan(a)tan(b)) = (tan(y) + tan(k*pi/128)) / (1 - tan(y)*tan(k*pi/128))
113
0
    let tan_k = DoubleDouble::from_bit_pair(TAN_K_PI_OVER_128[(k & 255) as usize]);
114
115
    // num = tan(y) + tan(k*pi/64)
116
0
    let num_dd = DoubleDouble::add(tan_y, tan_k);
117
    // den = 1 - tan(y)*tan(k*pi/64)
118
0
    let den_dd = DoubleDouble::mul_add_f64(tan_y, -tan_k, 1.);
119
120
    // num and den shifted for cot
121
0
    let cot_x = DoubleDouble::div(den_dd, num_dd);
122
123
    // Simple error bound: |1 / den_dd| < 2^(1 + floor(-log2(den_dd)))).
124
0
    let den_inv = ((E_BIAS + 1) << (52 + 1)) - (den_dd.hi.to_bits() & EXP_MASK);
125
    // For tan_x = (num_dd + err) / (den_dd + err), the error is bounded by:
126
    //   | tan_x - num_dd / den_dd |  <= err * ( 1 + | tan_x * den_dd | ).
127
0
    let tan_err = err * f_fmla(f64::from_bits(den_inv), cot_x.hi.abs(), 1.0);
128
129
0
    let err_higher = cot_x.lo + tan_err;
130
0
    let err_lower = cot_x.lo - tan_err;
131
132
0
    let tan_upper = cot_x.hi + err_higher;
133
0
    let tan_lower = cot_x.hi + err_lower;
134
135
    // Ziv_s rounding test.
136
0
    if tan_upper == tan_lower {
137
0
        return tan_upper;
138
0
    }
139
140
0
    cot_accurate(y, tan_k)
141
0
}
142
143
#[cfg(test)]
144
mod tests {
145
    use super::*;
146
147
    #[test]
148
    fn cot_test() {
149
        assert_eq!(f_cot(2.3006805685393681E-308), 4.346539948546049e307);
150
        assert_eq!(f_cot(5070552515158872000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.), 25.068466719883585);
151
        assert_eq!(f_cot(4.9406564584124654E-324), f64::INFINITY);
152
        assert_eq!(f_cot(0.0), f64::INFINITY);
153
        assert_eq!(f_cot(1.0), 0.6420926159343308);
154
        assert_eq!(f_cot(-0.5), -1.830487721712452);
155
        assert_eq!(f_cot(12.0), -1.5726734063976893);
156
        assert_eq!(f_cot(-12.0), 1.5726734063976893);
157
        assert!(f_cot(f64::INFINITY).is_nan());
158
        assert!(f_cot(f64::NEG_INFINITY).is_nan());
159
        assert!(f_cot(f64::NAN).is_nan());
160
    }
161
}