0

This code snippet:

Math.pow(10,20)+20000 == Math.pow(10,20) + 10000

evaluates to true. Is this a JS bug?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
user997712
  • 161
  • 9

1 Answers1

8

No. Javascript uses floating point numbers to represent large values, and the value 1020 exceeds the precision of the floating point numbers. When you add a relatively tiny value such as 20000 or 10000 to the result, the resulting sums are indistinguishable.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 10^20 doesn't exceed the precision of FP numbers, what is at play here is the fact that floating point numbers are not evenly distributed (they become more and more sparse as their magnitude grows). – jmbr Sep 22 '13 at 14:12