Big Integer Javascript

Note: This post is over 9 years old. The information may be outdated.

JavaScript's Integer has a limit of up to 18437736874454810627 (that is, 2^64-2^53+3). To handle much larger values with high performance, we can use the Big Integer library.

Big Integer allows performing basic arithmetic operations such as addition, subtraction, multiplication, division, and comparison with unlimited Integer values, as long as you have enough RAM.

Installation

Using Node.js and NPM:

npm install big-integer

Running in Browsers:

<script src="http://peterolson.github.com/BigInteger.js/BigInteger.min.js"></script>

Usage in code:

var bigInt = require('big-integer')

Usage

var bigInt = require('big-integer')

// Different ways of declaring
var zero = bigInt()
var ninetyThree = bigInt(93)
var largeNumber = bigInt(
  '75643564363473453456342378564387956906736546456235345',
)
var googol = bigInt('1e100')

var a =
  '218093290301725279849294552737145306526429515095910348328784867259719961681262317171990366018836471452273795738712382654617011499370332067465452153429131133154474494728461513797156576311424209209825768452476998761186844896333150192092696406370188813135474544186922431865203259468892782696696554856807492686240273426580684476908600903286664578178500293562463803241679236095343405558144595606432072340054'
var b =
  '759453421168594746297585634824794585057708073795685055048450994660667302169842440997187780071628842999365618342370044730249364762070171939525787172608446535167458760784909718498489041640160903072085566658168644606091524276643802085431070120034640336353069020700940598038997582524596177336508'
var c

// a + b
c = bigInt(a).add(b)
console.log(c.val()) // => 218093290301725279849294552737145306526429515095910348328784867259719961681262317171990366018836471452273795739471836075785606245667917702290246738486839206950159549776912508457823878481266650207013548524105841760552463238703194922342061168440360752661261716795368967032662020253802501195185596496968395758325840084749329083000125179930466663609570413597104139594748256796284003597142178131028249676562

// a - b
c = bigInt(a).subtract(b)
// or
c = bigInt(a).minus(b)

console.log(c.val()) // => 218093290301725279849294552737145306526429515095910348328784867259719961681262317171990366018836471452273795737952929233448416753072746432640657568371423059358789439680010519136489274141581768212637988380848155761821226553963105461843331644300016873609687371578475896697744498683983064198207513216646589614154706768412039870817076626642862492747430173527823466888610215394402807519147013081835895003546

// a * b
c = bigInt(a).multiply(b)
console.log(c.val()) // => 165631695453560768931354179676327783789554654471289094267396314999551619350086706770637603163259782521350605065527228675367441009889563464536042123812298158775025038552756989906635218183772046533558978457853765293877902141330057087553963131601282691171759816841117292436049592274238065569156249246125691526163874951713797884657704497629753668671292281869762553374641310311774140912980126830919151808832669504364383900117665833031771105917192115442091637918088985032215601898962325376736104912045524501146768304386244267559527475259139329594399610587041338732488477674534017135328109239900803529659849632039837754817318175918697532072796924765669004218196032450409366708571087537016172564891432

// a / b
c = bigInt(a).divide(b)
console.log(c.val()) // => 287171384343938182302166283267955634005236318876904228926536131374862297188572994580923635532994904266143630527

// a mod b
c = bigInt(a).mod(b)
console.log(c.val()) // => 592025574073873421838644719778706564169852072684877302818315404363803007260660346662730591457239291807144570448333654917475379749402034885167586678975138436729309695698180319906813373242540831615098968972192135361399928745893872371373756948364150937205578085919887319866770529450275971960338

// a^2
bigInt(3).square() // => 9

// Equality comparison
a.equals(b) // false
a.eq(b) // false

// Even number
a.isOdd() // true|false

// Positive number
a.isPositive() // true
bigInt(-1).isPositive() // false

// Chain usage
c = bigInt('100')
  .add('50') // 150
  .sub('10') // 140
  .mul('2') // 280
  .div('11') // 25
console.log(c.val()) // 25

APIs

Big Integer supports various basic arithmetic operations such as:

  • .add(n) - Add n.
  • .sub(n) - Subtract n.
  • .mul(n) - Multiply by n.
  • .divide(n) - Divide by n.
  • .mod(n) - Modulo (remainder) of n.
  • .abs() - Absolute value.
  • .neg() - Negate the current big integer.
  • .cmp(n) - Compare to n, returning 1 if greater, 0 if equal, and -1 if less than n.
  • .lt(n) - If the big integer is less than n, returns true.
  • .lte(n) - Less than or equal to.
  • .gt(n) - Greater than.
  • .gte(n) - Greater than or equal to.
  • .square() - Square the number.
  • .shiftLeft(n) - Shift left.
  • .next() - The next number (add 1).
  • .isPrime() - Check if the number is prime.
  • ...

See the full list of supported functions here.

References

JavascriptJavascript