biginteger

An arbitrarily-sized-integer library for JavaScript.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
biginteger
18331.0.39 years ago11 years agoMinified + gzip package size for biginteger in KB

Readme

"Permalink to BigInteger source"
BigInteger
An arbitrarily-large integer.
BigInteger1 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 BigInteger1 instances can be called “statically”.  The static versions are convenient if you don’t already have a BigInteger1 object.
As an example, these calls are equivalent.
BigInteger(4).multiply(5); // returns BigIngeger(20);`
BigInteger.multiply(4, 5); // returns BigInteger(20);

var a = 42;
var a = BigInteger.toJSValue("0b101010"); // Not completely useless...

Summary
BigInteger2 An arbitrarily-large integer.
Functions3
BigInteger()1 Convert a value to a BigInteger1.
Constants4
ZERO5 BigInteger1 0.
ONE6 BigInteger1 1.
MONE7 BigInteger1 -1.
08 Shortcut for ZERO5.
19 Shortcut for ONE6.
small10 Array of BigIntegers1 from 0 to 36.
Functions3
toString11 Convert a BigInteger1 to a string.
parse12 Parse a string into a BigInteger1.
add13 Add two BigIntegers1.
negate14 Get the additive inverse of a BigInteger1.
abs15 Get the absolute value of a BigInteger1.
subtract16 Subtract two BigIntegers1.
next17 Get the next BigInteger1 (add one).
prev18 Get the previous BigInteger1 (subtract one).
compareAbs19 Compare the absolute value of two BigIntegers1.
compare20 Compare two BigIntegers1.
isUnit21 Return true iff this is either 1 or -1.
multiply22 Multiply two BigIntegers1.
square23 Multiply a BigInteger1 by itself.
divide24 Divide two BigIntegers1.
remainder25 Calculate the remainder of two BigIntegers1.
divRem26 Calculate the integer quotient and remainder of two BigIntegers1.
isEven27 Return true iff this is divisible by two.
isOdd28 Return true iff this is not divisible by two.
sign29 Get the sign of a BigInteger1.
isPositive30 Return true iff this > 0.
isNegative31 Return true iff this < 0.
isZero32 Return true iff this == 0.
exp1033 Multiply a BigInteger1 by a power of 10.
pow34 Raise a BigInteger1 to a power.
modPow35 Raise a BigInteger1 to a power (mod m).
valueOf36 Convert a BigInteger1 to a native JavaScript integer.
toJSValue37 Convert a BigInteger1 to a native JavaScript integer.
Constants4
MAXEXP38 The largest exponent allowed in pow34 and exp1033 (0x7FFFFFFF or 2147483647).

Functions

BigInteger()

function BigInteger(n, s)
Convert a value to a BigInteger1.
Although BigInteger()1 is the constructor for BigInteger1 objects, it is best not to call it as a constructor.  If n is a BigInteger1 object, it is simply returned as-is.  Otherwise, BigInteger()1 is equivalent to parse12 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 9 inclusive.  A 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([3,2,1], -1): create a new BigInteger with value -123

Parameters

n Value to convert to a BigInteger1.

Returns

A BigInteger1 value.

See Also

parse12, BigInteger1

Constants

ZERO

BigInteger1 0.

ONE

BigInteger1 1.

MONE

BigInteger1 -1.

0

Shortcut for ZERO5.

1

Shortcut for ONE6.

small

Array of BigIntegers1 from 0 to 36.
These are used internally for parsing, but useful when you need a “small” BigInteger1.

See Also

ZERO5, ONE6, 08, 19

Functions

toString

BigInteger.prototype.toString = function(base)
Convert a BigInteger1 to a string.
When base is greater than 10, letters are upper case.

Parameters

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.

Returns

The string representation of the BigInteger1.

parse

BigInteger.parse = function(s, base)
Parse a string into a BigInteger1.
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 ”0b” or “0B”: base = 2 ”0”: base = 8 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 567

If any characters fall outside the range defined by the radix, an exception will be thrown.

Parameters

s The string to parse.
base Optional radix (default is to guess based on s).

Returns

a BigInteger1 instance.

