GithubHelp home page GithubHelp logo

alexbardas / bignumber.js Goto Github PK

View Code? Open in Web Editor NEW
62.0 1.0 22.0 44 KB

Arithmetic operations on big numbers

Home Page: http://alexbardas.github.io/bignumber.js

License: MIT License

JavaScript 100.00%

bignumber.js's Introduction

BigNumber.js

Build Status

BigNumber.js is a light javascript library for node.js and the browser. It supports arithmetic operations on Big Integers.

It is build with performance in mind, uses the fastest algorithms and supports all basic arithmetic operations (+, -, *, /, %, ^, abs). Works with both positive and negative big integers.

: (http://alexbardas.github.io/bignumber.js/)

Install: npm install

Test: npm test

Usage:

  • in node:
	var BigNumber = require('big-number');

    BigNumber(5).plus(97).minus(53).plus(434).multiply(5435423).add(321453).multiply(21).div(2).pow(2);
    // 760056543044267246001
  • in the browser:
	<script src ="big-number.js"></script>

    n(5).plus(97).minus(53).plus(434).multiply(5435423).add(321453).multiply(21).div(2).pow(2);
    // 760056543044267246001

API

Supported methods: add/plus, minus/subtract, multiply/mult, divide/div, power/pow, mod, equals, lt, lte, gt, gte, isZero, abs

Addition
	BigNumber(2).plus(10); // or
	BigNumber(2).add(10);
Subtraction
	BigNumber(2).minus(10); // or
	BigNumber(2).subtract(10);
Multiplication
	BigNumber(2).multiply(10); // or
	BigNumber(2).mult(10);
Division
	BigNumber(2).divide(10); // or
	BigNumber(2).div(10);
Modulo
	BigNumber(53).mod(14);
Power
	BigNumber(2).power(10); // or
	BigNumber(2).pow(10);

bignumber.js's People

Contributors

alexbardas avatar nathsou avatar

Stargazers

Cai Huanyu avatar Carmine DiMascio avatar Bogdan Melnichenko avatar TrasherDK avatar Fyodor Zima avatar  avatar  avatar sine avatar j[E]sMaM[o]M avatar  avatar lixiangyu avatar  avatar  avatar ruowind avatar abc avatar jack avatar zhBoSir avatar blueseal avatar  avatar Hejx avatar  avatar F__B__ avatar  avatar  avatar mars avatar huanghui avatar  avatar 糸色望了么 avatar im niuniu avatar  avatar  avatar moalex avatar tpkeeper avatar Ataris avatar  avatar Abhishek Gahlot avatar Burhanuddin Rampurawala avatar skyking avatar  avatar tiansin avatar cccc avatar Danilo Sampaio avatar Wang Zhilong avatar Cristian Cortez avatar Hugo Nogueira avatar Austin Formica avatar Petr Grishin avatar  avatar  avatar Sheldon avatar Jeff Auriemma avatar Florian Wendelborn avatar  avatar Dung Nguyen avatar Andrea Parodi avatar Alexandru Topliceanu avatar Mike Rapadas avatar David Manning avatar Mike Vegeto avatar Kai Davenport avatar Joe Pea avatar  avatar

Watchers

TrasherDK avatar

bignumber.js's Issues

gt method

There seems to be a typo in the gt() method.
You wrote

return this._compare(n);

instead of

return this._compare(n) > 0;

x.mod(BigNumber(1)) should be BigNumber(0) not 0

Expected:

const B = require('big-number');
B(4).mod(B(2)) // => B(0)
B(4).mod(B(1)) // => B(0)

Actual

const B = require('big-number');
B(4).mod(B(2)) // => B(0)
B(4).mod(B(1)) // => 0

This bug or special case makes mod very difficult to use generally. For example, this gcd algorithm is incorrect (crashes on some inputs, say gcd(2, 1)) because sometimes b will become a non-BigNumber and the equals call will fail:

function gcd(a, b) {
    while (! b.equals(0))
        [a, b] = [b, B(a).mod(b)];
    return a;
}

Typing big numbers.

How do we type big numbers?

Suppose I want to do the following.

BigNumber(7504438520191923456742887157841797954091357871698459738726187123489081745879172349)
    .plus(82357194235682371485692837452359245029452458945824812464614761874618746246);

Will that work? Or will those numbers lose precision as they get passed in? If it doesn't work, how would I type a large number?

BigNumber.power

BigNumber cannot pow 2 in really big number
9102164759468381021953593365493425147953415786330604127979781307297018289684701351978874692561774241258006466985527199333230386260112185719711130493617466006894611261945149781122783154466271361602571305614274526553292486854556445656296737770747669496329255006743707191162031903696264682170651788310431942758977617150342742100628662013899453602674190733871565107696151741478167846683408078279042334729776544366054791125547197964325564045346293768343258985308496756480456959529045251483125906931501693938425835478819711297849477016348169042767967139540767502485512127811772229686337456501876079337226739856554648465913159447588509974154252882400651687809943824373050459590762127517309755807867849450221231038955913955148747002135471699099952156828902473305426167374799810170548337589648772695229363491212027550003633731190168298677587894670994480082304403339651075869957309194239495968978621321057834847463526207742589944178672503555332802645242280257118452676081873905309705264398272923762940917552330712505112342349139610254681532358865393632862861560243251844830353823240580413160490349949586137624530589212696956904339298097461460120470625335023396829225088733106063884625596929143426413308079585685253116562220242244351768607551526492470398398424513781534011864748758496253270216170737583456041673887090370908946016456191233492606407023272789706837135915688836967394124762577850029390106719264555161721475576604321519344492908577613842815135716661015145666079727321332448758176221403491091726021605188697696053666394232122175762233919512033408962531327195403473633526127534469353160750084877037271072787404534424910380893511649081055999455439640952493261686389663244961164078536344324103441480849469346668089490981833879341217014987508018853700737051707029249965879786408274890556951368300981573635333973579521452600850331430936972740054889789803544733641380105999051392960197506912096370028534872645067279222186240926154639406465069689865145032427929018252489835814228062563839362389322230260610483042185004154649370582862000643142664253578707789857944144471349379880766363149001261193596773952565835771847539893969023781249318697955518383556314051872144702697976507837655966428234856438800821674731102147302188772899124928844054810823401592893412119842825847638559630795672679407733596758656764882546469756530544412139562393858815486176002625827342885165280683632344990243509938960994377803741805056539160015943519537384898526234052187830705967469169039498147162113770633297847487224910499332150943892641005846819585785733390035971977323736952743309888663211124317641924284088787937357296098644240761709673067552405563334774638434789307986536239598450094284799805685756181737196631577901584830590228534626389308001764539773149696246078887791606975581990062769539751693644729437676344556070383789425659691133768443793060344918422497713104788739946265854020153426452413402664755643287488233768986120296922300709059556602921503239659095686529328944354449370839072895586004074105983313018033104352084590848620591092364171575220631561696978521654478715952848407330008130218907758116185521223948332066973361732606821913053172824963133984877280485663800932321359431169532011048179865531999998567111936031499703656441486482489996625751635624647112598534579391268355816323413932555108329931315832758235283562990166618605692911581309565244312247335275169449419272092495004342796589739599389975825999359398040844889993918846670366623646295684333182547651344543675977165382860268816123701217626605179236301839400769255937161996054354602367605140007290855108056568795559753419392933042725532465833100627738107973514228547509736846986582759142527928040116070687589390215384594135674532981007539308987567891139053993959490965209842423610270930870196293597870694306260531064663658124359833518255834436095050273089009579816986641961309585950835275140521142712476561485275351785602210434494053843228526373959914441900222279890283250754864574185422107110449012558332570712361955314275757323942765757324737058260023936875491142147581607411668545051576838567949904560378872026834204379744957248211053035091291204822436135492540644787675071619486168989387206214972834841650429996913133343710014546793538008411950105865428792389514089511090574118039653100402341216546119529263114998384977654554312381802053521617295690165025899177412216336662281374970358160631882120904580073901430654678489574422039287032134800946948132940132981853111198201328993477399284778844816826619762292563123663694388967552018754425178210371062606650310750447609062477943980334383476409621708537387454273034643908229866525820942756529722740612432766384663672607860008442148838742564032841863683311713998579176694890563222163717899867594145019953670756554165477961632519720892640561987553730201447017924936501680387147131757898543735842314858622562363314390906830159523200195322033069136679834109822491716270862744353809542435742563506791594690274840815480903350825914718843718541308158120060896826152357717151271494013491470355488379350482281654314316893430703972196351638959228841044571797268355146826828311338418705338487639307465163758427851551514097545311882619974274711539948870505841923077671506413152032942157710664353503307333234994008864504532991268672875354315660059167483053171584696199139569506865369823854191664960516468864431790204493360228727871537165885281450935805276888018444249961493022068390279359553011513150071287898409193521049963007992318930935918739713641183405312045950487016241576734509767709722936110514535060115377464446928701802989895154616819155092313837531022868555117061198145455708523290614568590783607135616277583055210066331564920056999866191370106620916481506601176602468919865562489608653993149593601772052035111536019933213443947217194395107650297659382890697111456434523641245788587360276487307532690652987169815030068296401964546060790862856426178479268367210434539188812811512149412640255474199278173040179446661415581750202992227615845257258714268623603506390900783104443138721871859884013414042065936548036425566186070226744883505319456607061109821394293133528740414570677479093031183671515548854824669362422074875150175489610256127241926030895518587075737400021122069321978677930720581568542801006609322700521685851817410704111475271184897847866253550642099716504218784176767859716071957121523723854772071272568665272802922202986221111834457193819507953314492451300201334159157183546825913015759281070217321902147856487430361166421914926172375122792706599599561988199795146666559979478665873398426507664179161142854542163128402539826298577514803870142614715964204234128154918973034772925473393966922910095109295884854297332092441475588111062446179775636461630445142377706247408142528756104971287367737404072423964942310875318130572949472669192012027763318451842606043628611495399190562455451263964229830277172904258470302448745457481121324829294365620541458785229328217110398445114539513144302606249561735161215055689939549404165568296291759684165924731374135861981288538389035064747807480591633664199231275930372372105019306071383805851617654763040321540134542299835222310282059332601085855371203690843747743200642573776603831116415835149468322389670436399763036233016726019226438430203126768811653541237675773681039285524853879129600563206995777683573312991165906455950271220932639704628257868559934601296347377323932773055867964696140101598631073094068330623553320676412255324485267245484291480779247086380672684542235526803161185555016954497173170044269001063826301660978613547299044080213491143639208621075492624278317850410114917722618518413974963820015123922219925967848667888315198182099525183409943637284711907672687232588326068206934249255038377888772224028545550255145720146404359787170486245820239862877113014472324927276867714575025818226393920095726993387301909851313187508937846251131260572894736908179438251424017128532584429661612555160405559820562928730064429414721173871543020973618058776050714094210243819396739853223784908862919181351676910796343845643554631711498703692235829652881440616640165042725900901675842144954153231785759486381213217442634961978748708661021467832664078250008695801504719436737524199991429024558687464313426121489091673973085795021652512147485476279731052083438598419462274226821662224314746138286807489629699916556261074171709814846097767945719009941433629022525209965000710545426674576890203230632573209677327448828539863843105914391982212859054076224617701147591087013165651541830228295750547285129918670685867703188115435239474432445049257561614207640973405066229778792870117814045291449263606023741573125943959537045408326849919572531735135871031208692547259042566451667758866246315898229297234918540470534856034032348161214197008252108611405156745270063647707586841045304387392319796282884132332592031447501248335593977263855500661257083220452356775058551299963081362159458353753858270933350335612287643567366744737944501042451445371976289943145178483652821043783185704924852764395448550109941853700957633066418368576971716108111899470245473890265256959689941014666631690709152605866346875458619249912335213805269685166379531036337464852570932075806005697619770146785021525712740347957563356309738543347840619319581563004484182144522779883531403010831620683752537626946659079713986208752672393636577785243958120040993497836735534317133784728642574400190430788747155883667312410509772243089391369054216911191964562376246675015965232806152981148275013153840891508382402483782920213322345284922118432612597725410799262492621240695889404062165184345707504329034402151306061773965344875650538590408134561559760787286147898241366465774060096851930264381465314132898301790715060125936067292209191712774736759389913984518351542865249450125522365689699391320293439250552677569652377827231243752328001213086467621814145502036763632193826192571896729389943537172974610902003098842732768859188622224625580282179321962199144154482473485415404311253937878789312463923410522765700988544260531197189833722355015157867619399016016004339369525513222088855846715236

try yourself

Weird behavior on comparison

> let bn = require('big-number')
> let a = bn('5929390')
> let b = bn('852523')
> aa < bb
true

Super weird. The comparison is wrong.

Gives Wrong Number In 20 Digits

I tried to calculate this number:
304,153,525,784,175,760 * 82
and it gived me:
24,940,589,114,302,410,680
while the currect result is:
24,940,589,114,302,412,320
(the difference is in the last 4 digits).

_compare to 0 ?

Maybe I'm missing something, but the first line in _compare checks for (!n). This is causing an immediate false return if I try to compare anything to zero. It seems like it should be something like (typeof n === "undefined").

.equals(undefined) returns true

.equals(undefined) returns true. It should return false (or throw an exception).

var BigNumber = require('big-number');
var a = BigNumber(10);
console.log(a.equals(undefined) ? 't' : 'f'); // outputs 't'

var b = 10;
console.log(b == undefined ? 't' : 'f'); // outputs 'f'

BigNumber(0).mod(x) should be BigNumber(0) not 0

Expected:

const B = require('big-number');
B(0).mod(12) // => B(0)
B(16).mod(12) // => B(4)

Actual

const B = require('big-number');
B(0).mod(12) // => 0
B(16).mod(B(12)) // => B(4)

This makes it very difficult to use. For example the following code throws an exception because gt is not a function:

const B = require('big-number');
const a = B(0), b = B(a);
if (b.mod(12).gt(6)) {
   console.log(`${a} > ${b} x 12 + 6`);
} else {
   console.log(`${a} <= ${b} x 12 + 6`);
}

7601.11229246 invalid number

I cannot store bitcoin balance because bigNumber("7601.11229246") this is invalid number.

What is the workaround ? multiply ?

where

var test = 7601.11229246
console.log(test) //=> 7601.11229246

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.