Understanding the limitations of the bignumber.js library

2.3k views Asked by At

I have recently found the bignumber.js library:

source: https://github.com/MikeMcl/bignumber.js

What I'm not sure of is if there is a limit to how big the number operations can be? I have been looking into the documentation, trying to find sources online. But nothing as of yet.

2

There are 2 answers

0
Piyin On

If you run this:

console.log(new BigNumber('123456789012345678901234567890').c);

You can see that numbers are stored as arrays, of maximum 14 digits

So the actual limitation would be the maximum length you can have for an array, multiplied by 14. This depends a lot on your machine, but assuming you have the best machine ever, ECMA-262 v6.0 says the limit is for the length of an array is 2^32-1, which is the size of ToUint32, so, theoretically, you could store a number with 14 * (2^32-1) digits using that library, that's something like:

console.log(new BigNumber(2).pow(32).sub(1).mul(14).toString());

Which is something around 60129542130 digits

0
Cornelius Fillmore On

I haven't worked with bignumber.js but a small peek into http://mikemcl.github.io/bignumber.js/#range has shown me that it seems to be configurable.

BigNumber.config({ RANGE: 500 })
BigNumber.config().RANGE     // [ -500, 500 ]
new BigNumber('9.999e499')   // '9.999e+499'
new BigNumber('1e500')       // 'Infinity'
new BigNumber('1e-499')      // '1e-499'
new BigNumber('1e-500')      // '0'