peterolson / BigInteger.js

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

Endianness flag for toArray #183

Closed s1na closed 5 years ago

s1na commented 5 years ago

Hello,

We rely heavily on big numbers and are discussing switching to a library that polyfills to native BigInts, and that's how I found your great library.

Internally we mostly use big endian representation and If I'm not mistaken the toArray (and fromArray) methods work with little endian representation. I was wondering if you'd consider adding a flag for this to the relevant methods (or would accept a PR doing this).

Thanks

peterolson commented 5 years ago

Wouldn't it be pretty simple to just call .reverse() on the arrays? IMHO that would be a more readable way to change the order than passing in an extra boolean argument to the toArray/fromArray functions.

Also, toArray and fromArray are already big-endian, i.e. the most significant digit is the first element in the array.

bigInt(4).toArray(2)
/// {"value":[1,0,0],"isNegative":false}
bigInt.fromArray([1, 0, 0], 2).toString()
/// "4"
s1na commented 5 years ago

Sorry, I mistakenly got that impression from #39 (Note that the array representation is small endian).