-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Tracking Issue for float_gamma
#99842
Comments
A few comments. First: we should definitely elaborate on docs; cmath has a few guarantees for gamma that I think we can strengthen. I think it would be good to at least link the Wikipedia page for the gamma function, but we could also potentially add that for integers n, this is equivalent to (n-1)!, so that at a glance people understand the purpose of it. Taking a look at the requirements C gives: https://en.cppreference.com/w/c/numeric/math/tgamma
Based upon this, I would say that the best approach is to go with IEEE's definition and just return:
My gut tells me that adding these checks won't be an issue for performance since gamma will not perform great either way, but that could probably be an open question for later. Note that the biggest difference from the standard C implementation is that the C definition allows ± |
Thanks for the feedback! I added more tests to show the current behavior. In the "Error handling" section of the link above, it mentions if the implementation supports IEEE floating-point arithmetic, the behavior is:
Is the proposal in point 1 to change the behavior of ±0 and +∞ to return NaN? |
So, you didn't include negative non-integers, but I'm assuming you also meant those return NaN. My proposal would specify explicitly that negative integers return NaN (I believe it permits ±∞ by the C spec) and +∞ can stay +∞. We can keep 0 as +∞, I think. |
Thanks for the clarification. The gamma function is defined for negative non-integers, so I don't think we'd want to change that behavior. Agree it'd be good for negative integers to be consistent across platforms. If there are any platforms that return ±∞, we could probably use conditional compilation to return NaN. |
Sorry, I somehow got into my head that they'd have complex values here when that's not the case. Yes, they should be still defined there. |
Great. Another question is: should we add a |
@ankane Since both |
Thanks @scottmcm, updated the tracking issue and implementation to include this (but need to figure out how to make it work with |
The documentation of these functions could be greatly improved. Why does |
Is it possible that the approximation is really bad? I would expect this test to pass; Wolfram Alpha confirms a value close to -3.5449078. And yet we get -1.7724538? Interestingly, |
Looking at this page, it appears that it's the usual C shenanigans of things being represented very poorly. Since the gamma could be positive or negative, the logarithm is of the absolute value of gamma. However, since this would technically lose the sign information computed for the gamma function, the sign is returned as a separate integer argument. Personally, I would say that we should make |
Also if it's just a sign maybe it should be a special |
There's https://doc.rust-lang.org/std/primitive.i16.html#method.signum, though arguably that should be returning What sign gets returned here (along with a NAN) for |
I'd suggest naming the Edit: I thought of another reason why it makes sense for the sign to be a float. When generalizing the function to a complex argument type, the returned sign would be a complex number with unit magnitude. I realize that the standard library doesn't provide complex numbers, but it is worth considering how the API would generalize to complex numbers, to try to make the types consistent. Both real and complex numbers could have a |
This is just a parsing issue: -0.5.gamma() is being interpreted as -(0.5.gamma()) not as (-0.5).gamma() |
Oh, d'oh. Thanks! That's a strange parsing precedence... I opened #117155 to track that. |
FWIW, tgamma has been explicitly mentioned in #26350 as something we probably do not want to have in the standard library. |
Feature gate:
#![feature(float_gamma)]
This is a tracking issue for adding the gamma and log-gamma functions to
f32
andf64
(tgammaf
,tgamma
,lgammaf_r
, andlgamma_f
from C).Refs:
Public API
Steps / History
Unresolved Questions
Footnotes
https://std-dev-guide.rust-lang.org/feature-lifecycle/stabilization.html ↩
The text was updated successfully, but these errors were encountered: