I'm trying to increase by one this number: 9223372036854775808
:
var number = 9223372036854775808;
var plusOne = number + 1;
This should yield 9223372036854775809, but it instead yields 9223372036854776000.
Why? More important, how can I fix this?
The largest representable number in JavaScript is
(2^53) - 1
, or, written out,9007199254740991
. The number you have,9223372036854775808
, is more than 1024 times that quantity.If you want to work with numbers larger than the one above, you should use a big integer library. JavaScript does not have one built in, however, so you'll need to grab it yourself. Personally, I use big-integer when I'm working on things that deal with really large numbers, e.g., Project Euler.