I just encountered this and can\'t figure out why exactly Ruby behaves this way.
Could someone explain why in Ruby:
6.84 - 3.6 == 3.2399999999999998
0.01 or 0.07 cannot be precisely represented in binary floating-point.
Floating point uses an internal representation that's inherently imprecise. You should always round down your answers for display purposes:
'%.4f' % (6.84 - 3.6)
# => "3.2400"
Left to its own devices, Ruby, like many other languages, will express floating point numbers to a ridiculous degree of precision.
It's a binary floating-point problem.
read this
Base notation-2 and decimal base is used by double and float
Because double/float use base-2 notation and decimal base-10 notation.
Here's a useful link: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
See the answers in "Why is the 100*0.07 equal to 6.9999....?" (and many other places) about floating-point math.
Why is the 100*0.07 equal to 6.9999....?