

How BigIntegers work in JavaScript - Amaan
http://silentmatt.com/blog/2011/10/how-bigintegers-work/

======
wbhart
Here is a javascript bigint library by Leemon Baird

<http://www.leemon.com/crypto/BigInt.js>

It's about the best I've seen and embodies all the principles that are being
explained by Matt.

------
euroclydon
Seriously, for the number -123, the array [3, 2, 1] has the least significant
digit in position zero?

[edit] Ah ha, printed arrays have position zero on the left. I thought it was
on the right.

~~~
wbhart
That's quite standard. The sign can be taken care of separately if one wishes.

The reason for putting the least significant digit first is that carries
accumulate towards the most significant digit in ordinary arithmetic. Thus
many of the simplest operations want to work from least significant to most
significant digit. A final carry out at the top might add an extra digit to
the result. It would be really annoying to work with arrays that had most
significant digit first. If you had an extra digit you'd have to shift the
entire array one place to accommodate it!

------
hardy263
So does this mean using biginteger for small numbers could require almost 30x
more memory?

~~~
wbhart
A serious bignum library wouldn't use base 10. I understood that he intended
that you use base 10^15 or something like that. And even that was only by way
of illustration.

A more standard approach when using doubles would be to use base 2^32. This is
important if you want to implement a Schoenhage-Strassen FFT, which certainly
makes use of the fact that the base is a power of 2.

~~~
silentmatt
It's actually base 10^7 :) The reason for that is that at the lowest level,
the _result of any operation_ on a single digit needs to fit in a JavaScript
Number. Since you sometimes have to multiply two digits and 64-bit floats have
about 15 digits of precision, the largest power of 10 that will never lose
precision is 10^7 (because 10^7 * 10^7 = 10^14).

Of course, it would be more efficient to use a power of two, but using a power
of 10 makes converting back and forth between strings (in decimal) quite a bit
faster. In my original use case that was a large factor, so it's what I went
with.

~~~
wbhart
I understand.

Of course you can also multiply 32 x 32 -> 64 bits in Javascript. But of
course you need more than one operation to do it.

