2019-04-21 11:50:48 +08:00

153 lines
7.0 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<article id="wikiArticle">
<p>在JavaScript中<code>Number</code> 可以准确表达的最大数字是2<sup>53</sup>比2<sup>53</sup>大的所有数字可以使用<code>BigInt</code>表达。</p>
<h2 id="语法">语法</h2>
<pre><code class="language-javascript">BigInt(value);
</code></pre>
<h3 id="参数">参数</h3>
<dl>
<dt><code>value</code></dt>
<dd>创建对象的数值。可以是字符串或者整数。</dd>
</dl>
<div class="blockIndicator note">
<p>注意, <code>BigInt()</code> 不是构造函数,因此不能使用 <a href="Reference/Operators/new" title="new 运算符创建一个用户定义的对象类型的实例或具有构造函数的内置对象的实例。"><code>new</code></a> 操作符。</p>
</div>
<dl>
</dl>
<h2 id="描述">描述</h2>
<p>可以这样定义一个 <code>BigInt</code> 变量:在一个整数字面量后面加 <code>n</code>,如:<code>10n</code>,或者调用函数<code>BigInt()</code></p>
<pre><code class="language-javascript">const theBiggestInt = 9007199254740991n;
const alsoHuge = BigInt(9007199254740991);
// ↪ 9007199254740991n
const hugeButString = BigInt('9007199254740991');
// ↪ 9007199254740991n</code></pre>
<p>它在某些方面类似于 <a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a> ,但是也有几个关键的不同点:不能和 <a href="Reference/Global_Objects/Math" title="Math 是一个内置对象 它具有数学常数和函数的属性和方法。不是一个函数对象。"><code>Math</code></a> 对象中的方法一起使用;不能和任何 <a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a> 实例混合运算。</p>
<div class="blockIndicator warning">
<p><a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a><code>BigInt</code> 不能混合在一起运算,两者必须转换成同一种类型。</p>
<p>在两种类型来回转换时要小心,因为 <code>BigInt</code> 变量在转换成 <a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a> 变量时可能会丢失精度。</p>
</div>
<p> </p>
<h3 id="运算">运算</h3>
<p>以下操作符可以和 <code>BigInt</code> 一起使用: <code>+</code>、`<code>*</code>`、`<code>-</code>`、`<code>**</code>`、`<code>%</code>` 。</p>
<pre><code class="language-javascript">const previousMaxSafe = BigInt(Number.MAX_SAFE_INTEGER);
// ↪ 9007199254740991n
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
</code></pre>
<p><code>/</code> 操作符对于所有数字的运算也没问题。可是因为这些变量是 <code>BigInt</code> 而不是 <code>BigDecimal</code> ,该操作符会导致向零取整,也就是说不会返回小数部分。</p>
<div class="blockIndicator warning">
<p>当使用 <code>BigInt</code> 时,带小数的运算会被取整。</p>
</div>
<pre><code class="language-javascript">const expected = 4n / 2n;
// ↪ 2n
const rounded = 5n / 2n;
// ↪ 2n, not 2.5n
</code></pre>
<h3 id="比较">比较</h3>
<p><code>BigInt</code><a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a> 不是严格相等的,但是宽松相等的。</p>
<pre><code class="language-javascript">0n === 0
// ↪ false
0n == 0
// ↪ true</code></pre>
<p><a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a><code>BigInt</code> 可以像一般情况进行比较。</p>
<pre><code class="language-javascript">1n &lt; 2
// ↪ true
2n &gt; 1
// ↪ true
2 &gt; 2
// ↪ false
2n &gt; 2
// ↪ false
2n &gt;= 2
// ↪ true</code></pre>
<p>两者也可以混在一个数组内并排序。</p>
<pre><code class="language-javascript">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]</code></pre>
<h3 id="条件">条件</h3>
<p><code>BigInt</code> 在转换成 <a href="Reference/Global_Objects/Boolean" title="Boolean对象是一个布尔值的对象包装器。"><code>Boolean</code></a> 时和 <a href="Reference/Global_Objects/Number" title="JavaScript 的 Number 对象是经过封装的能让你处理数字值的对象。Number 对象由 Number() 构造器创建。"><code>Number</code></a> 类似:通过 <a href="Reference/Global_Objects/Boolean" title="Boolean对象是一个布尔值的对象包装器。"><code>Boolean</code></a> 函数;和 <a href="Reference/Operators/Logical_Operators" title="逻辑运算符通常用于布尔型(逻辑)值。这种情况下,它们返回一个布尔值。然而,&amp;&amp; 和 || 运算符会返回一个指定操作数的值,因此,这些运算符也用于非布尔值。这时,它们也就会返回一个非布尔型值。"><code>Logical Operators</code></a>  <code>||</code>, `<code>&amp;&amp;</code>`, and <code>!</code> 一起使用;或者在像 <a href="Reference/Statements/if...else" title="当指定条件为真if 语句会执行一段语句。如果条件为假,则执行另一段语句。"><code>if statement</code></a> 这样的条件语句中。</p>
<pre><code class="language-javascript">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
</code></pre>
<h2 id="例子">例子</h2>
<h3 id="Calculating_Primes">Calculating Primes</h3>
<pre><code class="language-javascript">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</code></pre>
</article>