Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No documentation about asIntN() and asUintN() methods #167

Open
ppKrauss opened this issue Feb 2, 2019 · 2 comments
Open

No documentation about asIntN() and asUintN() methods #167

ppKrauss opened this issue Feb 2, 2019 · 2 comments

Comments

@ppKrauss
Copy link

ppKrauss commented Feb 2, 2019

Hi, there are some documentation about how BigInteger wraps the signal of a number?

As (2018) "BigInt is being added as a native feature of JavaScript. This library now works as a polyfill", is important to check how is orking about it.

See https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/BigInt#Methods

@Yaffle
Copy link
Collaborator

Yaffle commented Feb 3, 2019

Probably, could be implemented as:

bigInt.asUintN = function (bits, bigint) {
  bigint = bigInt(bigint);
  if (typeof bigint.value === "bigint") {
    return bigInt(BigInt.asUintN(bits, bigint.value));
  }
  const p2bits = bigInt(1).shiftLeft(bits);
  const mod = bigint.and(p2bits.subtract(1));
  return mod;
};
bigInt.asIntN = function (bits, bigint) {
  bigint = bigInt(bigint);
  if (typeof bigint.value === "bigint") {
    return bigInt(BigInt.asIntN(bits, bigint.value));
  }
  const p2bits = bigInt(1).shiftLeft(bits);
  const mod = bigint.and(p2bits.subtract(1));
  return mod.greaterOrEquals(p2bits.subtract(mod)) ? mod.subtract(p2bits) : mod;
};

If needed.

@peterolson
Copy link
Owner

If someone wants to add a pull request for this feature I'll merge it in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants