# npm

## biginteger

1.0.3 • Public • Published

"Permalink to BigInteger source"

# BigInteger

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.

Summary

BigInteger An arbitrarily-large integer.

Functions

BigInteger() Convert a value to a BigInteger.

Constants

_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.

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.

divide Divide two BigIntegers.

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).

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).

### BigInteger()

`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.

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.

#### Parameters

`n` Value to convert to a BigInteger.

#### Returns

A BigInteger value.

BigInteger -1.

### _0

Shortcut for ZERO.

### _1

Shortcut for ONE.

### small

Array of BigIntegers from 0 to 36.

These are used internally for parsing, but useful when you need a “small” BigInteger.

ZERO, ONE, _0, _1

## Functions

### toString

`BigInteger.prototype.toString = function(base)`

Convert a BigInteger 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 BigInteger.

### parse

`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:

• ”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:

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 BigInteger instance.

`BigInteger.prototype.add = function(n)`

#### Parameters

`n` The number to add to this.  Will be converted to a BigInteger.

#### Returns

The numbers added together.

### negate

> BigInteger.prototype.negate = function()

Get the additive inverse of a BigInteger.

#### Returns

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

abs

### abs

> BigInteger.prototype.abs = function()

Get the absolute value of a BigInteger.

#### Returns

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

negate

### subtract

`BigInteger.prototype.negate = function()`

Subtract two BigIntegers.

#### Parameters

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

#### Returns

The n subtracted from this.

### next

`BigInteger.prototype.next = function()`

Get the next BigInteger (add one).

this + 1.

### prev

`BigInteger.prototype.prev = function()`

Get the previous BigInteger (subtract one).

this 1.

### compareAbs

`BigInteger.prototype.compareAbs = function(n)`

Compare the absolute value of two BigIntegers.

Calling compareAbs is faster than calling abs twice, then compare.

#### Parameters

`n` The number to compare to this.  Will be converted to a BigInteger.

#### Returns

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

### compare

`BigInteger.prototype.compare = function(n)`

Compare two BigIntegers.

#### Parameters

`n` The number to compare to this.  Will be converted to a BigInteger.

#### Returns

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

### isUnit

`BigInteger.prototype.isUnit = function()`

Return true iff this is either 1 or -1.

#### Returns

true if this compares equal to BigInteger.ONE or BigInteger.M_ONE.

### multiply

`BigInteger.prototype.multiply = function(n)`

Multiply two BigIntegers.

#### Parameters

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

#### Returns

The numbers multiplied together.

### square

`BigInteger.prototype.square = function()`

Multiply a BigInteger by itself.

This is slightly faster than regular multiplication, since it removes the duplicated multiplcations.

#### Returns

`this.multiply(this)`

multiply

### divide

`BigInteger.prototype.divide = function(n)`

Divide two BigIntegers.

divide throws an exception if n is zero.

#### Parameters

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

#### Returns

The this / n, truncated to an integer.

### remainder

`BigInteger.prototype.remainder = function(n)`

Calculate the remainder of two BigIntegers.

remainder throws an exception if n is zero.

#### Parameters

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

this % n.

### divRem

`BigInteger.prototype.divRem = function(n)`

Calculate the integer quotient and remainder of two BigIntegers.

divRem throws an exception if n is zero.

#### Parameters

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

#### 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.

### isEven

`BigInteger.prototype.isEven = function()`

Return true iff this is divisible by two.

Note that BigInteger.ZERO is even.

#### Returns

true if this is even, false otherwise.

isOdd

### isOdd

`BigInteger.prototype.isOdd = function()`

Return true iff this is not divisible by two.

#### Returns

true if this is odd, false otherwise.

isEven

### sign

`BigInteger.prototype.sign = function()`

Get the sign of a BigInteger.

#### Returns

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

### isPositive

`BigInteger.prototype.isPositive = function()`

Return true iff this > 0.

#### Returns

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

### isNegative

`BigInteger.prototype.isNegative = function()`

Return true iff this < 0.

#### Returns

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

### isZero

`BigInteger.prototype.isZero = function()`

Return true iff this == 0.

#### Returns

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

### exp10

`BigInteger.prototype.exp10 = function(n)`

Multiply a BigInteger by a power of 10.

This is equivalent to, but faster than

#### Parameters

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.

#### Returns

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

### pow

`BigInteger.prototype.pow = function(n)`

Raise a BigInteger to a power.

In this implementation, 0**0 is 1.

#### Parameters

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

#### Returns

this raised to the nth power.

modPow

### modPow

`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.

#### Parameters

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

modulus The modulus.

#### Returns

this ^ exponent (mod modulus).

pow,

### valueOf

`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.

#### Returns

`parseInt(this.toString(), 10)`

### toJSValue

`BigInteger.prototype.toJSValue = function()`

Convert a BigInteger to a native JavaScript integer.

This is the same as valueOf, but more explicitly named.

#### Returns

`parseInt(this.toString(), 10)`

### MAX_EXP

The largest exponent allowed in pow and exp10 (0x7FFFFFFF or 2147483647).

## Keywords

### Install

`npm i biginteger`

### Repository

github.com/silentmatt/javascript-biginteger

### Homepage

github.com/silentmatt/javascript-biginteger

2,163

1.0.3