-
Notifications
You must be signed in to change notification settings - Fork 119
tgamma overflows with half-integer arguments #150
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
Comments
I'm now convinced this is a bug in g++, not in the lib code. Sorry for the noise.
Works correctly (if the template argument's provided explicitly). Without much investigation, my best guess is that overload resolution with implicit conversion (no idea why) overrides template instantiation or even more likely that overloading rules aren't strictly followed. |
No this is a bug related to the limited exponent range of the number type. What's happening inside tgamma(z) is:
This works fine as long as exponent range and precision increase in step (at least somewhat), but fails catastrophically in this case because we have a high precision (and hence need a largish value for N), but the low exponent range means that tgamma(z+N) always overflows :( I'm not sure how to fix this at present... |
Indeed, you're right. I've hit it again and went through the stack a bit. It seems I'm triggering the
check causing the overflow. As a workaround I can increase the exponent size at least for this one (i.e. define a separate type to do the calculation). This code path is neither time nor efficiency critical. Thanks for looking into it and for the information! |
I am going through the multiprecision issues and find myself interested in this one. Let's limit this discussion at first to real argument greater than zero. @jzmaddock: Do you know if this is issue related to the argument's nearness to a half-integer, or it solely related overflow problems regarding small exponent range, regardless of the argument? |
Purely exponent size - as the digit count increases so minimum_argument_for_bernoulli_recursion increases - eventually to the point where tgamma at that point is infinite if the exponent range is small. |
Hi @jzmaddock, I was revisiting integer sequences such as A001163 and A001164, which basically list hundreds of coefficients for Stirling's approximation to the Gamma function. These could provide uniform asymptotic series expansion (though i believe ultimately divergent) for many digits of I could look into these possibilities, albeit slowly..., for converging methods in the numerical limits range pertinent for this issue. What do you think? |
Sometimes I extend the coefficients in A&S 6.1.34 for
But I have not yet found a really good high-precision method for |
I don't think we can extend the range of the coefficients - I believe the series become divergent rather promptly unfortunately. We could compute lgamma to avoid the initial overflow, but then subtracting values would lead to horrible cancellation error and a meaningless result. Something I did a lot in the hypergeometric functions - though it's a ton of work - is to rescale everything, so for example instead of computing n^n we could compute n^(n/2) and then rescale by that factor once the current result becomes small enough... but dividing by 2 might not be enough and we might have to rescale multiple times :( I wonder though: is this an issue at all once the exponent range matches that of |
I was on the wrong track with that research direction. My resources were actually for the incomplete Gamma function for large parameter a. I did, however find an accelerated Stirling's approximation for
Yes. In these (predicted) cases, we could also concentrate on computing the real-valued Gamma function of argument |
I hate to put you down, but while intuitive this is more often than not a dubious approach. LSE is excellent in very many cases, but not here. The least squares guarantee that the error is bound on average, but nothing can be said about the maximum error in the interval, and this is a problem ... typically for function evaluations one wants to minimize the maximum error[1]. |
Thank you for this critical information. I'll integrate those thoughts in future approach(es). |
Nod. And we do have Minimax code in Boost Math we can use, whether it would scale to 100's of digits though is another matter.... often it's a challenge to get up to quad precision. |
Off-topic: How come? At least for the exponent I haven't had a problem with convergence up to quads (I'd started it from the taylor expansion). I think, but don't hold me to it, it's sensitive to the monotonicity of the function in the interval, but that can be chosen appropriately (at least in this case). |
The following MRE illustrates that tgamma overflows (i.e. returns infinity) when half-integers are used:
Produces 3.32335097044784255117 for
r
, but infinity forrr
. In contrast:Produces the correct value of 2.
(g++ 8.3.0 on Linux,
long double
is 80bit extended)The text was updated successfully, but these errors were encountered: