Baseline Widely available
La méthode statique BigInt.asIntN()
permet d'écréter un nombre BigInt
pour obtenir un entier signé entre 2^(largeur-1) et 2^(largeur-1)-1.
const I64_CEIL = 2n ** 63n;
console.log(BigInt.asIntN(64, I64_CEIL - 1n));
// 9223372036854775807n (2n ** 64n - 1n, the maximum non-wrapping value)
console.log(BigInt.asIntN(64, I64_CEIL));
// -9223372036854775808n (wraps to min value)
console.log(BigInt.asIntN(64, I64_CEIL + 1n));
// -9223372036854775807n (min value + 1n)
console.log(BigInt.asIntN(64, I64_CEIL * 2n));
// 0n (wrapped around to zero)
console.log(BigInt.asIntN(64, -I64_CEIL * -42n));
// 0n (also wraps on negative multiples)
Syntaxe
var resultat = BigInt.asIntN(largeur, bigint);
Paramètres
largeur
La quantité de bits disponible pour stocker l'entier.
bigint
L'entier qu'on souhaite stocker sur le nombre de bits indiqués.
La valeur de bigint
modulo 2^largeur
comme entier signé.
La méthode BigInt.asIntN()
peut être utile pour rester dans une arithmétique sur 64 bits :
const max = 2n ** (64n - 1n) - 1n;
BigInt.asIntN(64, max);
// ⪠9223372036854775807n
BigInt.asIntN(64, max + 1n);
// ⪠-9223372036854775807n
// négatif car dépassement sur le nombre de bits
Spécifications Compatibilité des navigateurs Voir aussi
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4