tigrou
tigrou

Reputation: 4516

In JavaScript, why ( Number.MAX_VALUE - 1) < Number.MAX_VALUE always evaluate to false?

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 ?

Upvotes: 2

Views: 703

Answers (1)

Alnitak
Alnitak

Reputation: 339917

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.

Upvotes: 2

Related Questions