57

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?

Charles Merriam
  • 19,908
  • 6
  • 73
  • 83
David Cary
  • 5,250
  • 6
  • 53
  • 66
  • I don't quite understand your question, first you ask for a bignum library, then you provide two on your own? :) – Skurmedel Jun 18 '10 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. – David Cary Jun 21 '10 at 06:58
  • 1
    There is a [BigRational.js](https://github.com/peterolson/BigRational.js) library for exact rational arithmetic. – Peter Olson Nov 12 '14 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? – David Cary Nov 15 '14 at 17:05

1 Answers1

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.

jnnnnn
  • 3,889
  • 32
  • 37
  • 1
    This answer [is being discussed on Meta](https://meta.stackoverflow.com/questions/371529/this-post-has-been-locked-while-disputes-about-its-content-are-being-resolved). – halfer Jul 25 '18 at 22:09
  • 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. – Max Jun 14 '19 at 03:47
  • So that is on all firefox versions or starting from a particular one? – DRP Jan 17 '20 at 17:05