npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

biginteger

v1.0.3

Published

An arbitrarily-sized-integer library for JavaScript.

Downloads

14,659

Readme

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

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

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.

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

Functions

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.

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

Returns

A BigInteger value.

See Also

parse, 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.

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

See Also

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:

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

add

BigInteger.prototype.add = function(n)

Add two BigIntegers.

Parameters

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

Returns

The numbers added together.

See Also

subtract, multiply, divide, next

negate

> BigInteger.prototype.negate = function()

Get the additive inverse of a BigInteger.

Returns

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

See Also

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

See Also

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.

See Also

add, multiply, divide, prev

next

BigInteger.prototype.next = function()

Get the next BigInteger (add one).

Returns

this + 1.

See Also

add, prev

prev

BigInteger.prototype.prev = function()

Get the previous BigInteger (subtract one).

Returns

this 1.

See Also

next, subtract

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

See Also

compare, abs

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.

See Also

compareAbs, isPositive, isNegative, isUnit

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.

See Also

isZero, isNegative, isPositive, compareAbs, compare, BigInteger.ONE, 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.

See Also

add, subtract, divide, square

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)

See Also

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.

See Also

add, subtract, multiply, divRem, remainder

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.

Returns

this % n.

See Also

divRem, divide

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.

See Also

divide, remainder

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.

See Also

isOdd

isOdd

BigInteger.prototype.isOdd = function()

Return true iff this is not divisible by two.

Returns

true if this is odd, false otherwise.

See Also

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

See Also

isZero, isPositive, isNegative, compare, BigInteger.ZERO

isPositive

BigInteger.prototype.isPositive = function()

Return true iff this > 0.

Returns

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

See Also

sign, isZero, isNegative, isUnit, compare, BigInteger.ZERO

isNegative

BigInteger.prototype.isNegative = function()

Return true iff this < 0.

Returns

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

See Also

sign, isPositive, isZero, isUnit, compare, BigInteger.ZERO

isZero

BigInteger.prototype.isZero = function()

Return true iff this == 0.

Returns

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

See Also

sign, isPositive, isNegative, isUnit, BigInteger.ZERO

exp10

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
    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.MAX_EXP (0x7FFFFFFF), or an exception will be thrown.

Returns

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

See Also

pow, multiply

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.

See Also

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

See Also

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)

See Also

toString, toJSValue

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)

See Also

toString, valueOf

Constants

MAX_EXP

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