If you run the following simple code in Go playground:
f1 := 256.1
fmt.Println((f1 * 100 - f1 * 10 * 10)*10e15)
You will get 36379.78 instead of 0.
multiplication should not have any rounding errors.
Would appreciate your thoughts on this issue
Multiplication with floats is not an exact operation and will have rounding errors. You can check that even
f1*100 == f1*10*10 is
This is a feature of binary floating point arithmetic in every language and every platform I have worked on, and I have worked on a lot! This is why you can’t write accounting systems using C or Go floating point: the accountants get annoyed when the cross-footing is off by a penny for no good reason.
You are right. It seems to be a precision bug created by not definition of the float32 or float64.
We have to use this method to define float variables :
var f1 float32
Since nobody else has dropped a link, I was going to link to a long article with lots of formulas… but https://floating-point-gui.de/ looks to be much more accessible, and still links to the in-depth article. A very important read for anyone who expects floating point to be exact
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.