-2

How to increase this number(you can try it on the browser console):

36893488147419103000 + 1

The result of this is:

36893488147419103000

The number stays the same no changes to it why is that? and how can I increase it by 1?

Blurry Script
  • 329
  • 1
  • 3
  • 11

3 Answers3

3

For big integers you should use the BigInt (Big Integer) type.

Note 1: you almost always cannot mix BigInt numbers with Numbers (eg for math operations) without first performing an explicit conversion.

Note 2: JSON does not currently natively support BigInt values. As a workaround you can use strings (eg. '1n' for the values and then use a reviver function when calling JSON.parse.

JavaScript currently only has two numeric types: double-precision IEEE 754 floating point Numbers, and Big Integers which can be used to represent arbitrarily large integers. You can declare a BigInt number literal using the suffix n, eg. 1n.

IEEE 754 Numbers can "only" accurately represent integers up to and including Number.MAX_SAFE_INTEGER, which has a value of 2^53 - 1 or 9,007,199,254,740,991 or ~9 quadrillion.

From MDN:

Double precision floating point format only has 52 bits to represent the mantissa, so it can only safely represent integers between -(253 – 1) and 253 – 1. "Safe" in this context refers to the ability to represent integers exactly and to compare them correctly. For example, Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 will evaluate to true, which is mathematically incorrect. See Number.isSafeInteger() for more information.

A "Decimal" number type, that will be able to represent arbitrarily precise decimal numbers, is under development.

Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • oh fantastic solution nice – Blurry Script Oct 31 '22 at 17:34
  • 2
    Note for OP: If the value arrives via JSON, user input, or other means, you should make sure it is **never** converted into a number. It should be passed as a string to BigInt `BigInt(numberAsString)` to prevent the loss of information. An example is [Twitter IDs](https://developer.twitter.com/en/docs/twitter-ids) which are returned as both number and string. When using JavaScript you should always use the string to create a BigInt, never use the number. – 3limin4t0r Oct 31 '22 at 17:45
0

Double precision floating point format only has 52 bits to represent the mantissa, so it can only safely represent integers between -(253 – 1) and 253 – 1. See Number.MAX_SAFE_INTEGER. Larger numbers may not be able to be represented exactly.

Doug Domeny
  • 4,410
  • 2
  • 33
  • 49
0

Obviously the number is internally represented as a floating point number.When the value you want do add to this number is less then the value of the least significant bit, it will not change the the value. The only way would be to use floating point numbers with a higher resolution i.e. with a higher number of significant bits.

Elec1
  • 235
  • 2
  • 8