-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Add gamma function to f32 and f64 #99747
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -635,6 +635,38 @@ fn test_atanh() { | |
assert_approx_eq!((-0.5f64).atanh(), -0.54930614433405484569762261846126285f64); | ||
} | ||
|
||
#[test] | ||
fn test_gamma() { | ||
// precision can differ between platforms | ||
assert_approx_eq!(1.0f64.gamma(), 1.0f64); | ||
assert_approx_eq!(2.0f64.gamma(), 1.0f64); | ||
assert_approx_eq!(3.0f64.gamma(), 2.0f64); | ||
assert_approx_eq!(4.0f64.gamma(), 6.0f64); | ||
assert_approx_eq!(5.0f64.gamma(), 24.0f64); | ||
|
||
assert_approx_eq!(0.5f64.gamma(), consts::PI.sqrt()); | ||
assert_approx_eq!((-0.5f64).gamma(), -2.0 * consts::PI.sqrt()); | ||
assert_eq!(0.0f64.gamma(), f64::INFINITY); | ||
assert_eq!((-0.0f64).gamma(), f64::NEG_INFINITY); | ||
assert!((-1.0f64).gamma().is_nan()); | ||
assert!((-2.0f64).gamma().is_nan()); | ||
assert!(f64::NAN.gamma().is_nan()); | ||
assert!(f64::NEG_INFINITY.gamma().is_nan()); | ||
assert_eq!(f64::INFINITY.gamma(), f64::INFINITY); | ||
assert_eq!(171.71f64.gamma(), f64::INFINITY); | ||
} | ||
|
||
#[test] | ||
fn test_ln_gamma() { | ||
assert_approx_eq!(1.0f64.ln_gamma().0, 0.0f64); | ||
assert_eq!(1.0f64.ln_gamma().1, 1); | ||
assert_approx_eq!(2.0f64.ln_gamma().0, 0.0f64); | ||
assert_eq!(2.0f64.ln_gamma().1, 1); | ||
assert_approx_eq!(3.0f64.ln_gamma().0, 2.0f64.ln()); | ||
assert_eq!(3.0f64.ln_gamma().1, 1); | ||
assert_approx_eq!((-0.5f64).ln_gamma().0, (2.0 * consts::PI.sqrt()).ln()); | ||
assert_eq!((-0.5f64).ln_gamma().1, -1); | ||
} | ||
|
||
#[test] | ||
fn test_real_consts() { | ||
use super::consts; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong, it returns the natural log of the absolute value of the gamma function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? am I just missing something, if so why does the final test case pass...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Γ(-0.5f64)
is negative, its natural log should be undefinedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
...well, it sounds obvious now that you've said it, so...!
#114754