Coverage Report

Created: 2026-01-19 07:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/pxfm-0.1.27/src/gamma/beta.rs
Line
Count
Source
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
 */
29
use crate::double_double::DoubleDouble;
30
use crate::f_exp;
31
use crate::gamma::lgamma::lgamma_core;
32
33
/// Computes beta function
34
0
pub fn f_beta(a: f64, b: f64) -> f64 {
35
0
    let ax = a.to_bits();
36
0
    let bx = b.to_bits();
37
38
0
    if ax >= 0x7ffu64 << 52 || ax == 0 || bx >= 0x7ffu64 << 52 || bx == 0 {
39
0
        if (ax >> 63) != 0 || (bx >> 63) != 0 {
40
            // |a| < 0 or |b| < 0
41
0
            return f64::NAN;
42
0
        }
43
0
        if ax.wrapping_shl(1) == 0 || bx.wrapping_shl(1) == 0 {
44
            // |a| == 0 || |b| == 0
45
0
            if ax.wrapping_shl(1) != 0 || bx.wrapping_shl(1) != 0 {
46
0
                return f64::INFINITY;
47
0
            }
48
0
            return f64::NAN;
49
0
        }
50
0
        if a.is_infinite() || b.is_infinite() {
51
            // |a| == inf or |b| == inf
52
0
            return 0.;
53
0
        }
54
0
        return a + f64::NAN; // nan
55
0
    }
56
57
0
    let mut sign = 1i32;
58
0
    let (mut y, sgngamf) = lgamma_core(DoubleDouble::from_full_exact_add(a, b).to_f64());
59
0
    sign *= sgngamf; /* keep track of the sign */
60
0
    let (y1, sgngamf) = lgamma_core(b);
61
0
    y = DoubleDouble::quick_dd_sub(y1, y);
62
0
    sign *= sgngamf;
63
0
    let (y1, sgngamf) = lgamma_core(a);
64
0
    y = DoubleDouble::quick_dd_add(y1, y);
65
0
    sign *= sgngamf;
66
0
    f_exp(y.to_f64()) * (sign as f64)
67
0
}
68
69
#[cfg(test)]
70
mod tests {
71
    use crate::f_beta;
72
73
    #[test]
74
    fn test_beta() {
75
        assert!(f_beta(-5., 15.).is_nan());
76
        assert!(f_beta(5., -15.).is_nan());
77
        assert!(f_beta(f64::NAN, 15.).is_nan());
78
        assert!(f_beta(15., f64::NAN).is_nan());
79
        assert_eq!(f_beta(f64::INFINITY, 0.), f64::INFINITY);
80
        assert_eq!(f_beta(f64::INFINITY, 1.), 0.);
81
        assert_eq!(f_beta(1., f64::INFINITY), 0.);
82
        assert_eq!(f_beta(5., 3.), 0.009523809523809526);
83
        assert_eq!(f_beta(3., 5.), 0.009523809523809526);
84
        assert_eq!(f_beta(12., 23.), 1.5196994506201402e-10);
85
    }
86
}