-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Add gamma function to f32 and f64 #99747
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); | ||
Comment on lines
+641
to
+645
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...for what platform is this ever imprecise? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what guarantees different platforms provide around preciseness (or if it can change over time), but we can try with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like it fails on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My disappointment is immeasurable and my day is ruined. Well, okay. Can you add a comment to this effect and then squash down the first 6 commits before the one that includes the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated and squashed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem, thanks for helping move this along! |
||
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