An arbitrary length integer library for Javascript
npm install big-integerBigInt is being added as a native feature of JavaScript. This library now works as a polyfill: if the environment supports the native BigInt, this library acts as a thin wrapper over the native implementation.
bigInt function. You can pass in
"Invalid integer" error if the parsing fails.
"Invalid integer" error if the parsing fails.
bigInt.zero.
number as a number in base base. Note that base can be any bigInt (even negative or zero). The letters "a-z" and "A-Z" will be interpreted as the numbers 10 to 35. Higher digits can be specified in angle brackets (< and >). The default base is 10.
alphabet is "0123456789abcdefghijklmnopqrstuvwxyz".
a and A should be treated as different digits. By default caseSensitive is false.
9007199254740992 and smaller than -9007199254740992 are not precisely represented numbers and will not produce exact results. If you are dealing with numbers outside that range, it is better to pass in strings.
bigInt function yourself:
bigInt.one, equivalent to bigInt(1)
bigInt.zero, equivalent to bigInt(0)
bigInt.minusOne, equivalent to bigInt(-1)
bigInt[index], for example:
bigInt[-999], equivalent to bigInt(-999)
bigInt[256], equivalent to bigInt(256)
abs()
bigInt(-45).abs() => 45
bigInt(45).abs() => 45
add(number)
bigInt(5).add(7) => 12
and(number)
bigInt(6).and(3) => 2
bigInt(6).and(-3) => 4
bitLength()
bigInt(5) => 3 (since 5 is 101 in binary, which is three digits long)
compare(number)
0. If the first number is greater, it returns 1. If the first number is lesser, it returns -1.
bigInt(5).compare(5) => 0
bigInt(5).compare(4) => 1
bigInt(4).compare(5) => -1
compareAbs(number)
bigInt(5).compareAbs(-5) => 0
bigInt(5).compareAbs(4) => 1
bigInt(4).compareAbs(-5) => -1
compareTo(number)
compare method.
divide(number)
bigInt(59).divide(5) => 11
divmod(number)
quotient and remainder. The sign of the remainder will match the sign of the dividend.
bigInt(59).divmod(5) => {quotient: bigInt(11), remainder: bigInt(4) }
bigInt(-5).divmod(2) => {quotient: bigInt(-2), remainder: bigInt(-1) }
eq(number)
equals method.
equals(number)
bigInt(5).equals(5) => true
bigInt(4).equals(7) => false
geq(number)
greaterOrEquals method.
greater(number)
bigInt(5).greater(6) => false
bigInt(5).greater(5) => false
bigInt(5).greater(4) => true
greaterOrEquals(number)
bigInt(5).greaterOrEquals(6) => false
bigInt(5).greaterOrEquals(5) => true
bigInt(5).greaterOrEquals(4) => true
gt(number)
greater method.
isDivisibleBy(number)
true if the first number is divisible by the second number, false otherwise.
bigInt(999).isDivisibleBy(333) => true
bigInt(99).isDivisibleBy(5) => false
isEven()
true if the number is even, false otherwise.
bigInt(6).isEven() => true
bigInt(3).isEven() => false
isNegative()
true if the number is negative, false otherwise.
false for 0 and -0.
bigInt(-23).isNegative() => true
bigInt(50).isNegative() => false
isOdd()
true if the number is odd, false otherwise.
bigInt(13).isOdd() => true
bigInt(40).isOdd() => false
isPositive()
true if the number is positive, false otherwise.
false for 0 and -0.
bigInt(54).isPositive() => true
bigInt(-1).isPositive() => false
isPrime(strict?)
true if the number is prime, false otherwise.
bigInt(5).isPrime() => true
bigInt(6).isPrime() => false
isProbablePrime([iterations], [rng])
true if the number is very likely to be prime, false otherwise.
iterations is optional - it determines the number of iterations of the test (default: 5). The more iterations, the lower chance of getting a false positive.
bigInt(5).isProbablePrime() => true
bigInt(49).isProbablePrime() => false
bigInt(1729).isProbablePrime() => false
rng. The behavior and requirements are the same as with randBetween.
bigInt(1729).isProbablePrime(1, () => 0.1) => false
bigInt(1729).isProbablePrime(1, () => 0.2) => true
4 to the power −iterations.
true.
isUnit()
true if the number is 1 or -1, false otherwise.
bigInt.one.isUnit() => true
bigInt.minusOne.isUnit() => true
bigInt(5).isUnit() => false
isZero()
true if the number is 0 or -0, false otherwise.
bigInt.zero.isZero() => true
bigInt("-0").isZero() => true
bigInt(50).isZero() => false
leq(number)
lesserOrEquals method.
lesser(number)
bigInt(5).lesser(6) => true
bigInt(5).lesser(5) => false
bigInt(5).lesser(4) => false
lesserOrEquals(number)
bigInt(5).lesserOrEquals(6) => true
bigInt(5).lesserOrEquals(5) => true
bigInt(5).lesserOrEquals(4) => false
lt(number)
lesser method.
minus(number)
subtract method.
bigInt(3).minus(5) => -2
mod(number)
bigInt(59).mod(5) => 4
bigInt(-5).mod(2) => -1
modInv(mod)
mod.
bigInt(3).modInv(11) => 4
bigInt(42).modInv(2017) => 1969
modPow(exp, mod)
exp modulo mod.
bigInt(10).modPow(3, 30) => 10
multiply(number)
bigInt(111).multiply(111) => 12321
neq(number)
notEquals method.
next()
bigInt(6).next() => 7
not()
bigInt(10).not() => -11
bigInt(0).not() => -1
notEquals(number)
bigInt(5).notEquals(5) => false
bigInt(4).notEquals(7) => true
or(number)
bigInt(13).or(10) => 15
bigInt(13).or(-8) => -3
over(number)
divide method.
bigInt(59).over(5) => 11
plus(number)
add method.
bigInt(5).plus(7) => 12
pow(number)
0, pow returns 0. bigInt.zero.pow(0) returns 1.
bigInt(16).pow(16) => 18446744073709551616
prev(number)
bigInt(6).prev() => 5
remainder(number)
mod method.
shiftLeft(n)
n places in its binary representation. If a negative number is provided, it will shift right. Throws an error if n is outside of the range [-9007199254740992, 9007199254740992].
bigInt(8).shiftLeft(2) => 32
bigInt(8).shiftLeft(-2) => 2
shiftRight(n)
n places in its binary representation. If a negative number is provided, it will shift left. Throws an error if n is outside of the range [-9007199254740992, 9007199254740992].
bigInt(8).shiftRight(2) => 2
bigInt(8).shiftRight(-2) => 32
square()
bigInt(3).square() => 9
subtract(number)
bigInt(3).subtract(5) => -2
times(number)
multiply method.
bigInt(111).times(111) => 12321
toArray(radix)
bigInt("1e9").toArray(10) => {
bigInt("1e9").toArray(16) => {
bigInt(567890).toArray(100) => {
bigInt(12345).toArray(-10) => {
bigInt(-15).toArray(1) => {
bigInt(-15).toArray(-1) => {
bigInt(0).toArray(0) => {
bigInt(1).toArray(0) => Error: Cannot convert nonzero numbers to base 0.
toJSNumber()
[-9007199254740992, 9007199254740992].
bigInt("18446744073709551616").toJSNumber() => 18446744073709552000
xor(number)
bigInt(12).xor(5) => 9
bigInt(12).xor(-5) => -9
fromArray(digits, base = 10, isNegative?)
base. The optional isNegative flag will make the number negative.
bigInt.fromArray([1, 2, 3, 4, 5], 10) => 12345
bigInt.fromArray([1, 0, 0], 2, true) => -4
gcd(a, b)
a and b.
bigInt.gcd(42,56) => 14
isInstance(x)
true if x is a BigInteger, false otherwise.
bigInt.isInstance(bigInt(14)) => true
bigInt.isInstance(14) => false
lcm(a,b)
a and b.
bigInt.lcm(21, 6) => 42
max(a,b)
a and b.
bigInt.max(77, 432) => 432
min(a,b)
a and b.
bigInt.min(77, 432) => 77
randBetween(min, max, [rng])
min and max, optionally using rng to generate randomness.
bigInt.randBetween("-1e100", "1e100") => (for example) 8494907165436643479673097939554427056789510374838494147955756275846226209006506706784609314471378745
rng should take no arguments and return a number between 0 and 1. It defaults to Math.random.
bigInt.randBetween("-1e100", "1e100", () => 0.5) => (always) 50000005000000500000050000005000000500000050000005000000500000050000005000000500000050000005000000
toString(radix = 10, [alphabet])
10-35 will use the letters a-z.
bigInt("1e9").toString() => "1000000000"
bigInt("1e9").toString(16) => "3b9aca00"
alphabet is "0123456789abcdefghijklmnopqrstuvwxyz".
bigInt("5").toString(2, "aA") => "AaA"
valueOf function rather than the toString function. When converting a bigInteger to a string, you should use the toString method or the String function instead of adding the empty string.
bigInt("999999999999999999").toString() => "999999999999999999"
String(bigInt("999999999999999999")) => "999999999999999999"
bigInt("999999999999999999") + "" => 1000000000000000000
bigInt(567890).toString(100) => "<56><78><90>"
bigInt(12345).toString(-10) => "28465"
bigInt(-15).toString(1) => "-111111111111111"
bigInt(-15).toString(-1) => "101010101010101010101010101010"
bigInt(0).toString(0) => 0
bigInt(1).toString(0) => Error: Cannot convert nonzero numbers to base 0.
valueOf()
bigInt("100") + bigInt("200") === 300; //true
spec/spec.js file. You can run them locally by opening the spec/SpecRunner.html or file or running npm test. You can also run the tests online from GitHub.
benchmarks/index.html page. You can run them online from GitHub.