- BigInteger Class
- Static factories
- Constants
- Methods
- Static functions
- BigInteger.toString()
- BigInteger.toJSValue()
- BigInteger.add()
- BigInteger.subtract()
- BigInteger.multiply()
- BigInteger.quotient()
- BigInteger.remainder()
- BigInteger.divRem()
- BigInteger.divide()
- BigInteger.negate()
- BigInteger.abs()
- BigInteger.pow()
- BigInteger.modPow()
- BigInteger.square()
- BigInteger.exp10()
- BigInteger.log()
- BigInteger.next()
- BigInteger.prev()
- BigInteger.compare()
- BigInteger.compareAbs()
- BigInteger.isUnit()
- BigInteger.isEven()
- BigInteger.isOdd()
- BigInteger.sign()
- BigInteger.isPositive()
- BigInteger.isNegative()
- BigInteger.isZero()
An arbitrarily-large integer.
BigInteger objects should be considered immutable. None of the "built-in"
methods modify this or their arguments. All properties should be
considered private.
All the methods of BigInteger instances can be called "statically". The
static versions are convenient if you don't already have a BigInteger
object.
As an example, these calls are equivalent.
BigInteger(4).multiply(5); // returns BigInteger(20);
BigInteger.multiply(4, 5); // returns BigInteger(20);
var a = 42;
var a = BigInteger.toJSValue("0b101010");Convert a value to a BigInteger.
Although BigInteger() is the constructor for BigInteger objects, it should
be called as a function. If n is a BigInteger object, it is simply returned
as-is. Otherwise, BigInteger() is equivalent to BigInteger.parse without a
radix argument.
var n0 = BigInteger(); // Same as BigInteger.ZERO
var n1 = BigInteger("123"); // Create a new BigInteger with value 123
var n2 = BigInteger(123); // Create a new BigInteger with value 123
var n3 = BigInteger(n2); // Return n2, unchanged- n - Value to convert to a
BigInteger.
A BigInteger value.
Parse a string into a BigInteger.
base is optional but, if provided, must be from 2 to 36 inclusive. If base
is not provided, it will be guessed based on the leading characters of s as
follows:
"0x"or"0X":base= 16"0c"or"0C":base= 8"0b"or"0B":base= 2- else:
base= 10
If no base is provided, or base is 10, the number can be in exponential form.
For example, these are all valid:
BigInteger.parse("1e9"); // Same as "1000000000"
BigInteger.parse("1.234*10^3"); // Same as 1234
BigInteger.parse("56789 * 10 ** -2"); // Same as 567If any characters fall outside the range defined by the radix, an exception will be thrown.
s- The string to parse.base- Optional radix (default is to guess based ons).
a BigInteger instance.
BigInteger 0.
BigInteger 1.
BigInteger -1.
Shortcut for ZERO.
Shortcut for ONE.
Array of BigIntegers from 0 to 36.
These are used internally for parsing, but useful when you need a "small" BigInteger.
The largest exponent allowed in pow and exp10 (0x7FFFFFFF or 2147483647).
Convert a BigInteger to a string.
When base is greater than 10, letters are upper case.
base- Optional base to represent the number in (default is base 10). Must be between 2 and 36 inclusive, or an Error will be thrown.
The string representation of the BigInteger.
Convert a BigInteger to a native JavaScript integer.
parseInt(this.toString(), 10)
Add two BigIntegers
- n - The number to add to
this. Will be converted to aBigInteger.
The numbers added together.
Subtract two BigIntegers.
n- The number to subtract fromthis. Will be converted to aBigInteger.
The n subtracted from this.
Multiply two BigIntegers.
n- The number to multiplythisby. Will be converted to aBigInteger.
The numbers multiplied together.
Divide two BigIntegers and truncate towards zero.
quotient() throws an exception if n is zero.
n- The number to dividethisby. Will be converted to aBigInteger.
this / n, truncated to an integer.
Calculate the remainder of two BigIntegers.
remainder throws an exception if n is zero.
n- The remainder afterthisis dividedthisbyn. Will be converted to aBigInteger.
this % n.
Calculate the integer quotient and remainder of two BigIntegers.
divRem throws an exception if n is zero.
n- The number to dividethisby. Will be converted to aBigInteger.
A two-element array containing the quotient and the remainder.
a.divRem(b)is exactly equivalent to
[a.quotient(b), a.remainder(b)]except it is faster, because they are calculated at the same time.
Deprecated synonym for quotient().
Get the additive inverse of a BigInteger.
A BigInteger with the same magnatude, but with the opposite sign.
Get the absolute value of a BigInteger.
A BigInteger with the same magnitude, but always positive (or zero).
Raise a BigInteger to a power.
In this implementation, 0**0 is 1.
n- The exponent to raisethisby.nmust be no greater thanBigInteger.MAX_EXP(0x7FFFFFFF), or an exception will be thrown.
this raised to the nth power.
Raise a BigInteger to a power (mod m).
Because it is reduced by a modulus, modPow is not limited by
BigInteger.MAX_EXP like pow.
exponent- The exponent to raisethisby. Must be positive.modulus- The modulus.
this ** exponent (mod modulus).
Multiply a BigInteger by itself.
This is slightly faster than regular multiplication, since it removes the duplicated multiplications.
this.multiply(this)Multiply a BigInteger by a power of 10.
This is equivalent to, but faster than
if (n >= 0) {
return this.multiply(BigInteger("1e" + n));
} else { // n <= 0
return this.quotient(BigInteger("1e" + -n));
}n- The power of 10 to multiplythisby.nis converted to a javascipt number and must be no greater thanBigInteger.MAX_EXP(0x7FFFFFFF), or an exception will be thrown.
this * (10 ** n), truncated to an integer if necessary.
Get the natural logarithm of a BigInteger as a native JavaScript number.
This is equivalent to
Math.log(this.toJSValue())but handles values outside of the native number range.
log(this)
Get the next BigInteger (add one).
this + 1.
Get the previous BigInteger (subtract one).
this - 1.
Compare two BigIntegers.
n- The number to compare tothis. Will be converted to aBigInteger.
-1, 0, or +1 if this is less than, equal to, or greater than n.
Compare the absolute value of two BigIntegers.
Calling compareAbs is faster than calling abs() twice, then compare().
n- The number to compare tothis. Will be converted to aBigInteger.
-1, 0, or +1 if |this| is less than, equal to, or greater than |n|.
Return true iff this is either 1 or -1.
true if this compares equal to BigInteger.ONE or BigInteger.M_ONE.
Return true iff this is divisible by two.
Note that BigInteger.ZERO is even.
true if this is even, false otherwise.
Return true iff this is not divisible by two.
true if this is odd, false otherwise.
Get the sign of a BigInteger.
-1ifthis < 00ifthis == 0+1ifthis > 0
Return true iff this > 0.
true if this.compare(BigInteger.ZERO) == 1.
Return true iff this < 0.
true if this.compare(BigInteger.ZERO) == -1.
Return true iff this == 0.
true if this.compare(BigInteger.ZERO) == 0.
Convert a BigInteger to a native JavaScript integer.
This is called automatically by JavaScipt to convert a BigInteger to a
native value.
parseInt(this.toString(), 10)
Static equivalent to BigInteger(n).toString().
Static equivalent to BigInteger(n).toJSValue().
Static equivalent to BigInteger(a).add(b).
Static equivalent to BigInteger(a).subtract(b).
Static equivalent to BigInteger(a).multiply(b).
Static equivalent to BigInteger(a).quotient(b).
Static equivalent to BigInteger(a).remainder(b).
Static equivalent to BigInteger(a).divRem(b).
Static equivalent to BigInteger(a).divide(b).
Static equivalent to BigInteger(n).negate().
Static equivalent to BigInteger(n).abs().
Static equivalent to BigInteger(a).pow(b).
Static equivalent to BigInteger(base).modPow(exponent, modulus).
Static equivalent to BigInteger(n).square().
Static equivalent to BigInteger(a).exp10(b).
Static equivalent to BigInteger(n).log().
Static equivalent to BigInteger(n).next().
Static equivalent to BigInteger(n).prev().
Static equivalent to BigInteger(a).compare(b).
Static equivalent to BigInteger(a).compareAbs(b).
Static equivalent to BigInteger(n).isUnit().
Static equivalent to BigInteger(n).isEven().
Static equivalent to BigInteger(n).isOdd().
Static equivalent to BigInteger(n).sign().
Static equivalent to BigInteger(n).isPositive().
Static equivalent to BigInteger(n).isNegative().
Static equivalent to BigInteger(n).isZero().