aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/javascript/reference/global_objects/bigint/index.html
blob: 2e62e69184ba5717c7a5ca2a9793a99126e17206 (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
---
title: BigInt
slug: Web/JavaScript/Reference/Global_Objects/BigInt
translation_of: Web/JavaScript/Reference/Global_Objects/BigInt
---
<p>{{JSRef}}{{SeeCompatTable}}</p>

<p><code>BigInt</code> 是一個內建的物件,提供了表示大於2^53的整數的功能 (2^53是JavaScript原生的{{JSxRef("Number")}}能夠表示的最大值)</p>

<h2 id="語法">語法</h2>

<pre class="brush: js">BigInt(value);
</pre>

<h3 id="參數">參數</h3>

<dl>
 <dt><code>value</code></dt>
 <dd>欲創建的數值,可以為整數或字串。</dd>
</dl>

<div class="notecard note">
<p><strong>Note:</strong> <code>BigInt()</code> 不和 {{JSxRef("Operators/new", "new")}} 一起使用。</p>
</div>

<dl>
</dl>

<h2 id="說明">說明</h2>

<p><code>BigInt</code> 是透過在一個數值後加上 <code>n</code> ,例如 <code>10n</code> ,或呼叫 <code>BigInt()</code> 所生成的。</p>

<pre class="brush: js">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> 的實體混用運算子。</p>

<div class="notecard warning">
<p><strong>警告:</strong> {{JSxRef("Number")}} 和 <code>BigInt</code> 不能混和計算 — 他們必須被轉換到同一個型態。</p>

<p>然而,在相互轉換時要注意, <code>BigInt</code> 在被轉換成 <code>Number</code> 時可能會遺失部分精度的資訊。</p>
</div>

<h3 id="類別資訊">類別資訊</h3>

<p>當使用 <code>typeof</code> 測試時,一個 <code>BigInt</code> 會回傳 "bigint":</p>

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

<p>當使用 <code>Object</code> 來包裹時,<code>BigInt</code> 會被看成是普通的 "object" 型態:</p>

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

<h3 id="Operators">Operators</h3>

<p>下列的運算子可以被用在 <code>BigInt</code> 上 (或由object包裹的 <code>BigInt</code>): <code>+</code>, <code>*</code>, <code>-</code>, <code>**</code>, <code>%</code>.</p>

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

<p><code>/</code> 運算子也同樣的能夠運行。然而,因為型態是 <code>BigInt</code> 而不是 <code>BigDecimal</code> ,除法運算會無條件捨去小數。也就是說,回傳值不會包含小數部分。</p>

<div class="notecard warning">
<p><strong>警告:</strong>回傳值帶小數的運算在使用<code>BigInt</code> 時小數部分會被捨去。</p>
</div>

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

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

</pre>

<h3 id="比較">比較</h3>

<p>一個 <code>BigInt</code> 並不嚴格等於一個 {{JSxRef("Global_Objects/Number", "Number")}},但他們會一般相等。</p>

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

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

<p>一個 {{JSxRef("Global_Objects/Number", "Number")}} 和 <code>BigInt</code> 可以像普通運算一樣比較。</p>

<pre class="brush: js">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">const mixed = [4n, 6, -12n, 10, 4, 0, 0n];
// ↪  [4n, 6, -12n, 10, 4, 0, 0n]

mixed.sort();
// ↪ [-12n, 0, 0n, 10, 4n, 4, 6]
</pre>

<p>Note that comparisons with <code>Object</code>-wrapped <code>BigInt</code>s act as with other objects, only indicating equality when the same object instance is compared:</p>

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

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

<h3 id="Conditionals">Conditionals</h3>

<p>A <code>BigInt</code> behaves like a {{JSxRef("Global_Objects/Number", "Number")}} in cases where it is converted to a {{JSxRef("Global_Objects/Boolean", "Boolean")}}: via the {{JSxRef("Global_Objects/Boolean", "Boolean")}} function; when used with logical operators {{JSxRef("Operators/Logical_Operators", "Logical Operators")}} <code>||</code>, <code>&amp;&amp;</code>, and <code>!</code>; or within a conditional test like an {{JSxRef("Statements/if...else", "if statement")}}.</p>

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

<dl>
 <dt>{{JSxRef("BigInt.asIntN()")}}</dt>
 <dd>Clamps a BigInt value to a signed integer value, and returns that value.</dd>
 <dt>{{JSxRef("BigInt.asUintN()")}}</dt>
 <dd>Clamps a BigInt value to an unsigned integer value, and returns that value.</dd>
</dl>

<h2 id="屬性">屬性</h2>

<dl>
 <dt>{{JSxRef("BigInt.prototype")}}</dt>
 <dd>允許對一個 <code>BigInt</code> 物件增加其屬性。</dd>
</dl>

<h2 id="BigInt_物件實體"><code>BigInt</code> 物件實體</h2>

<p>All <code>BigInt</code> instances inherit from <code>BigInt.prototype</code>. The prototype object of the <code>BigInt</code> constructor can be modified to affect all <code>BigInt</code> instances.</p>

<h3 id="方法_2">方法</h3>

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

<h2 id="建議用法">建議用法</h2>

<h3 id="轉型">轉型</h3>

<p>因為在 {{JSxRef("Global_Objects/Number", "Number")}} 和 <code>BigInt</code> 之間轉換可能造成精度遺失,建議當數值會超過2^53時只使用 <code>BigInt</code> ,而不要在兩者之間進行轉換。</p>

<h3 id="加密">加密</h3>

<p><code>BigInt</code> 支援的運算並非常數時間。因此 <code>BigInt</code> <a href="https://www.chosenplaintext.ca/articles/beginners-guide-constant-time-cryptography.html">不適用在加密學上</a></p>

<h2 id="範例">範例</h2>

<h3 id="計算質數">計算質數</h3>

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

{{Specifications}}

<h2 id="瀏覽器相容性">瀏覽器相容性</h2>

{{Compat}}

<h2 id="另見">另見</h2>

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