BigInteger.js: Arbitrary-Length Integers
BigInteger.js is a robust JavaScript library for performing arithmetic operations on integers of unlimited size, circumventing JavaScript's standard number precision limitations. The current stable version is 1.6.52. While releases are not on a fixed schedule, the project is actively maintained, with recent updates addressing various fixes and TypeScript definitions. A key differentiator and recent major change is its evolution into a polyfill for the native JavaScript `BigInt` feature (introduced to TC39 in 2018). If the execution environment supports native `BigInt`, this library will transparently wrap the native implementation, otherwise, it provides its own software-based arbitrary-length integer solution. This makes it a highly compatible choice for environments with varying `BigInt` support.
Common errors
-
Error: Invalid integer
cause Attempting to create a `bigInt` from a string or number that cannot be parsed as a valid integer, or providing a base that is not a valid `bigInt`.fixEnsure that string inputs only contain valid digits for the specified base and that the base parameter itself is a valid integer. For example, `bigInt('hello')` will throw this error, as will `bigInt('10', 0)`. -
TypeError: Cannot mix BigInt and other types, use explicit conversions
cause This error occurs when trying to perform operations between a native `BigInt` (which `big-integer` might return as a polyfill) and a standard JavaScript `Number` or other type, without explicit conversion.fixWhen `big-integer` uses native `BigInt` as a polyfill, all operands in an arithmetic operation must be `BigInt`s. Convert standard numbers to `BigInt` using `BigInt(number)` or `bigInt(number)` before operations, e.g., `bigInt('100').plus(bigInt(5))` instead of `bigInt('100').plus(5)`.
Warnings
- breaking Starting with v1.6.37, `big-integer` acts as a polyfill for the native JavaScript `BigInt`. If the environment supports native `BigInt`, the library internally uses and often returns native `BigInt` instances. This can lead to unexpected behavior when using `instanceof` checks (e.g., `value instanceof bigInt.BigInteger`) or strict type comparisons, as the returned objects might be native `BigInt` primitives.
- gotcha JavaScript numbers have a maximum safe integer limit (`Number.MAX_SAFE_INTEGER`). Passing numbers larger than `9007199254740992` or smaller than `-9007199254740992` directly to `bigInt()` will result in loss of precision, as JavaScript itself cannot precisely represent these values.
- deprecated With the widespread adoption of native `BigInt` in modern JavaScript environments, for new projects targeting environments with `BigInt` support, it is generally recommended to use native `BigInt` directly rather than relying on polyfill libraries like `big-integer` for simple arithmetic.
Install
-
npm install big-integer -
yarn add big-integer -
pnpm add big-integer
Imports
- bigInt
import { bigInt } from 'big-integer';import bigInt from 'big-integer';
- bigInt (CommonJS)
const { bigInt } = require('big-integer');const bigInt = require('big-integer'); - BigInteger (Type)
import { BigInteger } from 'big-integer';import type { BigInteger } from 'big-integer';
Quickstart
import bigInt from 'big-integer';
function calculateFactorial(n: number): bigInt.BigInteger {
let result = bigInt.one;
for (let i = 2; i <= n; i++) {
result = result.times(i);
}
return result;
}
// Calculate factorial of a moderately large number
const numberToFactorial = 50;
const factorialResult = calculateFactorial(numberToFactorial);
console.log(`Factorial of ${numberToFactorial} is: ${factorialResult.toString()}`);
// Demonstrating operations with large string inputs to avoid JS number precision limits
const largeA = bigInt('123456789012345678901234567890');
const largeB = bigInt('987654321098765432109876543210');
const sum = largeA.plus(largeB);
console.log(`Sum of two large numbers: ${sum.toString()}`);
// Accessing pre-stored constants
console.log(`Zero: ${bigInt.zero.toString()}`);
console.log(`One: ${bigInt.one.toString()}`);
console.log(`Minus One: ${bigInt.minusOne.toString()}`);
// Note: The actual type of `factorialResult` might be native `BigInt` or `BigInteger` object
// depending on environment support and library's polyfill behavior.