Socket
Book a DemoInstallSign in
Socket

js-big-integer

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

js-big-integer

Yet another class for arbitrary-precision integers in pure JavaScript. Small. Well tested.

latest
Source
npmnpm
Version
3.0.26
Version published
Weekly downloads
58
-83.38%
Maintainers
1
Weekly downloads
 
Created
Source

BigInteger

Yet another BigInteger class in JavaScript This library performs arithmetic operations on integers of arbitrary size.

To use it from a web browser:

<script src="BigInteger.js"></script>

To use it from the node.js:

npm install Yaffle/BigInteger

Then:

var BigInteger = require("js-big-integer").BigInteger;

The API is terrible, but small integers are stored as primitive numbers, so operations on small integers are faster. The API was updated to match the API provided by https://github.com/GoogleChromeLabs/jsbi

OperationBigIntegerNumberBigInt (https://github.com/tc39/proposal-bigint)
Conversion from StringBigInteger.BigInt(string)Number(string)BigInt(string)
Conversion from NumberBigInteger.BigInt(number)N/ABigInt(number)
Conversion to Stringa.toString(radix)a.toString(radix)a.toString(radix)
Conversion to Numbera.toNumber()N/ANumber(bigint)
AdditionBigInteger.add(a, b)a + ba + b
SubtractionBigInteger.subtract(a, b)a - ba - b
MultiplicationBigInteger.multiply(a, b)0 + a * ba * b
DivisionBigInteger.divide(a, b)0 + Math.trunc(a / b)a / b
RemainderBigInteger.remainder(a, b)0 + a % ba % b
ExponentiationBigInteger.exponentiate(a, b)0 + a**ba**b
NegationBigInteger.unaryMinus(a)0 - a-a
ComparisonBigInteger.equal(a, b)a === ba === b
...BigInteger.lessThan(a, b)a < ba < b
...BigInteger.greaterThan(a, b)a > ba > b
...BigInteger.notEqual(a, b)a !== ba !== b
...BigInteger.lessThanOrEqual(a, b)a <= ba <= b
...BigInteger.greaterThanOrEqual(a, b)a >= ba >= b
Signed Right ShiftBigInteger.signedRightShift(a, b)a >> ba >> b
Left ShiftBigInteger.leftShift(a, b)a << ba << b

Example


var factorial = function (n) {
  var result = BigInteger.BigInt(1);
  var i = 0;
  while (++i <= n) {
    result = BigInteger.multiply(result, BigInteger.BigInt(i));
  }
  return result;
};

console.log(factorial(30).toString(10));

Other pure JavaScript implementations:

Benchmark: http://yaffle.github.io/BigInteger/benchmark/

Keywords

BigInteger

FAQs

Package last updated on 17 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts