biginteger.js | BigInteger |
BigInteger | An arbitrarily-large integer. |
Functions | |
BigInteger() | Convert a value to a BigInteger. |
Constants | |
ZERO | BigInteger 0. |
ONE | BigInteger 1. |
M_ONE | BigInteger -1. |
_0 | Shortcut for ZERO. |
_1 | Shortcut for ONE. |
small | Array of BigIntegers from 0 to 36. |
Functions | |
toString | Convert a BigInteger to a string. |
parse | Parse a string into a BigInteger. |
add | Add two BigIntegers. |
negate | Get the additive inverse of a BigInteger. |
abs | Get the absolute value of a BigInteger. |
subtract | Subtract two BigIntegers. |
next | Get the next BigInteger (add one). |
prev | Get the previous BigInteger (subtract one). |
compareAbs | Compare the absolute value of two BigIntegers. |
compare | Compare two BigIntegers. |
isUnit | Return true iff this is either 1 or -1. |
multiply | Multiply two BigIntegers. |
square | Multiply a BigInteger by itself. |
quotient | Divide two BigIntegers and truncate towards zero. |
divide | Deprecated synonym for quotient. |
remainder | Calculate the remainder of two BigIntegers. |
divRem | Calculate the integer quotient and remainder of two BigIntegers. |
isEven | Return true iff this is divisible by two. |
isOdd | Return true iff this is not divisible by two. |
sign | Get the sign of a BigInteger. |
isPositive | Return true iff this > 0. |
isNegative | Return true iff this < 0. |
isZero | Return true iff this == 0. |
exp10 | Multiply a BigInteger by a power of 10. |
pow | Raise a BigInteger to a power. |
modPow | Raise a BigInteger to a power (mod m). |
log | Get the natural logarithm of a BigInteger as a native JavaScript number. |
valueOf | Convert a BigInteger to a native JavaScript integer. |
toJSValue | Convert a BigInteger to a native JavaScript integer. |
Constants | |
MAX_EXP | The largest exponent allowed in pow and exp10 (0x7FFFFFFF or 2147483647). |
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"); // Not completely useless...
Functions | |
BigInteger() | Convert a value to a BigInteger. |
Constants | |
ZERO | BigInteger 0. |
ONE | BigInteger 1. |
M_ONE | BigInteger -1. |
_0 | Shortcut for ZERO. |
_1 | Shortcut for ONE. |
small | Array of BigIntegers from 0 to 36. |
Functions | |
toString | Convert a BigInteger to a string. |
parse | Parse a string into a BigInteger. |
add | Add two BigIntegers. |
negate | Get the additive inverse of a BigInteger. |
abs | Get the absolute value of a BigInteger. |
subtract | Subtract two BigIntegers. |
next | Get the next BigInteger (add one). |
prev | Get the previous BigInteger (subtract one). |
compareAbs | Compare the absolute value of two BigIntegers. |
compare | Compare two BigIntegers. |
isUnit | Return true iff this is either 1 or -1. |
multiply | Multiply two BigIntegers. |
square | Multiply a BigInteger by itself. |
quotient | Divide two BigIntegers and truncate towards zero. |
divide | Deprecated synonym for quotient. |
remainder | Calculate the remainder of two BigIntegers. |
divRem | Calculate the integer quotient and remainder of two BigIntegers. |
isEven | Return true iff this is divisible by two. |
isOdd | Return true iff this is not divisible by two. |
sign | Get the sign of a BigInteger. |
isPositive | Return true iff this > 0. |
isNegative | Return true iff this < 0. |
isZero | Return true iff this == 0. |
exp10 | Multiply a BigInteger by a power of 10. |
pow | Raise a BigInteger to a power. |
modPow | Raise a BigInteger to a power (mod m). |
log | Get the natural logarithm of a BigInteger as a native JavaScript number. |
valueOf | Convert a BigInteger to a native JavaScript integer. |
toJSValue | Convert a BigInteger to a native JavaScript integer. |
Constants | |
MAX_EXP | The largest exponent allowed in pow and exp10 (0x7FFFFFFF or 2147483647). |
function BigInteger( n, s )
Convert a value to a BigInteger.
Although BigInteger() is the constructor for BigInteger objects, it is best not to call it as a constructor. If n is a BigInteger object, it is simply returned as-is. Otherwise, BigInteger() is equivalent to 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
The constructor form only takes an array and a sign. n must be an array of numbers in little-endian order, where each digit is between 0 and BigInteger.base. The second parameter sets the sign: -1 for negative, +1 for positive, or 0 for zero. The array is not copied and may be modified. If the array contains only zeros, the sign parameter is ignored and is forced to zero.
new BigInteger([5], -1): create a new BigInteger with value -5
n | Value to convert to a BigInteger. |
A BigInteger value.
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.
BigInteger.prototype.toString = function( base )
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.
BigInteger.parse = function( s, base )
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:
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 567
If 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 on s). |
a BigInteger instance.
BigInteger.prototype.add = function( n )
Add two BigIntegers.
n | The number to add to this. Will be converted to a BigInteger. |
The numbers added together.
BigInteger.prototype.negate = function()
Get the additive inverse of a BigInteger.
A BigInteger with the same magnatude, but with the opposite sign.
BigInteger.prototype.abs = function()
Get the absolute value of a BigInteger.
A BigInteger with the same magnatude, but always positive (or zero).
BigInteger.prototype.subtract = function( n )
Subtract two BigIntegers.
n | The number to subtract from this. Will be converted to a BigInteger. |
The n subtracted from this.
BigInteger.prototype.prev = function()
Get the previous BigInteger (subtract one).
*this* | 1. |
BigInteger.prototype.compareAbs = function( n )
Compare the absolute value of two BigIntegers.
Calling compareAbs is faster than calling abs twice, then compare.
n | The number to compare to this. Will be converted to a BigInteger. |
-1, 0, or +1 if |this| is less than, equal to, or greater than |n|.
BigInteger.prototype.compare = function( n )
Compare two BigIntegers.
n | The number to compare to this. Will be converted to a BigInteger. |
-1, 0, or +1 if this is less than, equal to, or greater than n.
BigInteger.prototype.isUnit = function()
Return true iff this is either 1 or -1.
true if this compares equal to BigInteger.ONE or BigInteger.M_ONE.
isZero, isNegative, isPositive, compareAbs, compare, BigInteger.ONE, BigInteger.M_ONE
BigInteger.prototype.multiply = function( n )
Multiply two BigIntegers.
n | The number to multiply this by. Will be converted to a BigInteger. |
The numbers multiplied together.
BigInteger.prototype.square = function()
Multiply a BigInteger by itself.
This is slightly faster than regular multiplication, since it removes the duplicated multiplcations.
this.multiply(this)
BigInteger.prototype.quotient = function( n )
Divide two BigIntegers and truncate towards zero.
quotient throws an exception if n is zero.
n | The number to divide this by. Will be converted to a BigInteger. |
The this / n, truncated to an integer.
Deprecated synonym for quotient.
BigInteger.prototype.remainder = function( n )
Calculate the remainder of two BigIntegers.
remainder throws an exception if n is zero.
n | The remainder after this is divided this by n. Will be converted to a BigInteger. |
this % n.
BigInteger.prototype.divRem = function( n )
Calculate the integer quotient and remainder of two BigIntegers.
divRem throws an exception if n is zero.
n | The number to divide this by. Will be converted to a BigInteger. |
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.
BigInteger.prototype.isEven = function()
Return true iff this is divisible by two.
Note that BigInteger.ZERO is even.
true if this is even, false otherwise.
BigInteger.prototype.sign = function()
Get the sign of a BigInteger.
BigInteger.prototype.isPositive = function()
Return true iff this > 0.
true if this.compare(BigInteger.ZERO) == 1.
BigInteger.prototype.isNegative = function()
Return true iff this < 0.
true if this.compare(BigInteger.ZERO) == -1.
BigInteger.prototype.isZero = function()
Return true iff this == 0.
true if this.compare(BigInteger.ZERO) == 0.
BigInteger.prototype.exp10 = function( n )
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 multiply this by. n is converted to a javascipt number and must be no greater than BigInteger.MAX_EXP (0x7FFFFFFF), or an exception will be thrown. |
this * (10 ** n), truncated to an integer if necessary.
BigInteger.prototype.pow = function( n )
Raise a BigInteger to a power.
In this implementation, 0**0 is 1.
n | The exponent to raise this by. n must be no greater than BigInteger.MAX_EXP (0x7FFFFFFF), or an exception will be thrown. |
this raised to the nth power.
BigInteger.prototype.modPow = function( exponent, modulus )
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 raise this by. Must be positive. |
modulus | The modulus. |
this ^ exponent (mod modulus).
pow, <mod>
BigInteger.prototype.log = function()
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 )
BigInteger.prototype.valueOf = function()
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)
BigInteger.prototype.toJSValue = function()
Convert a BigInteger to a native JavaScript integer.
This is the same as valueOf, but more explicitly named.
parseInt(this.toString(), 10)
Convert a value to a BigInteger.
function BigInteger( n, s )
Convert a BigInteger to a string.
BigInteger.prototype.toString = function( base )
Parse a string into a BigInteger.
BigInteger.parse = function( s, base )
Add two BigIntegers.
BigInteger.prototype.add = function( n )
Get the additive inverse of a BigInteger.
BigInteger.prototype.negate = function()
Get the absolute value of a BigInteger.
BigInteger.prototype.abs = function()
Subtract two BigIntegers.
BigInteger.prototype.subtract = function( n )
Get the next BigInteger (add one).
BigInteger.prototype.next = function()
Get the previous BigInteger (subtract one).
BigInteger.prototype.prev = function()
Compare the absolute value of two BigIntegers.
BigInteger.prototype.compareAbs = function( n )
Compare two BigIntegers.
BigInteger.prototype.compare = function( n )
Return true iff this is either 1 or -1.
BigInteger.prototype.isUnit = function()
Multiply two BigIntegers.
BigInteger.prototype.multiply = function( n )
Multiply a BigInteger by itself.
BigInteger.prototype.square = function()
Divide two BigIntegers and truncate towards zero.
BigInteger.prototype.quotient = function( n )
Calculate the remainder of two BigIntegers.
BigInteger.prototype.remainder = function( n )
Calculate the integer quotient and remainder of two BigIntegers.
BigInteger.prototype.divRem = function( n )
Return true iff this is divisible by two.
BigInteger.prototype.isEven = function()
Return true iff this is not divisible by two.
BigInteger.prototype.isOdd = function()
Get the sign of a BigInteger.
BigInteger.prototype.sign = function()
Return true iff this > 0.
BigInteger.prototype.isPositive = function()
Return true iff this < 0.
BigInteger.prototype.isNegative = function()
Return true iff this == 0.
BigInteger.prototype.isZero = function()
Multiply a BigInteger by a power of 10.
BigInteger.prototype.exp10 = function( n )
Raise a BigInteger to a power.
BigInteger.prototype.pow = function( n )
Raise a BigInteger to a power (mod m).
BigInteger.prototype.modPow = function( exponent, modulus )
Get the natural logarithm of a BigInteger as a native JavaScript number.
BigInteger.prototype.log = function()
Convert a BigInteger to a native JavaScript integer.
BigInteger.prototype.valueOf = function()
Convert a BigInteger to a native JavaScript integer.
BigInteger.prototype.toJSValue = function()