aboutsummaryrefslogtreecommitdiff
path: root/files/ja/web/javascript/reference/global_objects/bigint/index.html
blob: 5b45760615fa2e9fbb56248c5b7a0c582e8b8716 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
---
title: BigInt
slug: Web/JavaScript/Reference/Global_Objects/BigInt
tags:
  - BigInt
  - JavaScript
translation_of: Web/JavaScript/Reference/Global_Objects/BigInt
---
<div>{{JSRef}}</div>

<p><strong><code>BigInt</code></strong> は組み込みオブジェクトで、 {{JSxRef("Number")}} プリミティブで表現できる最大の数、 {{JSxRef("Number.MAX_SAFE_INTEGER")}} よりも大きな数値を信頼できるものとして表現する方法を提供します。 <strong><code>BigInt</code></strong> は任意に巨大な整数に使用することができます。</p>

<h2 id="Syntax" name="Syntax">詳細</h2>

<p><code>BigInt</code> は10進数の整数リテラルの末尾に <code>n</code> をつけて <code>10n</code> とするか、 <code>BigInt()</code> 関数を呼び出すことで作成することができます。</p>

<pre class="brush: js notranslate">const theBiggestInt = 9007199254740991n;

const alsoHuge = BigInt(9007199254740991);
// ↪ 9007199254740991n

const hugeString = BigInt("9007199254740991");
// ↪ 9007199254740991n

const hugeHex = BigInt("0x1fffffffffffff");
// ↪ 9007199254740991n

const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111");
// ↪ 9007199254740991n
</pre>

<p><code>BigInt</code> はいくつかの点で {{JSxRef("Number")}} と似ていますが、重要ないくつかの点が異なります。 — 組込みの {{JSxRef("Math")}} オブジェクト内のメソッドでは利用できず、演算で <code>Number</code> のインスタンスと混ぜることができません。同じ型に統一する必要があります。ただし、<code>BigInt</code><code>Number</code> へ変換する際には精度が落ちることがあるので、相互に変化する場合には注意が必要です。</p>

<h3 id="Type_information" name="Type_information">型情報</h3>

<p><code>typeof</code><code>BigInt</code> に対する評価値は、"bigint" となります。</p>

<pre class="brush: js notranslate">typeof 1n === 'bigint'; // true
typeof BigInt('1') === 'bigint'; // true
</pre>

<p><code>Object</code> でラップされている場合は、 <code>BigInt</code> は通常の "object" として扱われます。</p>

<pre class="brush: js notranslate">typeof Object(1n) === 'object'; // true
</pre>

<h3 id="Operators" name="Operators">演算子</h3>

<p><code>BigInt</code> (または Object でラップした <code>BigInt</code>) を利用することができる演算子は、 <code>+</code>, <code>*</code>, <code>-</code>, <code>**</code>, <code>%</code> です。<a href="/ja/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators">ビット操作演算子</a>は、同様に利用できますが、 <code>&gt;&gt;&gt;</code> (論理的右シフト) は BigInt が常に符号付きなので除きます。同様に、単項演算子 (<code>+</code>) は <a href="https://github.com/tc39/proposal-bigint/blob/master/ADVANCED.md#dont-break-asmjs">asm.js を破らないように</a>対応していません。</p>

<pre class="brush: js notranslate">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
</pre>

<p><code>/</code> 演算子もすべての数値に対して、期待される通りに動作します。ただ <code>BigInt</code><code>BigDecimal</code> ではないため、演算結果は 0 の方向に丸められます。別の言い方をすれば、小数を返すことはありません。</p>

<div class="blockIndicator warning">
<p><code>BigInt</code> を使用した場合、結果が小数となるような演算は切り捨てられます。</p>
</div>

<pre class="brush: js notranslate">const expected = 4n / 2n;
// ↪ 2n

const rounded = 5n / 2n;
// ↪ 2n, not 2.5n

</pre>

<h3 id="Comparisons" name="Comparisons">比較演算</h3>

<p><code>BigInt</code>{{JSxRef("Number")}} と厳密等価ではありませんが、等価にはなります。</p>

<pre class="brush: js notranslate">0n === 0
// ↪ false

0n == 0
// ↪ true</pre>

<p>{{JSxRef("Global_Objects/Number", "Number")}}<code>BigInt</code> は通常通り比較できます。</p>

<pre class="brush: js notranslate">1n &lt; 2
// ↪ true

2n &gt; 1
// ↪ true

2 &gt; 2
// ↪ false

2n &gt; 2
// ↪ false

2n &gt;= 2
// ↪ true</pre>

<p>配列の要素に混在させることでき、並べ替えも可能です。</p>

<pre class="brush: js notranslate">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) =&gt; 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) =&gt; (a &lt; b) ? -1 : ((a &gt; b) ? 1 : 0)
// ↪  [ -12n, 0, 0n, 4n, 4, 6, 10 ]</pre>

<p>なお、 <code>Object</code> にラップされた <code>BigInt</code> は他のオブジェクトと同様の振る舞いをします。同じインスタンス同士が比較された場合にのみ、等価となります:</p>

<pre class="brush: js notranslate">0n === Object(0n); // false
Object(0n) === Object(0n); // false

const o = Object(0n);
o === o // true
</pre>

<h3 id="Conditionals" name="Conditionals">条件式</h3>

<p><code>BigInt</code>{{JSxRef("Global_Objects/Boolean", "Boolean")}} へ変換される次のような場合は、 {{JSxRef("Global_Objects/Number", "Number")}} と同様の変換が行われます。 </p>

