Reputation: 2053
I have two cases where BigInt misbehaves completely.
For completeness I attach the documentation of BigInt
BigInt doc and I quote
BigInt.asIntN() Wraps a BigInt value to a signed integer between -2^(width-1) and 2^(width-1 - 1).
BigInt.asUintN() Wraps a BigInt value to an unsigned integer between 0 and 2^(width - 1).
For 64 bit positive integer the max is 9223372036854775808
For 64 bit signed integer the min is -9223372036854775808
Case 1:
const max: BigInt = BigInt.asUintN(64, BigInt(9223372036854775806));
console.log("max is: " + max);
and prints
9223372036854775808 when it should print:
9223372036854775806
why? It works on small numbers like 10, 100 etc but fails on the limit.
Case 2:
const min: BigInt = BigInt.asIntN(64, BigInt(-9223372036854775808));
console.log("bits: " + min.toString(2).length);
and prints:
bits: 65
when clearly according to the documentation it should be 64
Upvotes: 1
Views: 133
Reputation: 386512
You could take the number as string for conversion to BigInt
.
const max = BigInt.asUintN(64, BigInt('9223372036854775806'));
console.log("max is: " + max);
Upvotes: 2