pxfm/triangle/
cathetus.rs

1/*
2 * // Copyright (c) Radzivon Bartoshyk 9/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::double_double::DoubleDouble;
30
31/// Computes the missing leg of a right triangle
32///
33/// Given a hypotenuse `x` and a known leg `y`, returns
34/// `sqrt(x^2 - y^2)` = the length of the other leg.
35///
36/// Domain: requires `|x| >= |y|`. Returns NaN if the input
37/// is outside this range.
38pub fn f_cathetus(x: f64, y: f64) -> f64 {
39    let x_abs = x.abs();
40    let y_abs = y.abs();
41
42    let x_bits = x_abs.to_bits();
43    let y_bits = y_abs.to_bits();
44
45    let a_u = x_bits.max(y_bits);
46
47    let mut dx = x;
48    let mut dy = y;
49
50    const EXP_MASK_F64: u64 = 0x7FF0_0000_0000_0000;
51    if a_u >= EXP_MASK_F64 {
52        // x or y is inf or nan
53        if f64::from_bits(x_bits).is_nan() || f64::from_bits(y_bits).is_nan() {
54            return f64::NAN;
55        }
56        if f64::from_bits(x_bits).is_infinite() || f64::from_bits(y_bits).is_infinite() {
57            if f64::from_bits(x_bits).is_infinite() && f64::from_bits(y_bits).is_infinite() {
58                // ∞² - ∞² is undefined
59                return f64::NAN;
60            }
61            return f64::INFINITY;
62        }
63        return f64::from_bits(x_bits);
64    }
65    if x_abs < y_abs {
66        // Would yield sqrt(negative), undefined
67        return f64::NAN;
68    }
69    if x_abs == y_abs {
70        // sqrt(c² - c²) = 0
71        return 0.0;
72    }
73
74    let e_x = x_bits >> 52;
75    let e_y = y_bits >> 52;
76    let unbiased_e_x = (e_x as i32).wrapping_sub(1023);
77    let mut scale = 1f64;
78
79    if e_y == 0 {
80        if e_x - e_y > 52 {
81            // y is too small to make difference, so result is just |x|
82            return x_abs;
83        }
84        dx *= f64::from_bits(0x6bb0000000000000); // 2^700
85        dy *= f64::from_bits(0x6bb0000000000000); // 2^700
86        scale = f64::from_bits(0x1430000000000000); // 2^(-700 / 2)
87    } else if unbiased_e_x >= 510 {
88        dx *= f64::from_bits(0x1430000000000000); // 2^-700
89        dy *= f64::from_bits(0x1430000000000000); // 2^-700
90        scale = f64::from_bits(0x6bb0000000000000); // 2^(700 / 2)
91    } else if unbiased_e_x <= -450 {
92        dx *= f64::from_bits(0x6bb0000000000000); // 2^700
93        dy *= f64::from_bits(0x6bb0000000000000); // 2^700
94        scale = f64::from_bits(0x1430000000000000); // 2^(-700)
95    }
96
97    let dy2 = DoubleDouble::from_exact_mult(dy, dy);
98    let dx2 = DoubleDouble::from_exact_mult(dx, dx);
99    let p = DoubleDouble::sub(dx2, dy2);
100    let cath = p.fast_sqrt(); // sqrt(x^2 - y^2)
101    cath.to_f64() * scale
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107
108    #[test]
109    fn test_cathethus() {
110        assert_eq!(
111            f_cathetus(0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002248996583584318,
112                       0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002842248694776204),
113            0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002248996583584318
114        );
115        assert_eq!(
116            f_cathetus(0.00003241747618121237, 0.00003241747618121195),
117            5.219099637789996e-12
118        );
119        assert_eq!(f_cathetus(0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003382112264930946,
120                              -0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005284550413954603),
121                   0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003381699384228079);
122        assert_eq!(f_cathetus(5., 3.), 4.);
123        assert_eq!(f_cathetus(5., 4.), 3.);
124        assert_eq!(f_cathetus(13., 12.), 5.);
125        assert_eq!(f_cathetus(65., 16.), 63.);
126        assert_eq!(f_cathetus(25., 24.), 7.);
127        assert!(f_cathetus(24., 25.).is_nan());
128    }
129
130    #[test]
131    fn test_cathetus_edge_cases() {
132        assert_eq!(f_cathetus(0.0, 0.0), 0.0);
133        assert_eq!(f_cathetus(f64::INFINITY, 0.0), f64::INFINITY);
134        assert_eq!(f_cathetus(0.0, f64::INFINITY), f64::INFINITY);
135        assert!(f_cathetus(f64::INFINITY, f64::INFINITY).is_nan());
136        assert_eq!(f_cathetus(f64::NEG_INFINITY, 0.0), f64::INFINITY);
137        assert_eq!(f_cathetus(0.0, f64::NEG_INFINITY), f64::INFINITY);
138        assert!(f_cathetus(f64::NEG_INFINITY, f64::NEG_INFINITY).is_nan());
139        assert!(f_cathetus(f64::NAN, 1.0).is_nan());
140        assert!(f_cathetus(1.0, f64::NAN).is_nan());
141    }
142}