0

Possible Duplicate:
Is JavaScript’s math broken?

If the main difference between Javascript's "strict comparison" operators and the traditional ones is type coercion, why does

0.1+0.2===0.3;

return false?

Community
  • 1
  • 1
Max Vu
  • 464
  • 2
  • 10

2 Answers2

3

In Firefox and Chrome, 0.1 + 0.2 is 0.30000000000000004, which is not equal to 0.3. This is presumably caused by it not being possible to represent 0.1 exactly as a floating point number.

Phil Ross
  • 25,590
  • 9
  • 67
  • 77
0

because 0.1 is tricky
it's irrational recurring in binary (see comments)

EDIT
actually I'm winging it here, I don't know that for sure. Is there a Math.boffin out there who can disprove or provide a proof?
would be very interesting

meouw
  • 41,754
  • 10
  • 52
  • 69
  • "Irrational" isn't true - 1 / 10 is a rational number regardless of what base you're storing things in. However, it *is* a recurring binary, so it's not possible to represent it exactly using a normal floating-point representation. – Anon. Feb 16 '10 at 00:37
  • 0.1 in decimal is 0.00011001100110011... recurring in binary. – Phil Ross Feb 16 '10 at 00:40
  • rational being representable as a fraction - silly me. Could you shed some light on why it is recurring? – meouw Feb 16 '10 at 00:41
  • 1
    in binary, each digit past the dot represents 1/2, 1/4, 1/8, 1/16, just as in decimal it is 1/10, 1/100, 1/1000. Using just these binary fractions, you can't ever add up to 1/10 exactly. The closest you can get is 1/16 + 1/32 + 1/256 + 1/512 + .... (0.0001100110011...) – nickf Feb 16 '10 at 00:46
  • IEEE standards (754) dictate fractional numbers be stored this way. – Max Vu Feb 17 '10 at 18:48