add

BigInteger.prototype.add = function(n)
Add two BigIntegers1.

Parameters

n The number to add to this.  Will be converted to a BigInteger1.

Returns

The numbers added together.

See Also

subtract16, multiply22, divide24, next17

negate

> BigInteger.prototype.negate = function()
Get the additive inverse of a BigInteger1.

Returns

A BigInteger1 with the same magnatude, but with the opposite sign.

See Also

abs15

abs

> BigInteger.prototype.abs = function()
Get the absolute value of a BigInteger1.

Returns

A BigInteger1 with the same magnatude, but always positive (or zero).

See Also

negate14

subtract

BigInteger.prototype.negate = function()
Subtract two BigIntegers1.

Parameters

n The number to subtract from this.  Will be converted to a BigInteger1.

Returns

The n subtracted from this.

See Also

add13, multiply22, divide24, prev18

next

BigInteger.prototype.next = function()
Get the next BigInteger1 (add one).

Returns

this + 1.

See Also

add13, prev18

prev

BigInteger.prototype.prev = function()
Get the previous BigInteger1 (subtract one).

Returns

this
  1. See Also


next17, subtract16

compareAbs

BigInteger.prototype.compareAbs = function(n)
Compare the absolute value of two BigIntegers1.
Calling compareAbs19 is faster than calling abs15 twice, then compare20.

Parameters

n The number to compare to this.  Will be converted to a BigInteger1.

Returns

-1, 0, or +1 if |this| is less than, equal to, or greater than |n|.

See Also

compare20, abs15

compare

BigInteger.prototype.compare = function(n)
Compare two BigIntegers1.

Parameters

n The number to compare to this.  Will be converted to a BigInteger1.

Returns

-1, 0, or +1 if this is less than, equal to, or greater than n.

See Also

compareAbs19, isPositive30, isNegative31, isUnit21

isUnit

BigInteger.prototype.isUnit = function()
Return true iff this is either 1 or -1.

Returns

true if this compares equal to BigInteger.ONE6 or BigInteger.MONE7.

See Also

isZero32, isNegative31, isPositive30, compareAbs19, compare20, BigInteger.ONE6, BigInteger.MONE7

multiply

BigInteger.prototype.multiply = function(n)
Multiply two BigIntegers1.

Parameters

n The number to multiply this by.  Will be converted to a BigInteger1.

Returns

The numbers multiplied together.

See Also

add13, subtract16, divide24, square23

square

BigInteger.prototype.square = function()
Multiply a BigInteger1 by itself.
This is slightly faster than regular multiplication, since it removes the duplicated multiplcations.

Returns

this.multiply(this)

See Also

multiply22

divide

BigInteger.prototype.divide = function(n)
Divide two BigIntegers1.
divide24 throws an exception if n is zero.

Parameters

n The number to divide this by.  Will be converted to a BigInteger1.

Returns

The this / n, truncated to an integer.

See Also

add13, subtract16, multiply22, divRem26, remainder25

remainder

BigInteger.prototype.remainder = function(n)
Calculate the remainder of two BigIntegers1.
remainder25 throws an exception if n is zero.

Parameters

n The remainder after this is divided this by n.  Will be converted to a BigInteger1.

Returns

this % n.

See Also

divRem26, divide24

divRem

BigInteger.prototype.divRem = function(n)
Calculate the integer quotient and remainder of two BigIntegers1.
divRem26 throws an exception if n is zero.

Parameters

n The number to divide this by.  Will be converted to a BigInteger1.

Returns

A two-element array containing the quotient and the remainder.
a.divRem(b)
is exactly equivalent to
[a.divide(b), a.remainder(b)]
except it is faster, because they are calculated at the same time.

See Also

divide24, remainder25

isEven

BigInteger.prototype.isEven = function()
Return true iff this is divisible by two.
Note that BigInteger.ZERO5 is even.

Returns

true if this is even, false otherwise.

See Also

isOdd28

isOdd

BigInteger.prototype.isOdd = function()
Return true iff this is not divisible by two.

Returns

true if this is odd, false otherwise.

See Also

isEven27

sign

