Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Remove panics from tgamma and tgammaf #264

Merged
merged 1 commit into from
Jul 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/math/tgamma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn sinpi(mut x: f64) -> f64 {

/* reduce x into [-.25,.25] */
n = (4.0 * x) as isize;
n = (n + 1) / 2;
n = div!(n + 1, 2);
x -= (n as f64) * 0.5;

x *= PI;
Expand Down Expand Up @@ -118,18 +118,19 @@ fn s(x: f64) -> f64 {
/* to avoid overflow handle large x differently */
if x < 8.0 {
for i in (0..=N).rev() {
num = num * x + SNUM[i];
den = den * x + SDEN[i];
num = num * x + i!(SNUM, i);
den = den * x + i!(SDEN, i);
}
} else {
for i in 0..=N {
num = num / x + SNUM[i];
den = den / x + SDEN[i];
num = num / x + i!(SNUM, i);
den = den / x + i!(SDEN, i);
}
}
return num / den;
}

#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn tgamma(mut x: f64) -> f64 {
let u: u64 = x.to_bits();
let absx: f64;
Expand Down Expand Up @@ -157,7 +158,7 @@ pub fn tgamma(mut x: f64) -> f64 {
return 0.0 / 0.0;
}
if x <= FACT.len() as f64 {
return FACT[(x as usize) - 1];
return i!(FACT, (x as usize) - 1);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/math/tgammaf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::tgamma;

#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn tgammaf(x: f32) -> f32 {
tgamma(x as f64) as f32
}