56

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
  • 18,628
  • 6
  • 71
  • 79
David Cary
  • 5,001
  • 6
  • 52
  • 63
  • 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

2 Answers2

38

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,440
  • 26
  • 32
  • 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
1

Big Number.

Babiker
  • 17,652
  • 27
  • 74
  • 122
  • This will work great for my application. (Although technically this library can't exactly represent 1/7, it comes close enough for my application). – David Cary Jun 21 '10 at 08:03
  • 2
    This library's internal representation is to store the number as a decimal string. This is very slow for larger numbers and operations. – jnnnnn Mar 14 '12 at 01:08
  • Babiker, forgive me from taking away your big green "answered" checkmark and giving it to jnnnnn. jnnnnn's answer actually does represent 1/7 exactly. – David Cary Jun 05 '12 at 16:35
  • 2
    Seriously there is a bug. Try `alert(new BigNumber(100).divide(1));` – Pacerier Oct 28 '13 at 15:29
  • precision is incorrect... 123.789 rounds off to 123.78 (with round half up) :( – Atharva Johri Oct 29 '13 at 11:40
  • 11
    Not to be rude, but this buggy library is going to subtly hurt more people than it helps... – Pacerier Oct 31 '13 at 16:56
  • I don't know why, but I opened this page for 2 seconds and hated it instantly, now reading the comments I guess it was for good reason :P – MickLH Nov 23 '13 at 13:20
  • Out of 700+ votes it's rated 1.27 out of 5... – Brian Burns Apr 14 '16 at 21:01