DEV Community

Cover image for Working with extremely large numbers in JavaScript
Kalpit Rathore
Kalpit Rathore

Posted on

Working with extremely large numbers in JavaScript

Alt Text

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

3) Import sateek.js library in your project.



var sateek = require('sateek')();


Enter fullscreen mode Exit fullscreen mode
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>


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode
Subtract


sateek.subtract("10000000000000002", "1"); // => 10000000000000001


Enter fullscreen mode Exit fullscreen mode
Multiply


sateek.multiply("10000000000000002", "3"); // => 30000000000000006


Enter fullscreen mode Exit fullscreen mode
Divide


sateek.divide("20000000000000022", "2"); // => 10000000000000011


Enter fullscreen mode Exit fullscreen mode
Compare


sateek.compare("10000000000000001", "10000000000000000"); // => 1

sateek.compare("10000000000000000", "10000000000000001"); // => -1

sateek.compare("10000000000000001", "10000000000000001"); // => 0


Enter fullscreen mode Exit fullscreen mode

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)

Collapse
 
peerreynders profile image
peerreynders • Edited

What is wrong with BigInt? Available since Node.js 10.4.0 (2018) and caniuse.

console.assert((10000000000000001n + 1n).toString(), '10000000000000002');
console.assert((10000000000000002n - 1n).toString(), '10000000000000001');
console.assert((10000000000000002n * BigInt(3)).toString(), '30000000000000006');
Enter fullscreen mode Exit fullscreen mode
Collapse
 
ssadek1976 profile image
ssadek1976

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.

    // transaction
const tx0 = 2755738843649989653057n; // amount0Out BMON
const tx1 = 113747760332474227520n; // amount1In BUSD

// reserve after transaction
const x = 466822693713887341798087n; // reserve0 BMON
const y = 19334468154310748213608n; // reserve1 BUSD

// BigInt method
console.log("BigInt method...");
const k = x * y;
console.log("xy ", k); // 9025768505320715115682091222146517245881767896n

const newX = x + tx0;
const newY = y - tx1;
const newK = newX * newY;
console.log("xy ", newK); // 9025635755231009297480820885627169090307684672n
Enter fullscreen mode Exit fullscreen mode

Any ideas on how to resolve this issue in JavaScript would be greatly appreciated!

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt

Just use Lisp, or Clojure.