Possible Duplicate:
Is JavaScript’s Math broken?
I noticed a weird behavior when I try to run 4.08-1.36 in the Chrome console or Firebug.
The console returns 2.7199999999999998 as a result when the result should be 2.72.
I was wondering how is this possible. Any ideas?
Computers store numbers in binary instead of decimal. Just as you cannot represent the fraction 1/3 exactly in decimal form, you cannot represent the fraction 1/10 exactly in binary, which means there are many values that can be represented exactly in decimal form but not in binary form.
All of the numbers in your example fall into this category. In binary, the decimal number 4.08 becomes 100.00010100011110101110…, where those digits after the binary point repeat forever. The other value, 1.36, becomes 1.01011100001010001111…., and their difference, 2.72, becomes 10.10111000010100011110… in each case, also with an infinitely-repeating binary fraction.
When converting back to decimal for human viewing, the computer rounds to the closest possible match, which isn’t always the exact value intended.