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?

4
  • I don't quite understand your question, first you ask for a bignum library, then you provide two on your own? :) Commented Jun 18, 2010 at 18:52
  • 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. Commented Jun 21, 2010 at 6:58
  • 1
    There is a BigRational.js library for exact rational arithmetic. Commented Nov 12, 2014 at 23:41
  • @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? Commented Nov 15, 2014 at 17:05

1 Answer 1

40

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
So that is on all firefox versions or starting from a particular one?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.