Introduction
In this article, we will going to work on extremely large numbers to get precise answers
The problem
JavaScript has a limitation it only allows precision of about 16 digits for the number format.
Minimum & maximum value that a number can go to without losing precision is -2^53<= x <=2^53
, where 2^53 = 9007199254740992.
If we go beyond this limit then we'll lose precision.
console.log(1000000000000011112); // => 1000000000000011100
This impreciseness affect arithmetic operations as well.
console.log(10000000000000001+1) // => 10000000000000000
console.log(10000000000000002-1) // => 10000000000000000
console.log(10000000000000002*3) // => 30000000000000010
console.log(10000000000000001==10000000000000000) // => true
It is difficult to get answers from arithmetic operations of large integers.
The Solution
To solve this impreciseness, We have created a library called sateek.js
The word sateek means exact or precise in hindi language.
To use this library, Encode your large integer in a string format & call the functions provided by sateek.js
sateek.add("10000000000000001", "1"); // => 10000000000000002
sateek.subtract("10000000000000002", "1"); // => 10000000000000001
sateek.multiply("10000000000000002", "3"); // => 30000000000000006
It returns output in string format.
Installation
Sateek.js is available on github & npm or you can simply add it's CDN to your JavaScript file.
Node.js
1) To use this library, You need to install Node.js & npm.
2) Now run the following command in your project directory.
npm install --save sateek
3) Import sateek.js library in your project.
var sateek = require('sateek')();
JavaScript
1) Create a HTML file & write some code in it.
<html>
<body>
</body>
<script type="module">
import sateekModule from 'https://cdn.skypack.dev/sateek';
const sateek = sateekModule();
console.log(sateek.add("10000000000000002", "3"));
</script>
</html>
Usage
Sateek.js offers 5 functions.
1) add(n1,n2);
2) subtract(n1,n2);
3) divide(n1,n2);
4) compare(n1,n2);
Where n1 & n2 are two numbers encoded in string format.
Add
sateek.add("10000000000000001", "1"); // => 10000000000000002
Subtract
sateek.subtract("10000000000000002", "1"); // => 10000000000000001
Multiply
sateek.multiply("10000000000000002", "3"); // => 30000000000000006
Divide
sateek.divide("20000000000000022", "2"); // => 10000000000000011
Compare
sateek.compare("10000000000000001", "10000000000000000"); // => 1
sateek.compare("10000000000000000", "10000000000000001"); // => -1
sateek.compare("10000000000000001", "10000000000000001"); // => 0
if n1>n2, it returns 1
if n1<n2, it returns -1
if n1==n2, it returns 0
The Conclusion
Every library has it's own advantages & limitations. Here are some limitations of sateek.js library.
1) Sateek.js focuses more on precision as compare to efficiency.
2) It only work with integers.
3) Division operation only work when dividend is greater than divisor.
4) Division operation only returns quotient.
We have tested this library with large test cases, Still if you found any issue feel free to report it on github/npm or mail me at kalpitrathore@gmail.com.
Thank you for reading.
For daily updates like this, Follow me on twitter.
Top comments (3)
What is wrong with BigInt? Available since Node.js 10.4.0 (2018) and caniuse.
When I tried to compute the below it gives me two different "k" constant values when they should be identical. See below results.
I´ve also tried all bignumber libraries with the exact two differing results each time.
Any ideas on how to resolve this issue in JavaScript would be greatly appreciated!
Just use Lisp, or Clojure.