BigInteger.prototype.sign = function()
Get the sign of a BigInteger1.

Returns

-1 if this < 0 0 if this == 0 +1 if this > 0

See Also

isZero
32, isPositive30, isNegative31, compare20, BigInteger.ZERO5

isPositive

BigInteger.prototype.isPositive = function()
Return true iff this > 0.

Returns

true if this.compare(BigInteger.ZERO5) == 1.

See Also

sign29, isZero32, isNegative31, isUnit21, compare20, BigInteger.ZERO5

isNegative

BigInteger.prototype.isNegative = function()
Return true iff this < 0.

Returns

true if this.compare(BigInteger.ZERO5) == -1.

See Also

sign29, isPositive30, isZero32, isUnit21, compare20, BigInteger.ZERO5

isZero

BigInteger.prototype.isZero = function()
Return true iff this == 0.

Returns

true if this.compare(BigInteger.ZERO5) == 0.

See Also

sign29, isPositive30, isNegative31, isUnit21, BigInteger.ZERO5

exp10

BigInteger.prototype.exp10 = function(n)
Multiply a BigInteger1 by a power of 10.
This is equivalent to, but faster than
``` javascript if (n >= 0) {
return this.multiply(BigInteger("1e" + n));
} else { // n
return this.divide(BigInteger("1e" + -n));
} ```

Parameters

n The power of 10 to multiply this by.  n is converted to a javascipt number and must be no greater than BigInteger.MAXEXP38 (0x7FFFFFFF), or an exception will be thrown.

Returns

this (10 n), truncated to an integer if necessary.

See Also

pow
34, multiply22

pow

BigInteger.prototype.pow = function(n)
Raise a BigInteger1 to a power.
In this implementation, 00 is 1.

Parameters

n The exponent to raise
this by.  n must be no greater than BigInteger.MAXEXP38 (0x7FFFFFFF), or an exception will be thrown.

Returns

this raised to the nth power.

See Also

modPow35

modPow

BigInteger.prototype.modPow = function(exponent, modulus)
Raise a BigInteger1 to a power (mod m).
Because it is reduced by a modulus, modPow35 is not limited by BigInteger.MAXEXP38 like pow34.

Parameters

exponent The exponent to raise this by.  Must be positive.
modulus The modulus.

Returns

this ^ exponent (mod modulus).

See Also

pow34,

valueOf

BigInteger.prototype.valueOf = function()
Convert a BigInteger1 to a native JavaScript integer.
This is called automatically by JavaScipt to convert a BigInteger1 to a native value.

Returns

parseInt(this.toString(), 10)

See Also

toString11, toJSValue37

toJSValue

BigInteger.prototype.toJSValue = function()
Convert a BigInteger1 to a native JavaScript integer.
This is the same as valueOf, but more explicitly named.

Returns

parseInt(this.toString(), 10)

See Also

toString11, valueOf36

Constants

MAXEXP

The largest exponent allowed in pow34 and exp1033 (0x7FFFFFFF or 2147483647).
[1]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.BigInteger
[2]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger
[3]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.Functions
[4]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.Constants
[5]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.ZERO
[6]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.ONE
[7]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.M_ONE
[8]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger._0
[9]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger._1
[10]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.small
[11]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.toString
[12]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.parse
[13]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.add
[14]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.negate
[15]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.abs
[16]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.subtract
[17]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.next
[18]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.prev
[19]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.compareAbs
[20]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.compare
[21]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.isUnit
[22]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.multiply
[23]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.square
[24]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.divide
[25]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.remainder
[26]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.divRem
[27]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.isEven
[28]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.isOdd
[29]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.sign
[30]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.isPositive
[31]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.isNegative
[32]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.isZero
[33]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.exp10
[34]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.pow
[35]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.modPow
[36]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.valueOf
[37]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.toJSValue
[38]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html#BigInteger.MAX_EXP
[39]: http://www.naturaldocs.org
[40]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html/index/General.html
[41]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html/index/Classes.html
[42]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html/index/Constants.html
[43]: http://silentmatt.com/biginteger-docs/files/biginteger-js.html/index/Functions.html