aboutsummaryrefslogtreecommitdiff
path: root/files/ko/web/javascript/reference/global_objects/bigint/index.html
diff options
context:
space:
mode:
Diffstat (limited to 'files/ko/web/javascript/reference/global_objects/bigint/index.html')
-rw-r--r--files/ko/web/javascript/reference/global_objects/bigint/index.html280
1 files changed, 280 insertions, 0 deletions
diff --git a/files/ko/web/javascript/reference/global_objects/bigint/index.html b/files/ko/web/javascript/reference/global_objects/bigint/index.html
new file mode 100644
index 0000000000..dcfc19e799
--- /dev/null
+++ b/files/ko/web/javascript/reference/global_objects/bigint/index.html
@@ -0,0 +1,280 @@
+---
+title: BigInt
+slug: Web/JavaScript/Reference/Global_Objects/BigInt
+tags:
+ - BigInt
+ - JavaScript
+ - Reference
+translation_of: Web/JavaScript/Reference/Global_Objects/BigInt
+---
+<div>{{JSRef}}</div>
+
+<p><strong><code>BigInt</code></strong>는 {{jsxref("Number")}} 원시 값이 안정적으로 나타낼 수 있는 최대치인 2<sup>53</sup> - 1보다 큰 정수를 표현할 수 있는 내장 객체입니다.</p>
+
+<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")}}는 어떤 면에서 비슷하지만 중요한 차이점이 있습니다. 예컨대 <code>BigInt</code>는 내장 {{jsxref("Math")}} 객체의 메서드와 함께 사용할 수 없고, 연산에서 <code>Number</code>와 혼합해 사용할 수 없습니다. 따라서 먼저 같은 자료형으로 변환해야 합니다. 그러나, <code>BigInt</code>가 <code>Number</code>로 바뀌면 정확성을 잃을 수 있으니 주의해야 합니다.</p>
+
+<h3 id="자료형_정보">자료형 정보</h3>
+
+<p><code>BigInt</code>의 <code>typeof</code> 판별 결과는 <code>"bigint"</code>입니다.</p>
+
+<pre class="brush: js">typeof 1n === 'bigint'; // true
+typeof BigInt('1') === 'bigint'; // true
+</pre>
+
+<p>{{jsxref("Object")}}로 감싼 <code>BigInt</code>는 일반적인 <code>object</code> 자료형으로 취급합니다.</p>
+
+<pre class="brush: js">typeof Object(1n) === 'object'; // true
+</pre>
+
+<h3 id="연산자">연산자</h3>
+
+<p><code>+</code>, <code>*</code>, <code>-</code>, <code>**</code>, <code>%</code> 연산자를 <code>BigInt</code>나 객체로 감싼 <code>BigInt</code>에서 사용할 수 있습니다. <a href="/ko/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators">비트 연산자</a>도 사용할 수 있으나, 모든 <code>BigInt</code>는 부호를 가져야 하므로 <code>&gt;&gt;&gt;</code> (부호 버림 오른쪽 시프트)는 사용할 수 없습니다. <a href="https://github.com/tc39/proposal-bigint/blob/master/ADVANCED.md#dont-break-asmjs">asm.js에서 문제를 일으키지 않도록</a>, 단항 <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;
+// ↪ 2.5n이 아니라 2n
+</pre>
+
+<h3 id="비교">비교</h3>
+
+<p><code>BigInt</code>는 {{jsxref("Number")}}와 일치하지 않지만 동등합니다.</p>
+
+<pre class="brush: js">0n === 0
+// ↪ false
+
+0n == 0
+// ↪ true</pre>
+
+<p><code>Number</code>와 <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>{{jsxref("Object")}}로 감싼 <code>BigInt</code>의 경우 다른 객체와 마찬가지로 자기 자신과 비교했을 때만 일치합니다.</p>
+
+<pre class="brush: js">0n === Object(0n); // false
+Object(0n) === Object(0n); // false
+
+const o = Object(0n);
+o === o // true
+</pre>
+
+<h3 id="조건">조건</h3>
+
+<p><code>BigInt</code>는 다음의 상황에서는 {{jsxref("Number")}}처럼 행동합니다.</p>
+
+<ul>
+ <li>{{jsxref("Boolean")}} 함수를 사용해 Boolean 객체로 변환</li>
+ <li><a href="/ko/docs/Web/JavaScript/Reference/Operators/%EB%85%BC%EB%A6%AC_%EC%97%B0%EC%82%B0%EC%9E%90(Logical_Operators)">논리 연산자</a> <code>||</code>, <code>&amp;&amp;</code>, <code>!</code>와 함께 사용</li>
+ <li>{{jsxref("Statements/if...else", "if")}}문 등 조건 판별 시</li>
+</ul>
+
+<pre class="brush: js">if (0n) {
+ console.log('if에서 안녕!');
+} else {
+ console.log('else에서 안녕!');
+}
+
+// ↪ "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.BigInt", "BigInt()")}}</dt>
+ <dd><code>BigInt</code> 객체를 생성합니다.</dd>
+</dl>
+
+<h2 id="정적_메서드">정적 메서드</h2>
+
+<dl>
+ <dt>{{jsxref("BigInt.asIntN()")}}</dt>
+ <dd>주어진 <code>BigInt</code>를 <code>-2<sup>width-1</sup></code>과 <code>2<sup>width-1</sup> - 1</code>의 범위로 자릅니다.</dd>
+ <dt>{{jsxref("BigInt.asUintN()")}}</dt>
+ <dd>주어진 <code>BigInt</code>를 <code>0</code>과 <code>2<sup>width</sup> - 1</code>의 범위로 자릅니다.</dd>
+</dl>
+
+<h2 id="인스턴스_메서드">인스턴스 메서드</h2>
+
+<dl>
+ <dt>{{jsxref("BigInt.prototype.toLocaleString()")}}</dt>
+ <dd>BigInt를 주어진 언어에 적합한 형태를 가진 문자열로 변환해 반환합니다. {{jsxref("Object.prototype.toLocaleString()")}} 메서드를 재정의합니다.</dd>
+ <dt>{{jsxref("BigInt.prototype.toString()")}}</dt>
+ <dd><code>BigInt</code>의 값을 주어진 진수로 표현한 문자열을 반환합니다. {{jsxref("Object.prototype.toString()")}} 메서드를 재정의합니다.</dd>
+ <dt>{{jsxref("BigInt.prototype.valueOf()")}}</dt>
+ <dd><code>BigInt</code> 객체의 원시 값 표현을 반환합니다. {{jsxref("Object.prototype.valueOf()")}} 메서드를 재정의합니다.</dd>
+</dl>
+
+<h2 id="권장사항">권장사항</h2>
+
+<h3 id="변환">변환</h3>
+
+<p><code>BigInt</code>를 {{jsxref("Number")}}로 변환하는 과정에서 정확도를 유실할 수 있으므로, 2<sup>53</sup>보다 큰 값을 예상할 수 있는 경우 <code>BigInt</code>만 사용하는 것이 좋습니다.</p>
+
+<h3 id="암호화">암호화</h3>
+
+<p><code>BigInt</code>가 지원하는 연산의 소요시간은 상수 시간이 아니기 때문에 <a href="https://www.chosenplaintext.ca/articles/beginners-guide-constant-time-cryptography.html">암호화에 적합하지 않습니다</a>.</p>
+
+<h3 id="JSON과_함께_사용하기">JSON과 함께 사용하기</h3>
+
+<p><code>BigInt</code>는 직렬화할 수 없기 때문에, {{jsxref("JSON.stringify()")}}에 <code>BigInt</code>를 포함한 값을 전달한다면 <code>TypeError</code>가 발생합니다. 대신, 필요한 경우 자신만의 <code>toJSON</code> 메서드를 만들 수 있습니다.</p>
+
+<pre class="brush: js">BigInt.prototype.toJSON = function() { return this.toString(); }</pre>
+
+<p>이제 아래 코드가 오류를 던지지 않고 문자열을 반환합니다.</p>
+
+<pre class="brush: js">JSON.stringify(BigInt(1));
+// '"1"'
+</pre>
+
+<h2 id="예제">예제</h2>
+
+<h3 id="소수_구하기">소수 구하기</h3>
+
+<pre class="brush: js">// 주어진 BigInt가 소수이면 true 반환
+function isPrime(p) {
+ for (let i = 2n; i * i &lt;= p; i++) {
+ if (p % i === 0n) return false;
+ }
+ return true;
+}
+
+// BigInt를 받아, n번째 소수를 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>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ </tr>
+ <tr>
+ <td>{{SpecName("ESDraft", "#sec-bigint-objects", "<code>BigInt</code> objects")}}</td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="브라우저_호환성">브라우저 호환성</h2>
+
+
+
+<p>{{Compat("javascript.builtins.BigInt")}}</p>
+
+<h3 id="구현_진척도">구현 진척도</h3>
+
+<p>본 기능은 아직 안정적인 크로스 브라우징에 도달하지 못했으므로, 매일 업데이트되는 아래 표에서 브라우저별 구현 상황을 확인할 수 있습니다. 이 데이터는 각 브라우저 JavaScript 엔진의 나이틀리 빌드 또는 최신 릴리즈판에서, JavaScript 표준 테스트인 <a href="https://github.com/tc39/test262">Test262</a>의 관련 항목을 시험해 생성합니다.</p>
+
+<p>{{EmbedTest262ReportResultsTable("BigInt")}}</p>
+
+<h2 id="같이_보기">같이 보기</h2>
+
+<ul>
+ <li>{{jsxref("Number")}}</li>
+</ul>