BigInt Arbitrary precision integers in JavaScript ( https://github.com/tc39/proposal-bigint )

Author

Motivation: Why Do We Need Such Big Numbers ?

There are a number of cases in JavaScript coding where integers larger than 253 come up — both instances where signed or unsigned 64-bit integers are needed and times where we may want integers even larger than 64-bits.

64-bit Use Cases

Often, other systems with which Javascript interacts provides data as 64-bit integers, which lose precision when coerced to Javascript Numbers.

These might come when reading certain machine registers or wire protocols or using protobufs or JSON documents that have GUIDs generated by 64-bit systems in them — including things like credit card or account numbers — which currently must remain strings in Javascript.

(Note, however, BigInts cannot be serialized to JSON directly. But you can use libraries like granola to serialize and deserialize BigInt and other JS datatypes to JSON.)

In node, fs.stat may give some data as 64-bit integers, which has caused issues already:

fs.lstatSync('one.gif').ino
// ↪ 9851624185071828

fs.lstatSync('two.gif').ino
// ↪ 9851624185071828, duplicate, but different file!

Finally, 64-bit integers enable higher resolution — nanosecond! — timestamps.

These will be put to use in the temporal proposal, currently in Stage 1.

State of the Proposal

This proposal is currently in Stage 4.

BigInt has been shipped in Chrome, Node, Firefox, and is underway in Safari.

  • V8 by Georg Neis and Jakob Kummerow.

  • JSC by Caio Lima and Robin Morisset.

  • SpiderMonkey by Robin Templeton and Andy Wingo.

Description

BigInt is a built-in object that provides a way to represent whole numbers larger than 253 - 1, which is the largest number JavaScript can reliably represent with the Number primitive and represented by the Number.MAX_SAFE_INTEGER constant.

BigInt can be used for arbitrarily large integers.

BigInt is similar to Number in some ways, but also differs in a few key matters — it cannot be used with methods in the built-in Math object and cannot be mixed with instances of Number in operations; they must be coerced to the same type.

Be careful coercing values back and forth, however, as the precision of a BigInt may be lost when it is coerced to a Number.

Description

A BigInt is created by appending n to the end of an integer literal or by calling the function BigInt().

const theBiggestInt = 9007199254740991n
const alsoHuge = BigInt(9007199254740991)

Who needs these numbers ?

You might be surprised to hear that it’s quite common to have numbers this large in software development.

Timestamps and unique identifiers can be numbers this large.

For example, Twitter use integers this large as unique keys for tweets. You would see weird bugs in your JavaScript application if you tried to store these as numbers without BigInt.

You would have to use a community package, or store them as a string instead - which is a common solution that JavaScript developers were using to solve this problem in environments where BigInt isn’t supported.

Operators

You can use +, , -, * and % with BigInts, just like with Numbers.

const previousMaxSafe = BigInt(Number.MAX_SAFE_INTEGER);
// ↪ 9007199254740991

const maxPlusOne = previousMaxSafe + 1n;
// ↪ 9007199254740992n

const theFuture = previousMaxSafe + 2n;
// ↪ 9007199254740993n, this works now!

const multi = previousMaxSafe * 2n;
// ↪ 18014398509481982n

const subtr = multi – 10n;
// ↪ 18014398509481972n

const mod = multi % 10n;
// ↪ 2n

const bigN = 2n ** 54n;
// ↪ 18014398509481984n

bigN * -1n
// ↪ –18014398509481984n

Other API Notes

BigInts may also be used in BigInt64Array and BigUint64Array typed arrays for 64-bit integers.

 1 const view = new BigInt64Array(4);
 2 // ↪ [0n, 0n, 0n, 0n]
 3 view.length;
 4 // ↪ 4
 5 view[0];
 6 // ↪ 0n
 7 view[0] = 42n;
 8 view[0];
 9 // ↪ 42n
10
11 // Highest possible BigInt value that can be represented as a
12 // signed 64-bit integer.
13 const max = 2n ** (64n - 1n) - 1n;
14 view[0] = max;
15 view[0];
16 // ↪ 9_223_372_036_854_775_807n
17 view[0] = max + 1n;
18 view[0];
19 // ↪ -9_223_372_036_854_775_808n
20 //   ^ negative because of overflow

Example: Calculating Primes

 1 function isPrime(p) {
 2   for (let i = 2n; i * i <= p; i++) {
 3     if (p % i === 0n) return false;
 4   }
 5   return true;
 6 }
 7
 8 // Takes a BigInt as an argument and returns a BigInt
 9 function nthPrime(nth) {
10   let maybePrime = 2n;
11   let prime = 0n;
12
13   while (nth >= 0n) {
14     if (isPrime(maybePrime)) {
15       nth -= 1n;
16       prime = maybePrime;
17     }
18     maybePrime += 1n;
19   }
20
21   return prime;
22 }

BigInt ( https://medium.com/javascript-in-plain-english/new-features-in-es2020-you-should-check-b4974d9d7edc )

When you had to add two numbers that are too big enough to cause an overflow, weren’t you suffered?

Number.MAX_VALUE * 2 // Infinity

BigInt is a savior in this case.

You can make a BigInt by calling BigInt() with parenthesis or 2n with n at the end of a number.

const num = 2;
const bigNum = BigInt(num);

bigNum; // 2n
bigNum === 2n; // true

You can also add, subtract, multiply and divide it.

const bigN = BigInt(10);
bigN + bigN; // 20n
bigN * 3n; // 30n
bigN - BigInt('55'); // 45n
bigN / 3n; // 3n

Note that bigN / 3n returns 3n , not 3.33333n .

Because as you also can assume from its name, it only handles the integers.

So bigN / 3n is similar to Math.floor(10 / 3) .

However, unfortunately, you can’t make a BigInt with a float number.

And also, you can’t use a BigInt and a Number together, either.

BigInt(3.3);
// Uncaught RangeError BigInt('3.3');
// Uncaught SyntaxErrorBigInt(1) + 1;
// Uncaught TypeError
// Cannot mix BigInt and other types

Slides

Articles

https://www.freecodecamp.org/

BigInt , one of the most anticipated features in JavaScript, is finally here.

It actually allows developers to have much greater integer representation in their JS code for data processing for data handling.

At the moment the maximum number you can store as an integer in JavaScript is pow(2, 53) - 1 . But BigInt actually allows you to go even beyond that.

However, you need to have an n appended at the very end of the number, as you can see above.

This n denotes that this is a BigInt and should be treated differently by the JavaScript engine (by the v8 engine or whatever engine it is using).

This improvement is not backwards compatible because the traditional number system is IEEE754 (which just cannot support numbers of this size).

https://2ality.com

https://github.com/MikeMcl/big.js