DEV Community

loading...
Cover image for Working with extremely large numbers in JavaScript

Working with extremely large numbers in JavaScript

kalpitrathore profile image Kalpit Rathore ・2 min read

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.

Discussion (2)

pic
Editor guide
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
patarapolw profile image
Pacharapol Withayasakpunt

Just use Lisp, or Clojure.