-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
math: Pow(x,y) fails for large values of y #7394
Comments
I was looking for a simple Go 1.3 issue to help out with the release effort, so I thought I'd start with this one. It didn't look like anyone had investigated it yet. I've reproduced the problem on GOARCH="386" (Linux), but it seems to work correctly on GOARCH=amd64 (Windows). I think that explains why it fails on the Playground, but not why the original submitter saw the problem on linux/amd64. I don't have suggested fix yet, but I've tracked it down to the use of 'int' (32 bits on '386, 64 bits on amd64), which overflows an int variable in the math.Pow code at some point after the call to math.Frexp. At the end of math.Pow it is passed as a negative int value to math.Ldexp, which at line #29, after normalization of the mantissa, executes: return Copysign(0, frac) // underflow I'll look at it some more tonight. I think there might be a simple fix in math.Pow that avoids the call to math.Ldexp in that case, but I haven't implemented it or run any tests yet. |
Owner changed to @griesemer. |
CL https://golang.org/cl/48290 mentions this issue. |
The loop that accumulates the exponent ae by effectively multiplying the 11-bit exponent of x (xe) with yi (int64) overflows both 32 and 64-bit int since the resulting value could be up to 74-bits. The code computes the multiplication by summing shifts of xe so the CL above checks for overflow of the output float64 exponent (11 bits plus denormals) by monitoring the magnitude of xe and exits the loop early. Additionally, for yi >= 1<<63 the code was using Exp( y * Log(x)) which gave Nan incorrectly for x<0 and 0/Inf otherwise. The CL drops the call to Exp and replaces it with a case statement since for yi that large Pow always under/overflows and handles x<0 correctly. |
by nicolas.riesch:
The text was updated successfully, but these errors were encountered: