| 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::common::{is_integer, is_odd_integer}; |
| 30 |
use crate::gamma::lgamma::lgamma_core; |
| 31 |
|
| 32 |
/// Computes log(gamma(x)) |
| 33 |
/// |
| 34 |
/// Returns gamma value + sign. |
| 35 |
pub fn f_lgamma_r(x: f64) -> (f64, i32) { |
| 36 |
// filter out exceptional cases |
| 37 |
let xb = x.to_bits().wrapping_shl(1); |
| 38 |
if xb >= 0x7ffu64 << 53 || xb == 0 { |
| 39 |
if x.is_nan() { |
| 40 |
return (f64::NAN, 1); |
| 41 |
} |
| 42 |
if xb == 0 { |
| 43 |
return (f64::INFINITY, 1 - 2 * ((x.to_bits() >> 63) as i32)); |
| 44 |
} |
| 45 |
if xb.wrapping_shl(11) == 0 { |
| 46 |
return (f64::INFINITY, 1); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
if is_integer(x) { |
| 51 |
if x == 2. || x == 1. { |
| 52 |
return (0., 1); |
| 53 |
} |
| 54 |
if x.is_sign_negative() { |
| 55 |
let is_odd = (!is_odd_integer(x)) as i32; |
| 56 |
return (f64::INFINITY, 1 - (is_odd << 1)); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
let (r, sign_gam) = lgamma_core(x); |
| 61 |
(r.to_f64(), sign_gam) |
| 62 |
} |
| 63 |
|
| 64 |
#[cfg(test)] |
| 65 |
mod tests { |
| 66 |
use crate::f_lgamma_r; |
| 67 |
|
| 68 |
#[test] |
| 69 |
fn test_lgamma_rf() { |
| 70 |
assert_eq!(f_lgamma_r(-0.), (f64::INFINITY, -1)); |
| 71 |
assert_eq!(f_lgamma_r(f64::NEG_INFINITY), (f64::INFINITY, 1)); |
| 72 |
assert_eq!(f_lgamma_r(f64::INFINITY), (f64::INFINITY, 1)); |
| 73 |
assert_eq!(f_lgamma_r(0.), (f64::INFINITY, 1)); |
| 74 |
assert_eq!(f_lgamma_r(5.), (3.1780538303479458, 1)); |
| 75 |
assert_eq!(f_lgamma_r(-4.5), (-2.813084081769316, -1)); |
| 76 |
assert_eq!(f_lgamma_r(-2.0015738), (5.759666328573806, -1)); |
| 77 |
} |
| 78 |
} |