-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
bug: Float Math Around 1.0 #53297
Comments
This is how floating-point numbers work. |
I should mention - this evaluates to 0 on Intel based machines |
That would be differences due to FMA. See #48145 for another example. |
This phenomenon happens even on amd64 https://go.dev/play/p/RF9ZzmLZusw |
Thanks for taking the time to look into this, I appreciate it :) I get that floats do weird things with rounding when numbers don't exactly fit into float representation and so you get drift - that's fine and expected. Floats gon' float. I was surprised that compiling and running on Intel vs ARM produced different numeric results (which is why I opened this issue...even tho I forgot to mention that originally in the description, my bad). I don't necessary care which one is "right", we can find all sorts of weird float combinations that should evaluate neatly but don't. But my expectation is that regardless of target architecture, the code would execute to the same result. We have devs using both Intel and ARM for dev, so if one dev writes a test/code that uses FMADD it may end up failing a test on the other's machine. Which is less that ideal. In this particular case, we had a test which had passed for years and only recently began to fail once devs started to use ARM for dev. FWIW I was able to get this to avoid the issue with FMADD by doing the following: dividend := float64(1.0)
divisor := float64(0.1)
log.Println(dividend - (divisor*math.Floor(dividend/divisor)+0.0)) |
This was exposed while testing CockroachDB. Following tests from pkg/sql/opt/exec/execbuilder fail due to floating point precision differences caused by FMA: - TestExecBuild/local/geospatial - TestExecBuild/local-vec-off/geospatial - TestExecBuild/local-spec-planning/geospatial - TestExecBuild/fakedist/geospatial - TestExecBuild/fakedist-vec-off/geospatial - TestExecBuild/fakedist-metadata/geospatial - TestExecBuild/fakedist-disk/geospatial - TestExecBuild/fakedist-spec-planning/geospatial With explicit casts in this patch, these failures are resolved. References: https://go.dev/ref/spec#Floating_point_operators golang/go#53297 golang/go#48145 disintegration/gift#20 (comment)
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Calculated float64(1.0) % float64(0.1)
What did you expect to see?
Should evaluate to
0
What did you see instead?
Evaluates to
-5.551115123125783e-17
The text was updated successfully, but these errors were encountered: