What is the standard solution in JavaScript for handling big numbers (BigNum)?

JavascriptFloating AccuracyBignum

Javascript Problem Overview


Is there a bignum built into JavaScript or browsers?

The alternate is loading an external library like

<script type="text/javascript" src="the_bignum_library.js"></script>

but that seems slow and may trigger a security warning.

I've considered basing my own off of http://github.com/silentmatt/javascript-biginteger or http://www.mainebrook.com/john/fun/euler.html. Alternately, is the solution to call into a Java bignum library such as apfloat?

Javascript Solutions


Solution 1 - Javascript

Update(2019-08-19): BigInt is now part of Firefox and Chrome; you no longer need a library:

const bigInt1 = 1111111111111111111111111111111n;
const bigInt2 = BigInt("1111111111111111111111111111111")
console.log((bigInt1 + bigInt2)+"")

Original answer:

If you need arbitrary-precision decimal numbers, use Javascript-bignum, as it is correct and fast.

Solution 2 - Javascript

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid CaryView Question on Stackoverflow
Solution 1 - JavascriptjnnnnnView Answer on Stackoverflow
Solution 2 - JavascriptBabikerView Answer on Stackoverflow