What is big-integer?
The big-integer npm package is designed for arithmetic and logical operations on large integers beyond the safe limit for JavaScript's native number type. It provides a way to work with integers of arbitrary size, allowing for precise calculations that would otherwise be impossible due to the limitations of standard number precision in JavaScript.
What are big-integer's main functionalities?
Arithmetic Operations
Demonstrates basic arithmetic operations such as addition, subtraction, multiplication, division, and modulo on large integers.
"use strict";\nconst bigInt = require('big-integer');\nlet x = bigInt('123456789012345678901234567890');\nlet y = bigInt('98765432109876543210987654321');\nlet sum = x.add(y);\nlet difference = x.minus(y);\nlet product = x.multiply(y);\nlet quotient = x.divide(y);\nlet remainder = x.mod(y);\nconsole.log('Sum:', sum.toString());\nconsole.log('Difference:', difference.toString());\nconsole.log('Product:', product.toString());\nconsole.log('Quotient:', quotient.toString());\nconsole.log('Remainder:', remainder.toString());
Logical Operations
Shows how to perform bitwise logical operations such as AND, OR, and XOR on large integers.
"use strict";\nconst bigInt = require('big-integer');\nlet x = bigInt('12345678901234567890');\nlet y = bigInt('9876543210987654321');\nlet andResult = x.and(y);\nlet orResult = x.or(y);\nlet xorResult = x.xor(y);\nconsole.log('AND:', andResult.toString());\nconsole.log('OR:', orResult.toString());\nconsole.log('XOR:', xorResult.toString());
Comparison Operations
Illustrates how to compare two large integers, checking for greater than, less than, and equality.
"use strict";\nconst bigInt = require('big-integer');\nlet x = bigInt('12345678901234567890');\nlet y = bigInt('9876543210987654321');\nconsole.log('x > y:', x.greater(y));\nconsole.log('x < y:', x.lesser(y));\nconsole.log('x == y:', x.equals(y));
Other packages similar to big-integer
bignumber.js
bignumber.js is a well-known library for arbitrary-precision decimal and non-decimal arithmetic. It's similar to big-integer but also supports decimal numbers, making it more versatile for applications requiring floating-point calculations.
decimal.js
decimal.js is another library for arbitrary-precision arithmetic, focusing on decimal numbers. It offers a similar range of operations as big-integer but is optimized for decimal arithmetic, providing high precision for financial and scientific calculations.
jsbn
jsbn is a library that implements big-integer arithmetic in pure JavaScript. It's similar to big-integer in its focus on large integers but is known for its use in cryptographic applications and for being part of the jsencrypt library.
BigInteger.js
BigInteger.js is an arbitrary-length integer library for Javascript, allowing arithmetic operations on integers of unlimited size, notwithstanding memory and time limitations.
If you are using a browser, you can download BigInteger.js from GitHub or just hotlink to it:
<script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script>
If you are using node, you can install BigInteger with npm.
npm install big-integer
Then you can include it in your code:
var bigInt = require("big-integer");
The unit tests are contained in the BigInteger.test.js
file. You can run them online from GitHub.
bigInt(number, [base])
You can create a bigInt by calling the bigInt
function. You can pass in
- a string, which it will parse as an bigInt and throw an
"Invalid integer"
error if the parsing fails. - a Javascript number, which it will parse as an bigInt and throw an
"Invalid integer"
error if the parsing fails. - another bigInt.
- nothing, and it will return
bigInt.zero
.
If you provide a second parameter, then it will parse number
as a number in base base
. Note that base
can be any bigInt (even negative or zero). The letters "a-z" and "A-Z" will be interpreted as the numbers 10 to 36. Higher digits can be specified in angle brackets (<
and >
).
Examples:
var zero = bigInt();
var ninetyThree = bigInt(93);
var largeNumber = bigInt("75643564363473453456342378564387956906736546456235345");
var googol = bigInt("1e100");
var bigNumber = bigInt(largeNumber);
var maximumByte = bigInt("FF", 16);
var fiftyFiveGoogol = bigInt("<55>0", googol);
Note that Javascript numbers larger than 9007199254740992
and smaller than -9007199254740992
are not precisely represented numbers and will not produce exact results. If you are dealing with numbers outside that range, it is better to pass in strings.
Method Chaining
Note that bigInt operations return bigInts, which allows you to chain methods, for example:
var salary = bigInt(dollarsPerHour).times(hoursWorked).plus(randomBonuses)
Constants
There are three constants already stored that you do not have to construct with the bigInt
function yourself:
bigInt.one
, equivalent to bigInt(1)
bigInt.zero
, equivalent to bigInt(0)
bigInt.minusOne
, equivalent to bigInt(-1)
Methods
abs()
Returns the absolute value of a bigInt.
bigInt(-45).abs()
=> 45
bigInt(45).abs()
=> 45
add(number)
Performs addition.
compare(number)
Performs a comparison between two numbers. If the numbers are equal, it returns 0
. If the first number is greater, it returns 1
. If the first number is lesser, it returns -1
.
bigInt(5).compare(5)
=> 0
bigInt(5).compare(4)
=> 1
bigInt(4).compare(5)
=> -1
compareAbs(number)
Performs a comparison between the absolute value of two numbers.
bigInt(5).compareAbs(-5)
=> 0
bigInt(5).compareAbs(4)
=> 1
bigInt(4).compareAbs(-5)
=> -1
compareTo(number)
Alias for the compare
method.
divide(number)
Performs integer division, disregarding the remainder.
bigInt(59).divide(5)
=> 11
divmod(number)
Performs division and returns an object with two properties: quotient
and remainder
. The sign of the remainder will match the sign of the dividend.
bigInt(59).divmod(5)
=> {quotient: bigInt(11), remainder: bigInt(4) }
bigInt(-5).divmod(2)
=> {quotient: bigInt(-2), remainder: bigInt(-1) }
equals(number)
Checks if two numbers are equal.
bigInt(5).equals(5)
=> true
bigInt(4).equals(7)
=> false
greater(number)
Checks if the first number is greater than the second.
bigInt(5).greater(6)
=> false
bigInt(5).greater(5)
=> false
bigInt(5).greater(4)
=> true
greaterOrEquals(number)
Checks if the first number is greater than or equal to the second.
bigInt(5).greaterOrEquals(6)
=> false
bigInt(5).greaterOrEquals(5)
=> true
bigInt(5).greaterOrEquals(4)
=> true
isEven(number)
Returns true
if the number is even, false
otherwise.
bigInt(6).isEven()
=> true
bigInt(3).isEven()
=> false
isNegative(number)
Returns true
if the number is negative, false
otherwise.
Returns false
for 0
and true
for -0
.
bigInt(-23).isNegative()
=> true
bigInt(50).isNegative()
=> false
isOdd(number)
Returns true
if the number is odd, false
otherwise.
bigInt(13).isOdd()
=> true
bigInt(40).isOdd()
=> false
isPositive(number)
Return true
if the number is positive, false
otherwise.
Returns true
for 0
and false
for -0
.
bigInt(54).isPositive()
=> true
bigInt(-1).isPositive()
=> false
lesser(number)
Checks if the first number is lesser than the second.
bigInt(5).lesser(6)
=> true
bigInt(5).lesser(5)
=> false
bigInt(5).lesser(4)
=> false
lesserOrEquals(number)
Checks if the first number is less than or equal to the second.
bigInt(5).lesserOrEquals(6)
=> true
bigInt(5).lesserOrEquals(5)
=> true
bigInt(5).lesserOrEquals(4)
=> false
minus(number)
Alias for the subtract
method.
mod(number)
Performs division and returns the remainder, disregarding the quotient. The sign of the remainder will match the sign of the dividend.
bigInt(59).mod(5)
=> 4
bigInt(-5).mod(2)
=> -1
multiply(number)
Performs multiplication.
bigInt(111).multiply(111)
=> 12321
next()
Adds one to the number.
notEquals(number)
Checks if two numbers are not equal.
over(number)
Alias for the divide
method.
plus(number)
Alias for the add
method.
pow(number)
Performs exponentiation. If the exponent is less than 0
, pow
returns 0
. bigInt.zero.pow(0)
returns 1
.
bigInt(16).pow(16)
=> 18446744073709551616
prev(number)
Subtracts one from the number.
subtract(number)
Performs subtraction.
bigInt(3).subtract(5)
=> -2
remainder(number)
Alias for the mod
method.
times(number)
Alias for the multiply
method.
bigInt(111).times(111)
=> 12321
toJSNumber()
Converts a bigInt into a native Javascript number. Loses precision for numbers outside the range [-9007199254740992, 9007199254740992]
.
bigInt("18446744073709551616").toJSNumber()
=> 18446744073709552000
Override Methods
toString()
Converts a bigInt to a string.
valueOf()
Converts a bigInt to a native Javascript number. This override allows you to use native arithmetic operators without explicit conversion:
bigInt("100") + bigInt("200") === 300; //true