Rounding off error in simple calculations

julia> 2.5/3
0.8333333333333334

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?

regards,

Sambit

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)
-53.77760757866355

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

julia> 7505999378950826/big(2)^53
8.333333333333332593184650249895639717578887939453125000000000000000000000000000e-01

julia> 7505999378950827/big(2)^53
8.333333333333333703407674875052180141210556030273437500000000000000000000000000e-01

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

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

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

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.

3 Likes

@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.