|
2 | 2 |
|
3 | 3 | [recent caniuse="bigint"] |
4 | 4 |
|
5 | | -`BigInt` is a special numeric type that provides support for integers of arbitrary length. |
| 5 | +`BigInt`는 길이의 제약 없이 정수를 다룰 수 있게 해주는 숫자형입니다. |
6 | 6 |
|
7 | | -A bigint is created by appending `n` to the end of an integer literal or by calling the function `BigInt` that creates bigints from strings, numbers etc. |
| 7 | +정수 리터럴 끝에 `n`을 붙이거나 함수 `BigInt`를 호출하면 문자열이나 숫자를 가지고 `BigInt` 타입의 값을 만들 수 있습니다. |
8 | 8 |
|
9 | 9 | ```js |
10 | 10 | const bigint = 1234567890123456789012345678901234567890n; |
11 | 11 |
|
12 | 12 | const sameBigint = BigInt("1234567890123456789012345678901234567890"); |
13 | 13 |
|
14 | | -const bigintFromNumber = BigInt(10); // same as 10n |
| 14 | +const bigintFromNumber = BigInt(10); // 10n과 동일합니다. |
15 | 15 | ``` |
16 | 16 |
|
17 | | -## Math operators |
| 17 | +## 수학 연산자 |
18 | 18 |
|
19 | | -`BigInt` can mostly be used like a regular number, for example: |
| 19 | +`BigInt`는 대개 일반 숫자와 큰 차이 없이 사용할 수 있습니다. |
20 | 20 |
|
21 | 21 | ```js run |
22 | 22 | alert(1n + 2n); // 3 |
23 | 23 |
|
24 | 24 | alert(5n / 2n); // 2 |
25 | 25 | ``` |
26 | 26 |
|
27 | | -Please note: the division `5/2` returns the result rounded towards zero, without the decimal part. All operations on bigints return bigints. |
| 27 | +위 예시에서 나눗셈 연산 `5/2`의 결과엔 소수부가 없다는 점에 주의하시기 바랍니다. `BigInt`형 값을 대상으로 한 연산은 `BigInt`형 값을 반환합니다. |
28 | 28 |
|
29 | | -We can't mix bigints and regular numbers: |
| 29 | +`BigInt`형 값과 일반 숫자를 섞어서 사용할 순 없습니다. |
30 | 30 |
|
31 | 31 | ```js run |
32 | 32 | alert(1n + 2); // Error: Cannot mix BigInt and other types |
33 | 33 | ``` |
34 | 34 |
|
35 | | -We should explicitly convert them if needed: using either `BigInt()` or `Number()`, like this: |
| 35 | +일반 숫자와 섞어서 써야 하는 상황이라면 `BigInt()`나 `Number()`를 사용해 명시적으로 형 변환을 해주면 됩니다. |
36 | 36 |
|
37 | 37 | ```js run |
38 | 38 | let bigint = 1n; |
39 | 39 | let number = 2; |
40 | 40 |
|
41 | | -// number to bigint |
| 41 | +// 숫자를 bigint로 |
42 | 42 | alert(bigint + BigInt(number)); // 3 |
43 | 43 |
|
44 | | -// bigint to number |
| 44 | +// bigint를 숫자로 |
45 | 45 | alert(Number(bigint) + number); // 3 |
46 | 46 | ``` |
47 | 47 |
|
48 | | -The conversion operations are always silent, never give errors, but if the bigint is too huge and won't fit the number type, then extra bits will be cut off, so we should be careful doing such conversion. |
| 48 | +형 변환과 관련된 연산은 항상 조용히 동작합니다. 절대 에러를 발생시키지 않죠. 그런데 bigint가 너무 커서 숫자형에서 허용하는 자릿수를 넘으면 나머지 비트는 자동으로 잘려 나갑니다. 이런 점을 염두하고 형 변환을 해야 합니다. |
49 | 49 |
|
50 | | -````smart header="The unary plus is not supported on bigints" |
51 | | -The unary plus operator `+value` is a well-known way to convert `value` to a number. |
| 50 | +````smart header="단항 덧셈 연산자는 bigint에 사용할 수 없습니다." |
| 51 | +단항 덧셈 연산자 `+value`를 사용하면 `value`를 손쉽게 숫자형으로 바꿀 수 있습니다. |
52 | 52 |
|
53 | | -On bigints it's not supported, to avoid confusion: |
| 53 | +그런데 bigint는 혼란을 방지하기 위해 단항 덧셈 연산자를 지원하지 않습니다. |
54 | 54 | ```js run |
55 | 55 | let bigint = 1n; |
56 | 56 |
|
57 | | -alert( +bigint ); // error |
| 57 | +alert( +bigint ); // 에러 |
58 | 58 | ``` |
59 | | -So we should use `Number()` to convert a bigint to a number. |
| 59 | +bigint를 숫자형으로 바꿀 때는 `Number()`를 사용해야 합니다. |
60 | 60 | ```` |
61 | 61 |
|
62 | | -## Comparisons |
| 62 | +## 비교 연산자 |
63 | 63 |
|
64 | | -Comparisons, such as `<`, `>` work with bigints and numbers just fine: |
| 64 | +비교 연산자 `<`, `>`는 bigint와 일반 숫자 모두에 사용할 수 있습니다. |
65 | 65 |
|
66 | 66 | ```js run |
67 | 67 | alert( 2n > 1n ); // true |
68 | 68 |
|
69 | 69 | alert( 2n > 1 ); // true |
70 | 70 | ``` |
71 | 71 |
|
72 | | -Please note though, as numbers and bigints belong to different types, they can be equal `==`, but not strictly equal `===`: |
| 72 | +그런데 비교하려는 대상이 다른 타입에 속하면 `==`를 사용할 때는 같을지 모르지만 `===`를 사용할 때는 다르다고 판단됩니다. |
73 | 73 |
|
74 | 74 | ```js run |
75 | 75 | alert( 1 == 1n ); // true |
76 | 76 |
|
77 | 77 | alert( 1 === 1n ); // false |
78 | 78 | ``` |
79 | 79 |
|
80 | | -## Boolean operations |
| 80 | +## 논리 연산 |
81 | 81 |
|
82 | | -When inside `if` or other boolean operations, bigints behave like numbers. |
| 82 | +bigint는 `if` 안이나 다른 논리 연산자와 함께 사용할 때 일반 숫자와 동일하게 행동합니다. |
83 | 83 |
|
84 | | -For instance, in `if`, bigint `0n` is falsy, other values are truthy: |
| 84 | +`if`안에서 `0n`은 falsy이고 다른 값들은 truthy로 평가되죠. |
85 | 85 |
|
86 | 86 | ```js run |
87 | 87 | if (0n) { |
88 | | - // never executes |
| 88 | + // 절대 실행되지 않습니다. |
89 | 89 | } |
90 | 90 | ``` |
91 | 91 |
|
92 | | -Boolean operators, such as `||`, `&&` and others also work with bigints similar to numbers: |
| 92 | +`||`, `&&` 등의 논리 연산자를 bigint에 적용할 때도 일반 숫자와 유사하게 동작합니다. |
93 | 93 |
|
94 | 94 | ```js run |
95 | | -alert( 1n || 2 ); // 1 (1n is considered truthy) |
| 95 | +alert( 1n || 2 ); // 1 (1n은 truthy로 판단됩니다.) |
96 | 96 |
|
97 | | -alert( 0n || 2 ); // 2 (0n is considered falsy) |
| 97 | +alert( 0n || 2 ); // 2 (0n은 falsy로 판단됩니다.) |
98 | 98 | ``` |
99 | 99 |
|
100 | | -## Polyfills |
| 100 | +## 폴리필 |
101 | 101 |
|
102 | | -Polyfilling bigints is tricky. The reason is that many JavaScript operators, such as `+`, `-` and so on behave differently with bigints compared to regular numbers. |
| 102 | +bigint 폴리필을 만드는 것은 꽤 까다롭습니다. `+`, `-`를 비롯한 다양한 연산자들이 bigint와 일반 숫자에서 다른 결과를 보이기 때문입니다. |
103 | 103 |
|
104 | | -For example, division of bigints always returns a bigint (rounded if necessary). |
| 104 | +bigint끼리 나누면 항상 bigint를 반환한다는 것을 앞서 말씀드린 바 있습니다. |
105 | 105 |
|
106 | | -To emulate such behavior, a polyfill would need to analyze the code and replace all such operators with its functions. But doing so is cumbersome and would cost a lot of performance. |
| 106 | +동일한 결과가 나오게 하려면 폴리필에서 기존 코드를 분석하고 내장 연산자 모두를 관련 함수로 대체해 줄 수 있어야 합니다. 그런데 이렇게 하려면 품이 많이 들고 성능 이슈도 생길 수 있습니다. |
107 | 107 |
|
108 | | -So, there's no well-known good polyfill. |
| 108 | +따라서 아직까진 제대로 된 bigint 폴리필이 나오지 않은 상황입니다. |
109 | 109 |
|
110 | | -Although, the other way around is proposed by the developers of [JSBI](https://github.com/GoogleChromeLabs/jsbi) library. |
| 110 | +잘 알려진 폴리필은 없지만 [JSBI](https://github.com/GoogleChromeLabs/jsbi) 라이브러리의 개발자들이 대안을 제시하긴 했습니다. |
111 | 111 |
|
112 | | -This library implements big numbers using its own methods. We can use them instead of native bigints: |
| 112 | +이 라이브러리는 자체적으로 만든 방법을 사용해 큰 숫자를 구현합니다. 순수 bigint대신 라이브러리에서 만든 숫자를 사용하는 게 대안이 될 수 있습니다. |
113 | 113 |
|
114 | | -| Operation | native `BigInt` | JSBI | |
| 114 | +| 연산 | 네이티브 `BigInt` | JSBI | |
115 | 115 | |-----------|-----------------|------| |
116 | | -| Creation from Number | `a = BigInt(789)` | `a = JSBI.BigInt(789)` | |
117 | | -| Addition | `c = a + b` | `c = JSBI.add(a, b)` | |
118 | | -| Subtraction | `c = a - b` | `c = JSBI.subtract(a, b)` | |
| 116 | +| 일반 숫자를 사용해 bigint만들기 | `a = BigInt(789)` | `a = JSBI.BigInt(789)` | |
| 117 | +| 덧셈 | `c = a + b` | `c = JSBI.add(a, b)` | |
| 118 | +| 뺄셈 | `c = a - b` | `c = JSBI.subtract(a, b)` | |
119 | 119 | | ... | ... | ... | |
120 | 120 |
|
121 | | -...And then use the polyfill (Babel plugin) to convert JSBI calls to native bigints for those browsers that support them. |
| 121 | +이렇게 JSBI를 사용해 숫자를 만든 다음 바벨 플러그인에 있는 폴리필을 사용해 JSBI 호출을 네이티브 bigint로 변환하면 원하는 브라우저에서 연산을 수행할 수 있습니다. |
122 | 122 |
|
123 | 123 | In other words, this approach suggests that we write code in JSBI instead of native bigints. But JSBI works with numbers as with bigints internally, emulates them closely following the specification, so the code will be "bigint-ready". |
124 | 124 |
|
125 | 125 | We can use such JSBI code "as is" for engines that don't support bigints and for those that do support - the polyfill will convert the calls to native bigints. |
126 | 126 |
|
127 | | -## References |
| 127 | +## 참고 자료 |
128 | 128 |
|
129 | 129 | - [MDN docs on BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt). |
130 | 130 | - [Specification](https://tc39.es/ecma262/#sec-bigint-objects). |
0 commit comments