-
Notifications
You must be signed in to change notification settings - Fork 17.5k
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() results are less accurate than libc pow() #25270
Comments
is this "by design"? or something that would be addressed in the future? |
As rsc wrote in the old golang-dev thread about this:
|
Please add the details with specific examples in this issue, rather than making people sort through a lengthy mailing list thread. Thanks. |
From the referenced thread, the example given is that:
where the math.Pow result is low compared to the correctly rounded result from arbitrary precision calculation using math/big, which the c++ stdlib produces. The raw binary representations to see the error in the last places clearly:
In this calculation with integer exponent, the error is solely due to floating point error from repeated multiplications. The only way that I know to improve this is to use higher precision for the internal calculations, with associated performance and complexity impacts. Note that the correctly rounded result for this case can be obtained by using 64 bits of precision, i.e. 80 bit extended precision floats aka long double (these are available in x87 FPU). I'm not sure what internal precision the c++ stdlib is using, but pow is known for having more difficulty obtaining accurate results than other elementary functions. Playground summarizing the calculation is here. |
Please see detailed discussion on the topic here: https://groups.google.com/forum/#!topic/golang-nuts/LqVD5kMHJQw
Note that the headline is about Fibonacci numbers, however, the root issue is regarding the accuracy of math.Pow, compare to libc pow() function.
What version of Go are you using (
go version
)?go version go1.9.5 linux/amd64
Does this issue reproduce with the latest release?
did not test with go1.10, but could not find anything related to math performance in release notes
What operating system and processor architecture are you using (
go env
)?What did you do?
compare accuracy of math.Pow tolibc pow(). cod, as well as detailed results are in the go-nuts discussion above
What did you expect to see?
see group discussion above
What did you see instead?
see group discussion above
The text was updated successfully, but these errors were encountered: