BigInt
BigInt は組み込みオブジェクトで、 Number プリミティブで表現できる最大の数、 Number.MAX_SAFE_INTEGER よりも大きな数値を信頼できるものとして表現する方法を提供します。 BigInt は任意に巨大な整数に使用することができます。
詳細
BigInt は10進数の整数リテラルの末尾に n をつけて 10n とするか、 BigInt() 関数を呼び出すことで作成することができます。
const theBiggestInt = 9007199254740991n;
const alsoHuge = BigInt(9007199254740991);
// ↪ 9007199254740991n
const hugeString = BigInt("9007199254740991");
// ↪ 9007199254740991n
const hugeHex = BigInt("0x1fffffffffffff");
// ↪ 9007199254740991n
const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111");
// ↪ 9007199254740991n
BigInt はいくつかの点で Number と似ていますが、重要ないくつかの点が異なります。 — 組込みの Math オブジェクト内のメソッドでは利用できず、演算で Number のインスタンスと混ぜることができません。同じ型に統一する必要があります。ただし、BigInt を Number へ変換する際には精度が落ちることがあるので、相互に変化する場合には注意が必要です。
型情報
typeof の BigInt に対する評価値は、"bigint" となります。
typeof 1n === 'bigint'; // true
typeof BigInt('1') === 'bigint'; // true
Object でラップされている場合は、 BigInt は通常の "object" として扱われます。
typeof Object(1n) === 'object'; // true
演算子
BigInt (または Object でラップした BigInt) を利用することができる演算子は、 +, *, -, **, % です。ビット操作演算子は、同様に利用できますが、 >>> (論理的右シフト) は BigInt が常に符号付きなので除きます。同様に、単項演算子 (+) は asm.js を破らないように対応していません。
const previousMaxSafe = BigInt(Number.MAX_SAFE_INTEGER);
// ↪ 9007199254740991
const maxPlusOne = previousMaxSafe + 1n;
// ↪ 9007199254740992n
const theFuture = previousMaxSafe + 2n;
// ↪ 9007199254740993n, this works now!
const multi = previousMaxSafe * 2n;
// ↪ 18014398509481982n
const subtr = multi – 10n;
// ↪ 18014398509481972n
const mod = multi % 10n;
// ↪ 2n
const bigN = 2n ** 54n;
// ↪ 18014398509481984n
bigN * -1n
// ↪ –18014398509481984n
/ 演算子もすべての数値に対して、期待される通りに動作します。ただ BigInt はBigDecimal ではないため、演算結果は 0 の方向に丸められます。別の言い方をすれば、小数を返すことはありません。
BigInt を使用した場合、結果が小数となるような演算は切り捨てられます。
const expected = 4n / 2n;
// ↪ 2n
const rounded = 5n / 2n;
// ↪ 2n, not 2.5n
比較演算
BigInt は Number と厳密等価ではありませんが、等価にはなります。
0n === 0
// ↪ false
0n == 0
// ↪ trueNumber と BigInt は通常通り比較できます。
1n < 2
// ↪ true
2n > 1
// ↪ true
2 > 2
// ↪ false
2n > 2
// ↪ false
2n >= 2
// ↪ true配列の要素に混在させることでき、並べ替えも可能です。
const mixed = [4n, 6, -12n, 10, 4, 0, 0n];
// ↪ [4n, 6, -12n, 10, 4, 0, 0n]
mixed.sort();
// ↪ [-12n, 0, 0n, 4n, 4, 6, 10]
mixed.sort((a, b) => a - b)
// won't work since subtraction will not work with mixed types
// TypeError: can't convert BigInt to number
// sort with an appropriate numeric comparator
mixed.sort((a, b) => (a < b) ? -1 : ((a > b) ? 1 : 0)
// ↪ [ -12n, 0, 0n, 4n, 4, 6, 10 ]なお、 Object にラップされた BigInt は他のオブジェクトと同様の振る舞いをします。同じインスタンス同士が比較された場合にのみ、等価となります:
0n === Object(0n); // false
Object(0n) === Object(0n); // false
const o = Object(0n);
o === o // true
条件式
BigInt が Boolean へ変換される次のような場合は、 Number と同様の変換が行われます。
if (0n) {
console.log('Hello from the if!');
} else {
console.log('Hello from the else!');
}
// ↪ "Hello from the else!"
0n || 12n
// ↪ 12n
0n && 12n
// ↪ 0n
Boolean(0n)
// ↪ false
Boolean(12n)
// ↪ true
!12n
// ↪ false
!0n
// ↪ true
コンストラクタ
-
新しい
bigint値を作ります。
静的メソッド
BigInt.asIntN()- BigInt の値を -2width-1 ~ 2width-1-1 の間に丸めます。
BigInt.asUintN()- BigInt の値を 0 ~ 2width-1 の間に丸めます。
プロパティ
BigInt.prototypeBigIntオブジェクトへの属性追加が可能です。
BigInt インスタンス
全ての BigInt インスタンスは BigInt.prototype を継承します。このプロトタイプオブジェクトへの変更は、全ての BigInt インスタンスに影響します。
メソッド
{{page("/ja/docs/Web/JavaScript/Reference/Global_Objects/BigInt/prototype", "Methods")}}
使用方法の推奨事項
型変換
Number と BigInt との間の型変換は精度が落ちる可能性があるため、 BigInt は値が論理的に253以上になる場合にのみ使用し、この2つの型の間で型変換を行わないこと推奨します。
暗号処理
BigInt で対応している演算は、実行時間が一定ではありません。従って、 BigInt は暗号処理での使用には向きません。
JSON での使用
BigInt の値は既定で JSON のシリアライズに対応していないため、JSON.stringify() を BigInt 値に対して使用すると TypeError が発生します。ただし、必要であれば独自の toJSON メソッドを実装することができます。
BigInt.prototype.toJSON = function() { return this.toString(); }JSON.stringify により、例外が発生する代わりに次のように文字列を生成するようになります。
JSON.stringify(BigInt(1));
// '"1"'例
素数の計算
function isPrime(p) {
for (let i = 2n; i * i <= p; i++) {
if (p % i === 0n) return false;
}
return true;
}
// Takes a BigInt as an argument and returns a BigInt
function nthPrime(nth) {
let maybePrime = 2n;
let prime = 0n;
while (nth >= 0n) {
if (isPrime(maybePrime)) {
nth -= 1n;
prime = maybePrime;
}
maybePrime += 1n;
}
return prime;
}
nthPrime(20n)
// ↪ 73n仕様書
ブラウザーの互換性
BCD tables only load in the browser