Rounding off error in simple calculations

julia> 2.5/3

The result should have been reported 0.8333333333333333. An infinite series of 3s will never reach to 4 and will be a floor when rounded.

Is there any specific reason we have preferred rounding up in this case?



Note: I still use 0.6.1. My apologies if the behavior has been changed in a later release.


I think this is just a printing issue:

julia> log(2, rationalize(2.5/3; tol = eps()/8) - 5//6)


The rational number 5/6 is not representable as a binary float. The two nearest binary floats are

julia> 7505999378950826/big(2)^53

julia> 7505999378950827/big(2)^53

which reveals that the larger of them is actually closer to 5/6

julia> 7505999378950826/big(2)^53 - 5//6

julia> 7505999378950827/big(2)^53 - 5//6

so this is not a bug. It is the usual issue that humans like decimal numbers but computers prefer binary numbers. Each time a number is read or written there is a conversion happening between the two representations and this is an eternal source of confusion.


@andreasnoack Thanks. But never thought that 5/6 falls into that category. I think 0.1 does not have a true binary representation. The behavior now makes perfect sense to me.