Remove panics from tgamma and tgammaf

This commit is contained in:
Andrew Kane
2022-07-29 16:52:30 -07:00
parent 1f7b8eb61c
commit 33ccb28a97
2 changed files with 8 additions and 6 deletions
+7 -6
View File
@@ -38,7 +38,7 @@ fn sinpi(mut x: f64) -> f64 {
/* reduce x into [-.25,.25] */ /* reduce x into [-.25,.25] */
n = (4.0 * x) as isize; n = (4.0 * x) as isize;
n = (n + 1) / 2; n = div!(n + 1, 2);
x -= (n as f64) * 0.5; x -= (n as f64) * 0.5;
x *= PI; x *= PI;
@@ -118,18 +118,19 @@ fn s(x: f64) -> f64 {
/* to avoid overflow handle large x differently */ /* to avoid overflow handle large x differently */
if x < 8.0 { if x < 8.0 {
for i in (0..=N).rev() { for i in (0..=N).rev() {
num = num * x + SNUM[i]; num = num * x + i!(SNUM, i);
den = den * x + SDEN[i]; den = den * x + i!(SDEN, i);
} }
} else { } else {
for i in 0..=N { for i in 0..=N {
num = num / x + SNUM[i]; num = num / x + i!(SNUM, i);
den = den / x + SDEN[i]; den = den / x + i!(SDEN, i);
} }
} }
return num / den; return num / den;
} }
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn tgamma(mut x: f64) -> f64 { pub fn tgamma(mut x: f64) -> f64 {
let u: u64 = x.to_bits(); let u: u64 = x.to_bits();
let absx: f64; let absx: f64;
@@ -157,7 +158,7 @@ pub fn tgamma(mut x: f64) -> f64 {
return 0.0 / 0.0; return 0.0 / 0.0;
} }
if x <= FACT.len() as f64 { if x <= FACT.len() as f64 {
return FACT[(x as usize) - 1]; return i!(FACT, (x as usize) - 1);
} }
} }
+1
View File
@@ -1,5 +1,6 @@
use super::tgamma; use super::tgamma;
#[cfg_attr(all(test, assert_no_panic), no_panic::no_panic)]
pub fn tgammaf(x: f32) -> f32 { pub fn tgammaf(x: f32) -> f32 {
tgamma(x as f64) as f32 tgamma(x as f64) as f32
} }