BigInteger.js is an arbitrary-length integer library for Javascript, allowing arithmetic operations on integers of unlimited size, notwithstanding memory and time limitations.
Update (December 2, 2018):BigInt 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.
You can create a bigInt by calling the bigInt function. You can pass in
a string, which it will parse as an bigInt and throw an "Invalid integer" error if the parsing fails.
a Javascript number, which it will parse as an bigInt and throw an "Invalid integer" error if the parsing fails.
another bigInt.
nothing, and it will return bigInt.zero.
If you provide a second parameter, then it will parse 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.
You can specify a custom alphabet for base conversion with the third parameter. The default alphabet is "0123456789abcdefghijklmnopqrstuvwxyz".
The fourth parameter specifies whether or not the number string should be case-sensitive, i.e. whether a and A should be treated as different digits. By default caseSensitive is false.
Note that Javascript numbers larger than 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.
Method Chaining
Note that bigInt operations return bigInts, which allows you to chain methods, for example:
var salary = bigInt(dollarsPerHour).times(hoursWorked).plus(randomBonuses)
Constants
There are three named constants already stored that you do not have to construct with the bigInt function yourself:
bigInt.one, equivalent to bigInt(1)
bigInt.zero, equivalent to bigInt(0)
bigInt.minusOne, equivalent to bigInt(-1)
The numbers from -999 to 999 are also already prestored and can be accessed using bigInt[index], for example:
Performs the bitwise AND operation. The operands are treated as if they were represented using two's complement representation.
bigInt(6).and(3) => 2
bigInt(6).and(-3) => 4
bitLength()
Returns the number of digits required to represent a bigInt in binary.
bigInt(5) => 3 (since 5 is 101 in binary, which is three digits long)
compare(number)
Performs a comparison between two numbers. If the numbers are equal, it returns 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)
Performs a comparison between the absolute value of two numbers.
bigInt(5).compareAbs(-5) => 0
bigInt(5).compareAbs(4) => 1
bigInt(4).compareAbs(-5) => -1
compareTo(number)
Alias for the compare method.
divide(number)
Performs integer division, disregarding the remainder.
Checks if the first number is greater than the second.
bigInt(5).greater(6) => false
bigInt(5).greater(5) => false
bigInt(5).greater(4) => true
greaterOrEquals(number)
Checks if the first number is greater than or equal to the second.
bigInt(5).greaterOrEquals(6) => false
bigInt(5).greaterOrEquals(5) => true
bigInt(5).greaterOrEquals(4) => true
gt(number)
Alias for the greater method.
isDivisibleBy(number)
Returns true if the first number is divisible by the second number, false otherwise.
bigInt(999).isDivisibleBy(333) => true
bigInt(99).isDivisibleBy(5) => false
isEven()
Returns true if the number is even, false otherwise.
bigInt(6).isEven() => true
bigInt(3).isEven() => false
isNegative()
Returns true if the number is negative, false otherwise.
Returns false for 0 and -0.
bigInt(-23).isNegative() => true
bigInt(50).isNegative() => false
isOdd()
Returns true if the number is odd, false otherwise.
bigInt(13).isOdd() => true
bigInt(40).isOdd() => false
isPositive()
Return true if the number is positive, false otherwise.
Returns false for 0 and -0.
bigInt(54).isPositive() => true
bigInt(-1).isPositive() => false
isPrime(strict?)
Returns true if the number is prime, false otherwise.
Set "strict" boolean to true to force GRH-supported lower bound of 2*log(N)^2.
bigInt(5).isPrime() => true
bigInt(6).isPrime() => false
isProbablePrime([iterations], [rng])
Returns true if the number is very likely to be prime, false otherwise.
Supplying 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.
This uses the Miller Rabin test.
bigInt(5).isProbablePrime() => true
bigInt(49).isProbablePrime() => false
bigInt(1729).isProbablePrime() => false
Note that this function is not deterministic, since it relies on random sampling of factors, so the result for some numbers is not always the same - unless you pass a predictable random number generator as rng. The behavior and requirements are the same as with randBetween.
If the number is composite then the Miller–Rabin primality test declares the number probably prime with a probability at most 4 to the power −iterations.
If the number is prime, this function always returns true.
isUnit()
Returns true if the number is 1 or -1, false otherwise.
bigInt.one.isUnit() => true
bigInt.minusOne.isUnit() => true
bigInt(5).isUnit() => false
isZero()
Return true if the number is 0 or -0, false otherwise.
bigInt.zero.isZero() => true
bigInt("-0").isZero() => true
bigInt(50).isZero() => false
leq(number)
Alias for the lesserOrEquals method.
lesser(number)
Checks if the first number is lesser than the second.
bigInt(5).lesser(6) => true
bigInt(5).lesser(5) => false
bigInt(5).lesser(4) => false
lesserOrEquals(number)
Checks if the first number is less than or equal to the second.
Shifts the number left by 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)
Shifts the number right by 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].
Converts a bigInt into an object with the properties "value" and "isNegative." "Value" is an array of integers modulo the given radix. "isNegative" is a boolean that represents the sign of the result.
Converts a bigInt to a string. There is an optional radix parameter (which defaults to 10) that converts the number to the given radix. Digits in the range 10-35 will use the letters a-z.
bigInt("1e9").toString() => "1000000000"
bigInt("1e9").toString(16) => "3b9aca00"
You can use a custom base alphabet with the second parameter. The default alphabet is "0123456789abcdefghijklmnopqrstuvwxyz".
bigInt("5").toString(2, "aA") => "AaA"
Note that arithmetical operators will trigger the 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.
Converts a bigInt to a native Javascript number. This override allows you to use native arithmetic operators without explicit conversion:
bigInt("100") + bigInt("200") === 300; //true
Contributors
To contribute, just fork the project, make some changes, and submit a pull request. Please verify that the unit tests pass before submitting.
The unit tests are contained in the 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.
There are performance benchmarks that can be viewed from the benchmarks/index.html page. You can run them online from GitHub.
License
This project is public domain. For more details, read about the Unlicense.