npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

bigarith.js

v0.0.8

Published

Do very large math to precision!

Downloads

11

Readme

Welcome to bigarith.js

Join the chat at https://gitter.im/BigArith-js/Lobby

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.

Chanelog for v0.0.8

  • Fixed a bug in div

Full Changelog here.

Install

Depending on the environment in which bigarith.js will be used, it can be installed via:

Server-side usage

  1. Installing on node.js
    npm install bigarith.js

Client-side usage

  1. 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).
  2. 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.

Usage

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.

Initialization

bigarith.js can be initialized in six ways.

1. Initiating without any parameter or null
Server-side
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"
Client-side
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".

2. Initiating with a number
Server-side
var BigArith = require('bigarith.js');
var ba = new BigArith(12345); //initialize ba to a BigArith object of value "12345"
Client-side
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.

3. Initiating with string
Server-side
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"
Client-side
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

4. Initiating with words
Server-side
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"
Client-side
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
5. Initiating with a constant
Server-side
var BigArith = require('bigarith.js');
var ba = new BigArith("PI"); // this evaluate to "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196"
Client-side
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.

6. Initiating with a BigArith object
Server-side
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")
Client-side
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")

Functions

toString() method

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

valueOf() method

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"

toWords() method

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:

  1. abs()
  2. add()
  3. ceil()
  4. compare()
  5. compareAbs()
  6. divide()
  7. floor()
  8. isEven()
  9. isNegative()
  10. isOdd()
  11. isPositive()
  12. max()
  13. min()
  14. modulus()
  15. multiply()
  16. random()
  17. randomInt()
  18. round()
  19. square()
  20. squareRoot() [unstable]
  21. subtract()
  22. toFixed()
  23. toWords()
  24. toString()
  25. valueOf()
  26. truncate()
  27. negate()
  28. sin() [unstable]
  29. cos() [unstable]
  30. tan() [unstable]

Full documentation is here.