2

Why 100 == 99.999 is true, however 100 == 99.99 is false in Javascript?

airbai
  • 3,906
  • 5
  • 26
  • 30

3 Answers3

4

What Every Computer Scientist Should Know About Floating Point Arithmetic

and the shorter, more to the point:

http://floating-point-gui.de/

Marco Mariani
  • 13,556
  • 6
  • 39
  • 55
3

Where are you getting that result?

From Firebug console in Firefox 3.6.3

>>> 100==99.99
false
>>> 100==99.999
false
>>> 100==99.9999
false
>>> 100==99.99999
false
>>> 100==99.999999
false
>>> 100==99.9999999
false
>>> 100==99.99999999
false
>>> 100==99.999999999
false
>>> 100==99.9999999999
false
>>> 100==99.99999999999
false
>>> 100==99.999999999999
false
>>> 100==99.9999999999999
false
>>> 100==99.99999999999999
false
>>> 100==99.999999999999999
true
harpo
  • 41,820
  • 13
  • 96
  • 131
-3

Why is 100 == 99.999 true in Javascript?

It's not; whatever Javascript implementation you are using is buggy.

however 100 == 99.99 is false in Javascript?

Because they're not equal.

Stephen Canon
  • 103,815
  • 19
  • 183
  • 269