bigarith.js
offers a way to handle very large numbers (be it integers, fractionals, strings of digits, english words) to precision. bigarith is from the words big arithmetic.
var ba = new BigArith("two million three hundred and sixty nine point two five seven one nine");
console.log( ba.add("300164677834746574576636455846484578468383.968785364758348").subtract("58967883445567247657787456634534546650.0955344465644497").multiply("-323333256645735747677476657457673734399124.90745562122345555").divide("0.009899667856554544334565487673234").abs().square().toString());
/* This outputs
"96074475586197577621365133601838365203080754085195939359953075396624558104713187059409912271015743874101504692641365467550955268112648730652787010947788963291422897835924.2697158811425922569339591402860355619971451186954192254552990138752891749629006917912953942976702570634093674662564598087492412925971450372323378467910693943693505333234015230699695868364256999174194600745045683291593833019710855049245471924913674245229558192580612054808910477322678828203196181992794776861557204199127862657871835998341406371689589320661826402247662519900739514364587371169640378081"
to the console */
Check here for full documentation.
- Fixed a bug in div
Full Changelog here.
Depending on the environment in which bigarith.js will be used, it can be installed via:
Server-side usage
- Installing on node.js
npm install bigarith.js
Client-side usage
- Including the library from the rawgit.com CDN.
You can do that by adding<script src="https://cdn.rawgit.com/osofem/bigarith.js/<version tag>/bigarith.js"></script>
to your code. Replace<version tag>
with the version targeted e.g.v1.0.0
. Check versions for the latest version (the latest version is always recommended). - Downloading the source from GitHub.com
You can also download bigarith.js from releases on github.com (the latest version is always recommended). Extract the files and include the bigarith.js file in your work.
Choose the method that best suit your need.
In the server-side, always add the
var BigArith = require('bigarith.js');
however every other thing remains the same in both server-side and client-side.
Check documentation for the full list of supported functions.
bigarith.js
can be initialized in six ways.
var BigArith = require('bigarith.js');
var ba = new BigArith(); //initialize ba to a BigArith object of value "0"
var ba = new BigArith(null); //initialize ba to a BigArith object of value "0"
var ba = new BigArith(); //initialize ba to a BigArith object of value "0"
var ba = new BigArith(null); //initialize ba to a BigArith object of value "0"
This simply initialize the variable ba
to a BigArith
object of value "0"
.
var BigArith = require('bigarith.js');
var ba = new BigArith(12345); //initialize ba to a BigArith object of value "12345"
var ba = new BigArith(12345); //initialize ba to a BigArith object of value "12345"
The number must be between the Number.MIN_SAFE_INTEGER
(-9007199254740991) and Number.MAX_SAFE_INTEGER
(9007199254740991) limits else a RangeError
will be thrown. Please note that only integers are recommended for this method because of the floating point precision problem in JavaScript (which is one of the problems bigarith.js aim to solve).
Doing var ba = new BigArith(0.45);
might still be considered "safe" but some could be tempted to do var ba = new BigArith(0.1*0.2);
. As it is known 0.1*0.2
will not give 0.02
in JavaScript but rather 0.020000000000000004
. Therefore, it is better to avoid initializing fractional numbers this way.
It is recommended fractional numbers are initialized with strings. See here.
var BigArith = require('bigarith.js');
var ba = new BigArith("67876445565433556789877654567987457008645656765434567889086654234542126677.8977566766788767"); //initialize ba to a BigArith object of value "67876445565433556789877654567987457008645656765434567889086654234542126677.8977566766788767"
var bb = new BigArith(""); //initialize bb to a BigArith object of value "0"
var bc = new BigArith("-123"); //initialize bc to a BigArith object of value "-123"
var bd = new BigArith("+123"); //initialize bd to a BigArith object of value "123"
var be = new BigArith("123"); //initialize be to a BigArith object of value "123"
var ba = new BigArith("67876445565433556789877654567987457008645656765434567889086654234542126677.8977566766788767"); //initialize ba to a BigArith object of value "67876445565433556789877654567987457008645656765434567889086654234542126677.8977566766788767"
var bb = new BigArith(""); //initialize bb to a BigArith object of value "0"
var bc = new BigArith("-123"); //initialize bc to a BigArith object of value "-123"
var bd = new BigArith("+123"); //initialize bd to a BigArith object of value "123"
var be = new BigArith("123"); //initialize be to a BigArith object of value "123"
bigarith.js
accepts strings of digits. This can be of any length, can be negative, positive, integer, or fractional number. An empty string initializes to "0"
.
Strings that contains characters other than: digits 0
to 9
, -
or +
(at the start of the string), or .
(appearing just once), will evaluate to NaN
var BigArith = require('bigarith.js');
var ba = new BigArith("negative five million six hundred and thirty seven thousand eight hundred and sixty five point three two"); //initialize ba to a BigArith object of value "-5637865.32"
var bb = new BigArith("positive three"); //initialize bb to a BigArith object of value "3"
var bc = new BigArith("three"); //initialize bc to a BigArith object of value "3"
var bd = new BigArith("point two three seven"); //initialize bd to a BigArith object of value "0.237"
var ba = new BigArith("negative five million six hundred and thirty seven thousand eight hundred and sixty five point three two"); //initialize ba to a BigArith object of value "-5637865.32"
var bb = new BigArith("positive three"); //initialize bb to a BigArith object of value "3"
var bc = new BigArith("three"); //initialize bc to a BigArith object of value "3"
var bd = new BigArith("point two three seven"); //initialize bd to a BigArith object of value "0.237"
bigarith.js
accepts english words of up to (±1x10^1,005)-0.0000{insert 195 more zeroes}01 (i.e. nine hundred and ninety nine trecentretrigintillion point nine nine nine nine nine {insert 195 more "nine"'s}). That is 1,005 length of characteristic (parts before the decimal point) and 200 length of mantissa (parts after the decimal point).
*This limit only applies to when initializing with words, initializing with strings can be to any length.
A negative number should start with the word "negative"
, a positive number can start with the "positive" word but this can be outrightly omitted. The mantissa part should be spelt out after the word point
or else the word will evaluate to NaN
.
This is case insensitive and only Short Scale naming system is supported.
var ba = new BigArith("three point one two"); // This evaluate to "3.12"
var bb = new BigArith("three point twelve"); // This evaluate to NaN
var BigArith = require('bigarith.js');
var ba = new BigArith("PI"); // this evaluate to "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196"
var ba = new BigArith("PI"); // this evaluate to "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196"
bigarith.js
has a list of inbuilt constants which can be used for initialization. Check here for the updated list.
var BigArith = require('bigarith.js');
var ba = new BigArith("3"); //initialize ba to a BigArith object of value "3"
var bb = new BigArith(ba); //initialize bb to the value of ba (i.e. "3")
var ba = new BigArith("3"); //initialize ba to a BigArith object of value "3"
var bb = new BigArith(ba); //initialize bb to the value of ba (i.e. "3")
The toString()
method returns the value of the BigArith object as a strings of digits.
var ba = new BigArith("negative five million six hundred and thirty seven thousand eight hundred and sixty five point three two");
console.log(ba.toString());//this outputs "-5637865.32" to the console
The valueOf()
method returns the value of the BigArith object as a number.
var ba = new BigArith("negative five million six hundred and thirty seven thousand eight hundred and sixty five point three two");
console.log(ba.valueOf());//this outputs -5637865.32 to the console
NOTE: Use this function with caution as JavaScript numbers looses precision once it is greater than Number.MAX_SAFE_INTEGER or lesser than Number.MIN_SAFE_INTEGER and becomes "Infinity" when it is greater than Number.MAX_VALUE and "-Infinity" when it is less than Number.MIN_VALUE.
var ba = new BigArith("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999");
consoole.log(ba.valueOf()); //this outputs Infinity
consoole.log(ba.toString()); //this outputs "999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999"
The toWords
method returns the value of the BigArith object in English words using the Short Scale naming system. If the length of the object's characteristic part (part before the decimal point) is greater than 1,005 or the length of the mantissa part (part after the decimal point) is greater than 200, a RangeError
is thrown.
This limit only applies to the
toWords()
method function, toString() outputs the value of the BigArith object to any lenth as a string of digits.
var ba = new BigArith(1e3);
console.log(ba.toWords());//this outputs "one thousand" to the console
See also:
- abs()
- add()
- ceil()
- compare()
- compareAbs()
- divide()
- floor()
- isEven()
- isNegative()
- isOdd()
- isPositive()
- max()
- min()
- modulus()
- multiply()
- random()
- randomInt()
- round()
- square()
- squareRoot() [unstable]
- subtract()
- toFixed()
- toWords()
- toString()
- valueOf()
- truncate()
- negate()
- sin() [unstable]
- cos() [unstable]
- tan() [unstable]
Full documentation is here.