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

1

58

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?

Dinosaurian answered 18/6, 2010 at 18:47 Comment(4)
I don't quite understand your question, first you ask for a bignum library, then you provide two on your own? :)Poul
When I said "exact-rational-arithmetic", I meant "can exactly represent numbers like 1/7". The two libraries I mentioned, as far as I can tell, can't do that -- they can only handle integers.Dinosaurian
There is a BigRational.js library for exact rational arithmetic.Griffith
@PeterOlson: Thank you, that project (started in 2013) looks like exactly what I was looking for in 2010. Perhaps I should have started such a project myself back then, rather than assuming that surely someone else has already started such a project?Dinosaurian
T
39

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.

Tropic answered 18/6, 2010 at 18:47 Comment(3)
This answer is being discussed on Meta.Taryn
Not an enlightening discussion. I'm quite a bit frustrated that it seems "illegal" to give an answer to this question which appears more than legit to me.Acromion
So that is on all firefox versions or starting from a particular one?Detumescence

© 2022 - 2024 — McMap. All rights reserved.