big-int
v0.0.6
Published
Big Integer for JavaScript
Downloads
680
Readme
JavaScript Big-Int
- Author: oldj
- Blog: http://oldj.net
- Example: http://oldj.net/static/js-BIO/eg.html
This library is made for big integer operations in JavaScript, such as add, subtract, multiply, divide, mod, compare, etc.
The principle of this library is to simulate pen and paper operation, you can operate integers as big as your RAM allowed.
Install
npm install --save big-int
Example
var bigInt = require('big-int');
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).sub(b);
console.log(c.val()); // => 218093290301725279849294552737145306526429515095910348328784867259719961681262317171990366018836471452273795737952929233448416753072746432640657568371423059358789439680010519136489274141581768212637988380848155761821226553963105461843331644300016873609687371578475896697744498683983064198207513216646589614154706768412039870817076626642862492747430173527823466888610215394402807519147013081835895003546
// a * b
c = bigInt(a).mul(b);
console.log(c.val()); // => 165631695453560768931354179676327783789554654471289094267396314999551619350086706770637603163259782521350605065527228675367441009889563464536042123812298158775025038552756989906635218183772046533558978457853765293877902141330057087553963131601282691171759816841117292436049592274238065569156249246125691526163874951713797884657704497629753668671292281869762553374641310311774140912980126830919151808832669504364383900117665833031771105917192115442091637918088985032215601898962325376736104912045524501146768304386244267559527475259139329594399610587041338732488477674534017135328109239900803529659849632039837754817318175918697532072796924765669004218196032450409366708571087537016172564891432
// a / b
c = bigInt(a).div(b);
console.log(c.val()); // => 287171384343938182302166283267955634005236318876904228926536131374862297188572994580923635532994904266143630527
// a mod b
c = bigInt(a).mod(b);
console.log(c.val()); // => 592025574073873421838644719778706564169852072684877302818315404363803007260660346662730591457239291807144570448333654917475379749402034885167586678975138436729309695698180319906813373242540831615098968972192135361399928745893872371373756948364150937205578085919887319866770529450275971960338
// chain usage
c = bigInt('100')
.add('50') // 150
.sub('10') // 140
.mul('2') // 280
.div('11'); // 25
console.log(c.val()); // 25
APIs
.add(n)
add n.
.sub(n)
sub n.
.mul(n)
multiply by n.
.div(n)
divide by n.
.mod(n)
get the modulo by n.
.abs()
return the absolute value.
.neg()
return a new bigint with the negative of the instance value.
cmp(n)
compare with n
, return 1
if the instance value is greater than n
, 0
if they are equal, -1
if the instance value is less than n
.
lt(n)
if the instance value is less than n
.
lte(n)
if the instance value is less than or equal n
.
gt(n)
if the instance value is greater than n
.
gte(n)
if the instance value is greater than or equal n
.
Log
- 2016-07-20 Some change, and then publish to npm.
- 2012-10-13 The first version.
License
MIT