Closed js-choi closed 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
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?
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.
@js-choi , for not very optimized (but simple) sqrt you may look at https://github.com/nodef/extra-bigint/blob/ed932f2f2596a27a49febb594a9ff2c8e245f8ff/src/sqrt.ts
@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)));
}
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!