Skip to content

Commit 12901f9

Browse files
day01YeungOnion
authored andcommitted
fix: Finish large beta edge handling
1 parent 8b5c844 commit 12901f9

5 files changed

Lines changed: 23 additions & 4 deletions

File tree

src/function/beta.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
//!
44
//! This module sets the default precision more tightly than crate defaults for `DEFAULT_EPS`
55
6-
mod double_double;
76
mod large_params;
87
mod temme;
98

10-
use crate::function::gamma;
9+
use crate::function::{double_double, gamma};
1110
use crate::prec;
1211
#[cfg(not(feature = "std"))]
1312
use num_traits::Float as _;
@@ -295,6 +294,14 @@ pub fn checked_beta_reg(a: f64, b: f64, x: f64) -> Result<f64, BetaFuncError> {
295294
return Ok(x);
296295
}
297296

297+
if b == 1.0 && a + b == a {
298+
return Ok((a * x.ln()).exp());
299+
}
300+
301+
if a == 1.0 && a + b == b {
302+
return Ok(-(b * (-x).ln_1p()).exp_m1());
303+
}
304+
298305
let log_prefactor = match large_params::log_prefactor(a, b, x) {
299306
Some(large_params::LogPrefactor::Value(logarithm)) => logarithm,
300307
Some(large_params::LogPrefactor::Underflow) => {
@@ -706,6 +713,16 @@ mod tests {
706713
assert_eq!(beta_reg(1e8, 2e8, 1.0), 1.0);
707714
}
708715

716+
#[test]
717+
fn test_beta_reg_extreme_shape_one() {
718+
assert_eq!(checked_beta_reg(1e308, 1.0, 0.0), Ok(0.0));
719+
assert_eq!(checked_beta_reg(1e308, 1.0, 0.5), Ok(0.0));
720+
assert_eq!(checked_beta_reg(1e308, 1.0, 1.0), Ok(1.0));
721+
assert_eq!(checked_beta_reg(1.0, 1e308, 0.0), Ok(0.0));
722+
assert_eq!(checked_beta_reg(1.0, 1e308, 0.5), Ok(1.0));
723+
assert_eq!(checked_beta_reg(1.0, 1e308, 1.0), Ok(1.0));
724+
}
725+
709726
#[test]
710727
fn test_beta_reg_next_down_from_one_is_not_a_boundary() {
711728
let x = f64::from_bits(1.0_f64.to_bits() - 1);

src/function/beta/large_params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::double_double::{accurate_ln_one_plus, add, divide, multiply, two_sum};
21
use crate::consts;
2+
use crate::function::double_double::{accurate_ln_one_plus, add, divide, multiply, two_sum};
33
#[cfg(not(feature = "std"))]
44
use num_traits::Float as _;
55

src/function/beta/temme.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use super::double_double::{add, divide, exp, multiply, two_sum};
21
use super::large_params::log_ratio;
32
use crate::consts;
3+
use crate::function::double_double::{add, divide, exp, multiply, two_sum};
44
use crate::function::erf;
55
#[cfg(not(feature = "std"))]
66
use num_traits::Float as _;

src/function/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Provides a host of special statistical functions (e.g. the beta function or
22
//! the error function)
33
4+
mod double_double;
5+
46
pub mod beta;
57
pub mod erf;
68
pub mod evaluate;

0 commit comments

Comments
 (0)