Reputation: 171341
I work in Javascript with integer numbers only (mainly adding numbers and shifting them). I wonder how big they can be without loosing any bits.
For example, how big X
can be such that 1 << X
will represent 2^X
?
Upvotes: 36
Views: 32740
Reputation: 4257
All answers are partially wrong - Maybe due the new ES6/ES7 specs - , read why:
First of all, in JavaScript, the representation of the number is 2^53 - 1 that is true for @Luke answer,
we can prove that by running Number.MAX_SAFE_INTEGER
that will show a big number, then we do log2
to confirm that the number of bits is the same :
Number.MAX_SAFE_INTEGER
// 9007199254740991
Math.log2(9007199254740991)
// 53
Welcome to Javascript!
Upvotes: 17
Reputation: 269368
All numbers in JavaScript are actually IEEE-754 compliant floating-point doubles. These have a 53-bit mantissa which should mean that any integer value with a magnitude of approximately 9 quadrillion or less -- more specifically, 9,007,199,254,740,991 -- will be represented accurately.
NOTICE: in 2018 main browsers and NodeJS are working also with the new Javascript's primitive-type, BigInt, solving the problems with integer value magnitude.
Upvotes: 42