pxfm/tangent/cot.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::bits::EXP_MASK;
30use crate::common::f_fmla;
31use crate::double_double::DoubleDouble;
32use crate::sin::range_reduction_small;
33use crate::sincos_reduce::LargeArgumentReduction;
34use crate::tangent::tan::{tan_eval, tan_eval_dd};
35use crate::tangent::tanpi_table::TAN_K_PI_OVER_128;
36
37#[cold]
38fn 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 let tan_y = tan_eval_dd(y);
42
43 // num = tan(y) + tan(k*pi/64)
44 let num_dd = DoubleDouble::full_dd_add(tan_y, tan_k);
45 // den = 1 - tan(y)*tan(k*pi/64)
46 let den_dd = DoubleDouble::mul_add_f64(tan_y, -tan_k, 1.);
47
48 let cot_x = DoubleDouble::div(den_dd, num_dd);
49 cot_x.to_f64()
50}
51
52/// Cotangent in double precision
53///
54/// ULP 0.5
55pub fn f_cot(x: f64) -> f64 {
56 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 let mut argument_reduction = LargeArgumentReduction::default();
63
64 if x_e < E_BIAS + 16 {
65 // |x| < 2^16
66 if x_e < E_BIAS - 7 {
67 // |x| < 2^-7
68 if x_e < E_BIAS - 27 {
69 // |x| < 2^-27, |cot(x) - x| < ulp(x)/2.
70 if x == 0.0 {
71 // Signed zeros.
72 return if x.is_sign_negative() {
73 f64::NEG_INFINITY
74 } else {
75 f64::INFINITY
76 };
77 }
78
79 if x_e < E_BIAS - 53 {
80 return 1. / x;
81 }
82
83 let dx = DoubleDouble::from_quick_recip(x);
84 // taylor order 3
85 return DoubleDouble::f64_mul_f64_add(x, f64::from_bits(0xbfd5555555555555), dx)
86 .to_f64();
87 }
88 // No range reduction needed.
89 k = 0;
90 y = DoubleDouble::new(0., x);
91 } else {
92 // Small range reduction.
93 (y, k) = range_reduction_small(x);
94 }
95 } else {
96 // Inf or NaN
97 if x_e > 2 * E_BIAS {
98 if x.is_nan() {
99 return f64::NAN;
100 }
101 // tan(+-Inf) = NaN
102 return x + f64::NAN;
103 }
104
105 // Large range reduction.
106 (k, y) = argument_reduction.reduce(x);
107 }
108
109 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 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 let num_dd = DoubleDouble::add(tan_y, tan_k);
117 // den = 1 - tan(y)*tan(k*pi/64)
118 let den_dd = DoubleDouble::mul_add_f64(tan_y, -tan_k, 1.);
119
120 // num and den shifted for cot
121 let cot_x = DoubleDouble::div(den_dd, num_dd);
122
123 // Simple error bound: |1 / den_dd| < 2^(1 + floor(-log2(den_dd)))).
124 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 let tan_err = err * f_fmla(f64::from_bits(den_inv), cot_x.hi.abs(), 1.0);
128
129 let err_higher = cot_x.lo + tan_err;
130 let err_lower = cot_x.lo - tan_err;
131
132 let tan_upper = cot_x.hi + err_higher;
133 let tan_lower = cot_x.hi + err_lower;
134
135 // Ziv_s rounding test.
136 if tan_upper == tan_lower {
137 return tan_upper;
138 }
139
140 cot_accurate(y, tan_k)
141}
142
143#[cfg(test)]
144mod 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}