<ul>
 <li>{{JSxRef("Global_Objects/Boolean", "Boolean")}} 関数を利用した場合</li>
 <li><a href="/ja/docs/Web/JavaScript/Reference/Operators/Logical_Operators">論理演算子</a> <code>||</code>、`<code>&amp;&amp;</code>`、<code>!</code> で使用される場合</li>
 <li> {{JSxRef("Statements/if...else", "if")}} 文などの条件式に使用される場合</li>
</ul>

<pre class="brush: js notranslate">if (0n) {
  console.log('Hello from the if!');
} else {
  console.log('Hello from the else!');
}

// ↪ "Hello from the else!"

0n || 12n
// ↪ 12n

0n &amp;&amp; 12n
// ↪ 0n

Boolean(0n)
// ↪ false

Boolean(12n)
// ↪ true

!12n
// ↪ false

!0n
// ↪ true
</pre>

<h2 id="Methods" name="Methods">コンストラクタ</h2>

<dl>
 <dt>
 <p>{{JSxRef("BigInt()")}}</p>
 </dt>
 <dd>
 <p><font face="Arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">新しい</span></font><code>bigint</code> 値を作ります。</p>
 </dd>
</dl>

<h2 id="Methods" name="Methods">静的メソッド</h2>

<dl>
 <dt>{{JSxRef("BigInt.asIntN()")}}</dt>
 <dd>BigInt の値を -2<sup>width-1</sup> ~ 2<sup>width-1</sup>-1 の間に丸めます。</dd>
 <dt>{{JSxRef("BigInt.asUintN()")}}</dt>
 <dd>BigInt の値を 0 ~ 2<sup>width</sup>-1 の間に丸めます。</dd>
</dl>

<h2 id="Properties" name="Properties">プロパティ</h2>

<dl>
 <dt>{{JSxRef("BigInt.prototype")}}</dt>
 <dd><code>BigInt</code> オブジェクトへの属性追加が可能です。</dd>
</dl>

<h2 id="BigInt_instances" name="BigInt_instances"><code>BigInt</code> インスタンス</h2>

<p>全ての <code>BigInt</code> インスタンスは <code>BigInt.prototype</code> を継承します。このプロトタイプオブジェクトへの変更は、全ての <code>BigInt</code> インスタンスに影響します。</p>

<h3 id="Methods_2" name="Methods_2">メソッド</h3>

<p>{{page("/ja/docs/Web/JavaScript/Reference/Global_Objects/BigInt/prototype", "Methods")}}</p>

<h2 id="Usage_recommendations" name="Usage_recommendations">使用方法の推奨事項</h2>

<h3 id="Coercion" name="Coercion">型変換</h3>

<p>{{JSxRef("Number")}}<code>BigInt</code> との間の型変換は精度が落ちる可能性があるため、 <code>BigInt</code> は値が論理的に2<sup>53</sup>以上になる場合にのみ使用し、この2つの型の間で型変換を行わないこと推奨します。</p>

<h3 id="Cryptography" name="Cryptography">暗号処理</h3>

<p><code>BigInt</code> で対応している演算は、実行時間が一定ではありません。従って、 <code>BigInt</code><a href="https://www.chosenplaintext.ca/articles/beginners-guide-constant-time-cryptography.html">暗号処理での使用には向きません</a></p>

<h3 id="Use_within_JSON" name="Use_within_JSON">JSON での使用</h3>

<p><code>BigInt</code> の値は既定で JSON のシリアライズに対応していないため、{{JSxRef("JSON.stringify()")}}<code>BigInt</code> 値に対して使用すると <code>TypeError</code> が発生します。ただし、必要であれば独自の <code>toJSON</code> メソッドを実装することができます。</p>

<pre class="brush: js notranslate">BigInt.prototype.toJSON = function() { return this.toString(); }</pre>

<p><code>JSON.stringify</code> により、例外が発生する代わりに次のように文字列を生成するようになります。</p>

<pre class="brush: js notranslate">JSON.stringify(BigInt(1));
// '"1"'</pre>

<h2 id="Examples" name="Examples"></h2>

<h3 id="Calculating_Primes" name="Calculating_Primes">素数の計算</h3>

<pre class="brush: js notranslate">function isPrime(p) {
  for (let i = 2n; i * i &lt;= 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 &gt;= 0n) {
    if (isPrime(maybePrime)) {
      nth -= 1n;
      prime = maybePrime;
    }
    maybePrime += 1n;
  }

  return prime;
}

nthPrime(20n)
// ↪ 73n</pre>

<h2 id="Specifications" name="Specifications">仕様書</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">仕様書</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName("ESDraft", "#sec-bigint-objects", "<code>BigInt</code> objects")}}</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザーの互換性</h2>

<div class="hidden">このページの互換性一覧表は構造化データから生成されています。データに協力していただけるのであれば、 <a class="external" href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> をチェックアウトしてプルリクエストを送信してください。</div>

<p>{{Compat("javascript.builtins.BigInt")}}</p>

<h3 id="Implementation_Progress">Implementation Progress</h3>

<p>The following table provides a daily implementation status for this feature, because this feature has not yet reached cross-browser stability. The data is generated by running the relevant feature tests in <a href="https://github.com/tc39/test262">Test262</a>, the standard test suite of JavaScript, in the nightly build, or latest release of each browser's JavaScript engine.</p>

<p>{{EmbedTest262ReportResultsTable("BigInt")}}</p>

<h2 id="See_also" name="See_also">関連情報</h2>

<ul>
 <li>{{JSxRef("Number")}}</li>
</ul>