peterolson / BigInteger.js

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

TC39 proposal: extended BigInt Math methods #217

Closed js-choi closed 3 years ago

js-choi commented 3 years ago

I’ve written a proposal to extend several standard Math methods to accept BigInts. A few days ago, it reached Stage 1.

BigInteger.js is perhaps the most popular “polyfill” library of BigInts. I suspect that its authors might have useful insights into the proposal and its polyfillability. If there’s no apparent problems with it from BigInteger.js’s perspective, that’s fine too. Either way, in the future, the library might want to polyfill most of the proposed methods (except the rounding/identity functions). Thanks!

peterolson commented 3 years ago

The proposal looks fine to me. This library wouldn't be able to polyfill everything out of the box since it doesn't implement non-integer methods like sqrt and log

js-choi commented 3 years ago

Thanks for taking a look. Is there a technical reason why the potentially truncated methods like sqrt and log2 can’t be polyfilled, or is it just because they’re considered out of scope for this library?

peterolson commented 3 years ago

No, there's no technical reason that it's impossible to implement them. I just decided not to implement them since they seemed to be out of scope for a library focused on integers and integer operations.

It's certainly possible to implement sqrt or log2 in userland using existing methods if it's something that you need.

Yaffle commented 3 years ago

@js-choi , for not very optimized (but simple) sqrt you may look at https://github.com/nodef/extra-bigint/blob/ed932f2f2596a27a49febb594a9ff2c8e245f8ff/src/sqrt.ts

Yaffle commented 3 years ago

@js-choi log2:

function log2(a) {
  const s = BigInt(a).toString(16);
  const c = s.charCodeAt(0) - '0'.charCodeAt(0);
  if (c <= 0) {
    throw new RangeError();
  }
  return (s.length - 1) * 4 + (31 - Math.clz32(Math.min(c, 8)));
}