2

In JavaScript, Number.MAX_VALUE represents the maximum representable numeric value (which is approximately 1.79E+308, a pretty big number)

However, if I evaluate (Number.MAX_VALUE - 1) < Number.MAX_VALUE in javascript console, it returns me false.

If i use multiplication, it works thought :

(Number.MAX_VALUE * 0.99999) < Number.MAX_VALUE returns true

Maybe I am missing something, but what is the possible explanation for this ?

tigrou
  • 4,236
  • 5
  • 33
  • 59

1 Answers1

2

The reason is that although the allowable number range is very large, the precision of a JS number (IEEE 754 double precision) is insufficient to exactly represent every possible number in that range. Doing so would require 308 digits!

Hence MAX_VALUE - 1 is indistinguishable from MAX_NUMBER.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • Does it means that if i would remove a very very small value from MAX_VALUE constant, it would work ? – tigrou Feb 06 '14 at 10:55
  • @tigrou it would need to be a value sufficiently large to change the 53 bit (~15 - 17 digit) mantissa of the floating point representation. It would actually be a very very large value - something like 1E+291! – Alnitak Feb 06 '14 at 10:58
  • Yes you are right it shoult rather be a large value. It seems to works with 1E+292. – tigrou Feb 06 '14 at 11:00
  • Indeed - `MAX_VALUE - 1E+291 === MAX_VALUE` but `MAX_VALUE - 1E+292` does not. – Alnitak Feb 06 '14 at 11:04