Why do simple arithmetics cause weird Precision bugs

In short, 295.8 is not the number you think it is. It’s actually 295.80000000000001136868377216160297393798828125:

julia> using Printf; @printf "%.60f" 295.8
295.800000000000011368683772161602973937988281250000000000000000

295.8 is used to input (and output) that number because there’s no other floating point number closer to the exact 295.8 than that one. So then when you do maths with that number, you can get further roundings away from what you’d otherwise expect.

To make this even more tangible, you can see that if you subtract 295 from the above true value, the answer must be 0.80000000000001136868377216160297393798828125, or the nearest floating point number that rounds to it:

julia> 295.8-295
0.8000000000000114
2 Likes