peterolson / BigInteger.js

An arbitrary length integer library for Javascript
The Unlicense
1.12k stars 187 forks source link

Why use 1e7 as radix for the internal representation? #162

Closed CidTori closed 5 years ago

CidTori commented 5 years ago

Are there any reason to choose that seemingly random number instead of, for example, Number.MAX_SAFE_INTEGER === 2^53-1 which should optimize memory use?

peterolson commented 5 years ago

Not only does the base have to be a safe integer, but with the way the library works, operations on digits have to also be safe integers.

That is, if I have digits a and b, a + b and a*b also have to be safe integers.

CidTori commented 5 years ago

Thanks for your quick answer, I didn't think about that. And just out of curiosity, why didn't you choose 94906265 (which is floor(sqrt(2^53-1))? And why did you choose a power of 10 instead of, for example, a power of 2?

peterolson commented 5 years ago

I chose the largest power of 10 below 94906265.

I chose a power of 10 because parsing a base-10 string and converting a big integer to a base-10 string are some of the most common operations users do. In base 94906265 some arithmetic operations might be slightly faster due to fewer digits (but not significantly fewer), but it would take much longer to convert base-10 number strings to and from base 94906265.

CidTori commented 5 years ago

It makes sense, thanks!