JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q35165F
  • License Apache-2.0

Do very large math!

Package Exports

  • bigarith.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (bigarith.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Welcome to BigArith.js

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

The name BigArith is short for Big Arithmetic i.e. a library that handles very large numbers (be it integers or fractions) to precision. In this article BigArith.js refers to the library and BigArith refers to the object.

Where can I use this library?

BigArith.js has been successfully tested on all modern browser. ===TODO===Updated supported browsers===

How do I include BigArith.js in my code?

There are two ways to include BigArith.js in your code.

  1. Include 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 targetted e.g. v1.0. Check versions for the latest version (the latest version is always recommended).
  2. Download the source from GitHub.com
    You can also download BigArith.js from releases on github.com (the latest version is always recommended).

Choose any method that best suit your need.

Initializing the BigArith

BigArith can be initialized in seven ways.

1. Initiating without any parameter or null
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
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 (with is one of the problems BigArith.js aim at solving). Doing this var ba = new BigArith(0.45); might still be considered "safe" but some can be tempted to do this var ba = new BigArith(0.10.2);. As it is known 0.10.2 will not give 0.02 in JavaScript but rather 0.020000000000000004. Therefore, it is better to avoid initializing fractional numbers this way all together.

It is recommended fractional numbers are initialized with strings. See here.

3. Initiating with string
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 fracton. 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
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 and 200 length of mantissa (yea, I know, that is awesome 😜). A negative number should start with the word "negative", a positive number can start with the "postive" word but this can be outrightly omitted. The mantissa part (part after the decimal point) 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
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
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")

How the BigArith works

valueOf() method

The valueOf() 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.valueOf());//this outputs "-5637865.32" to the console

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 1005 or the length of the mantissa part (part after the decimal point) is greater than 200, a RangeError is thrown.

var ba = new BigArith(1e3);
console.log(ba.toWords());//this outputs "one thousand" to the console

Below are the lists of currently supported functions:

  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(),
  21. subtract(),
  22. toFixed(),
  23. toWords(),
  24. valueOf()
  25. truncate()
  26. isPrime()
  27. negate()