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
spec: Different results when using float64 constant vs float64(int) cast #23876
Comments
Precision, not optimisation is the culprit:
|
After some good discussions on Slack (thanks everyone), this may be the correct albeit unexpected (to me and to a casual reader of the code): https://www.ardanlabs.com/blog/2014/04/introduction-to-numeric-constants-in-go.html
@cznic, you are correct in that its precision but the issues is deeper than just the typical issues of floating point precision. it's related, it seems, to "[in] your issue all the computations are done at the "ideal constant" level with effectively unlimited precision, and then afterwards converted down to a float64" (credit @dgryski with that phrasing). I'm left wondering about all the times I got this wrong. A typical use case for me would be using float64 constants in variable declarations à la: x := Foo {
Mean: (x0 + x1 + x3 + x4) / 4.0,
} From my new understanding, that is not the same as cnt := float64(4)
x := Foo {
Mean: (x0 + x1 + x3 + x4) / cnt,
} |
In this case, there are no other calculations to be done with the ideal |
yeah, it was a bad example case as, in this case, the answers are the same. But introducing more complex math, like the original post, shows how that assumptions in that last example break down. Just one more thing to keep in mind. Your explanation about the "ideal |
Sounds like there's nothing to do here. Closing. |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go version go1.9.4 darwin/amd64
Does this issue reproduce with the latest release?
yes. I can replicated this on play.golang.org
What operating system and processor architecture are you using (
go env
)?GOARCH="amd64"
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
What did you do?
https://play.golang.org/p/o3I21mRFPt2
What did you expect to see?
All cases should be equal
What did you see instead?
Case A is different. When using 4.0 vs float64(4) directly, I get different results (Case A vs Case B). If I assign the values to variables then I get the same result (Case C and D are the same result as Case B). There appears to be some optimisation that is affecting the 4.0 constant.
The text was updated successfully, but these errors were encountered: