diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
| commit | 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch) | |
| tree | a9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/zh-tw/web/javascript/reference/global_objects/bigint | |
| parent | 074785cea106179cb3305637055ab0a009ca74f2 (diff) | |
| download | translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2 translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip | |
initial commit
Diffstat (limited to 'files/zh-tw/web/javascript/reference/global_objects/bigint')
| -rw-r--r-- | files/zh-tw/web/javascript/reference/global_objects/bigint/index.html | 279 |
1 files changed, 279 insertions, 0 deletions
diff --git a/files/zh-tw/web/javascript/reference/global_objects/bigint/index.html b/files/zh-tw/web/javascript/reference/global_objects/bigint/index.html new file mode 100644 index 0000000000..705a85e255 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/bigint/index.html @@ -0,0 +1,279 @@ +--- +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<sup>53</sup>的整數的功能 (2<sup>53</sup>是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="blockIndicator 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="blockIndicator warning"> +<p>{{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="blockIndicator warning"> +<p>回傳值帶小數的運算在使用<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 < 2 +// ↪ true + +2n > 1 +// ↪ true + +2 > 2 +// ↪ false + +2n > 2 +// ↪ false + +2n >= 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>&&</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 && 12n +// ↪ 0n + +Boolean(0n) +// ↪ false + +Boolean(12n) +// ↪ true + +!12n +// ↪ false + +!0n +// ↪ true +</pre> + +<h2 id="方法">方法</h2> + +<dl> + <dt>{{JSxRef("BigInt.asIntN()")}}</dt> + <dd>Wraps a BigInt between -2<sup>width-1</sup> and 2<sup>width-1</sup>-1</dd> + <dt>{{JSxRef("BigInt.asUintN()")}}</dt> + <dd>Wraps a BigInt between 0 and 2<sup>width</sup>-1</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<sup>53</sup>時只使用 <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 <= 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</pre> + +<h2 id="規範">規範</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">規範</th> + <th scope="col">狀態</th> + </tr> + <tr> + <td><a href="https://tc39.github.io/proposal-bigint/#sec-bigint-objects">BigInt</a></td> + <td>Stage 3</td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + +<div> + + +<p>{{Compat("javascript.builtins.BigInt")}}</p> +</div> + +<h2 id="另見">另見</h2> + +<ul> + <li>{{JSxRef("Number")}}</li> +</ul> |
