Comprehensive arithmetic operations for arbitrary-precision integer mathematics including basic operations, division with remainders, modular arithmetic, and exponentiation.
Add two BigInteger values.
/**
* Performs addition
* @param number - Value to add (number, bigint, string, or BigInteger)
* @returns New BigInteger with the sum
*/
add(number: BigNumber): BigInteger;
// Alias
plus(number: BigNumber): BigInteger;Usage Examples:
const bigInt = require('big-integer');
// Simple addition
bigInt(5).add(7); // 12
// Large number addition
const a = bigInt("999999999999999999999");
const b = bigInt("1");
a.add(b); // 1000000000000000000000
// Method chaining
bigInt(10).add(5).add(3); // 18Subtract one BigInteger from another.
/**
* Performs subtraction
* @param number - Value to subtract
* @returns New BigInteger with the difference
*/
subtract(number: BigNumber): BigInteger;
// Alias
minus(number: BigNumber): BigInteger;Usage Examples:
// Simple subtraction
bigInt(10).subtract(3); // 7
// Negative results
bigInt(3).subtract(5); // -2
// Method chaining
bigInt(100).minus(30).minus(20); // 50Multiply two BigInteger values.
/**
* Performs multiplication
* @param number - Value to multiply by
* @returns New BigInteger with the product
*/
multiply(number: BigNumber): BigInteger;
// Alias
times(number: BigNumber): BigInteger;Usage Examples:
// Simple multiplication
bigInt(111).multiply(111); // 12321
// Large number multiplication
const big = bigInt("123456789012345678901234567890");
big.multiply(2); // 246913578024691357802469135780
// Method chaining
bigInt(5).times(10).times(2); // 100Square a BigInteger (optimized compared to multiply(n)).
/**
* Squares the number (equivalent to multiply(this) but optimized)
* @returns New BigInteger with the square
*/
square(): BigInteger;Usage Examples:
// Square a number
bigInt(3).square(); // 9
// Large squares
bigInt("12345").square(); // 152399025Divide and return only the quotient, discarding the remainder.
/**
* Performs integer division (discards remainder)
* @param number - Divisor
* @returns New BigInteger with the quotient
*/
divide(number: BigNumber): BigInteger;
// Alias
over(number: BigNumber): BigInteger;Usage Examples:
// Integer division
bigInt(59).divide(5); // 11 (remainder 4 is discarded)
// Negative division
bigInt(-10).divide(3); // -3Get the remainder of division. The sign of the remainder matches the sign of the dividend.
/**
* Performs division and returns the remainder
* The sign of the remainder matches the sign of the dividend
* @param number - Divisor
* @returns New BigInteger with the remainder
*/
mod(number: BigNumber): BigInteger;
// Alias
remainder(number: BigNumber): BigInteger;Usage Examples:
// Modulo operation
bigInt(59).mod(5); // 4
// Negative dividend
bigInt(-5).mod(2); // -1 (sign matches dividend)
// Large modulo
bigInt("123456789").mod(1000); // 789Get both quotient and remainder in a single operation.
/**
* Performs division and returns both quotient and remainder
* The sign of the remainder matches the sign of the dividend
* @param number - Divisor
* @returns Object with quotient and remainder properties
*/
divmod(number: BigNumber): { quotient: BigInteger, remainder: BigInteger };Usage Examples:
// Get both quotient and remainder
const result = bigInt(59).divmod(5);
result.quotient; // 11
result.remainder; // 4
// Negative dividend
const neg = bigInt(-5).divmod(2);
neg.quotient; // -2
neg.remainder; // -1 (sign matches dividend)
// Destructuring
const { quotient, remainder } = bigInt(100).divmod(7);
// quotient: 14, remainder: 2Raise a BigInteger to a power.
/**
* Performs exponentiation
* Returns 0 for negative exponents
* bigInt.zero.pow(0) returns 1
* @param number - Exponent
* @returns New BigInteger raised to the power
*/
pow(number: BigNumber): BigInteger;Usage Examples:
// Basic exponentiation
bigInt(2).pow(10); // 1024
// Large exponents
bigInt(2).pow(100); // 1267650600228229401496703205376
// Zero base
bigInt.zero.pow(0); // 1
// Negative exponent returns 0
bigInt(5).pow(-2); // 0Efficiently compute (base ^ exponent) mod modulus without computing the full power first.
/**
* Takes the number to the power exp modulo mod
* Efficient for cryptographic operations
* @param exp - Exponent
* @param mod - Modulus
* @returns (this ^ exp) mod mod
*/
modPow(exp: BigNumber, mod: BigNumber): BigInteger;Usage Examples:
// Modular exponentiation
bigInt(10).modPow(3, 30); // 10 (since 1000 mod 30 = 10)
// Cryptographic use case
bigInt(2).modPow(1000, 97); // 36
// Large exponents stay efficient
const base = bigInt(7);
const exp = bigInt("1234567890");
const mod = bigInt(1000000);
base.modPow(exp, mod); // Result computed efficientlyFind the multiplicative inverse of a number modulo m (i.e., find x such that (this * x) mod m = 1).
/**
* Finds the multiplicative inverse of the number modulo mod
* Returns x such that (this * x) mod mod = 1
* @param mod - Modulus
* @returns Modular multiplicative inverse
*/
modInv(mod: BigNumber): BigInteger;Usage Examples:
// Find modular inverse
bigInt(3).modInv(11); // 4 (because 3 * 4 mod 11 = 1)
// Cryptographic use case
bigInt(42).modInv(2017); // 1969
// Verification
const a = bigInt(3);
const m = bigInt(11);
const inv = a.modInv(m);
a.multiply(inv).mod(m); // 1 (verification)Get the absolute value of a BigInteger.
/**
* Returns the absolute value
* @returns New BigInteger with absolute value
*/
abs(): BigInteger;Usage Examples:
bigInt(-45).abs(); // 45
bigInt(45).abs(); // 45
bigInt.zero.abs(); // 0Reverse the sign of a BigInteger.
/**
* Reverses the sign of the number
* @returns New BigInteger with opposite sign
*/
negate(): BigInteger;Usage Examples:
bigInt(10).negate(); // -10
bigInt(-10).negate(); // 10
bigInt.zero.negate(); // 0Add or subtract one from a BigInteger.
/**
* Adds one to the number
* @returns New BigInteger incremented by 1
*/
next(): BigInteger;
/**
* Subtracts one from the number
* @returns New BigInteger decremented by 1
*/
prev(): BigInteger;Usage Examples:
// Increment
bigInt(6).next(); // 7
bigInt(-1).next(); // 0
// Decrement
bigInt(6).prev(); // 5
bigInt(0).prev(); // -1
// Chaining
bigInt(10).next().next(); // 12For convenience, several methods have shorter aliases:
add() ↔ plus()subtract() ↔ minus()multiply() ↔ times()divide() ↔ over()mod() ↔ remainder()