Skip to content

Commit f1a51f5

Browse files
committed
BigInt
1 parent 3f94738 commit f1a51f5

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

Diff for: 1-js/99-js-misc/05-bigint/article.md

+41-41
Original file line numberDiff line numberDiff line change
@@ -2,129 +2,129 @@
22

33
[recent caniuse="bigint"]
44

5-
`BigInt` is a special numeric type that provides support for integers of arbitrary length.
5+
`BigInt` 是一種支援任意整數長度的特殊數值類型。
66

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` 函式以字串、數字等建立大數。
88

99
```js
1010
const bigint = 1234567890123456789012345678901234567890n;
1111

1212
const sameBigint = BigInt("1234567890123456789012345678901234567890");
1313

14-
const bigintFromNumber = BigInt(10); // same as 10n
14+
const bigintFromNumber = BigInt(10); // 等同 10n
1515
```
1616

17-
## Math operators
17+
## 數學運算子
1818

19-
`BigInt` can mostly be used like a regular number, for example:
19+
`BigInt` 通常可以像一般數字使用,例如:
2020

2121
```js run
2222
alert(1n + 2n); // 3
2323

2424
alert(5n / 2n); // 2
2525
```
2626

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` 的除法傳回的是四捨五入後結果,不含小數部份。所有在大數上的操作皆傳回大數。
2828

29-
We can't mix bigints and regular numbers:
29+
我們不能將大數和一般數字混用:
3030

3131
```js run
3232
alert(1n + 2); // Error: Cannot mix BigInt and other types
3333
```
3434

35-
We should explicitly convert them if needed: using either `BigInt()` or `Number()`, like this:
35+
如果需要使用,我們應該明確轉換它們:使用 `BigInt()` `Number()`,像這樣:
3636

3737
```js run
3838
let bigint = 1n;
3939
let number = 2;
4040

41-
// number to bigint
41+
// 數字轉大數
4242
alert(bigint + BigInt(number)); // 3
4343

44-
// bigint to number
44+
// 大數轉數字
4545
alert(Number(bigint) + number); // 3
4646
```
4747

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+
轉換操作總是默認的,不會給出錯誤,但是如果大數數字大太無法適用在數字類型時,多出來的位元將被截斷,所以我們應該謹慎的進行這類的轉換。
4949

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="大數不支援一元加號"
51+
一元加號運算子 `+value` 眾所皆知,是將 `value` 轉換為數字的方法。
5252
53-
On bigints it's not supported, to avoid confusion:
53+
為了避免混淆,在大數上是不支援的:
5454
```js run
5555
let bigint = 1n;
5656
5757
alert( +bigint ); // error
5858
```
59-
So we should use `Number()` to convert a bigint to a number.
59+
所以我們應該使用 `Number()` 去把大數轉換成數字。
6060
````
6161

62-
## Comparisons
62+
## 比較
6363

64-
Comparisons, such as `<`, `>` work with bigints and numbers just fine:
64+
像是 `<``>` 的比較,用在大數和數字間是可以的:
6565

6666
```js run
6767
alert( 2n > 1n ); // true
6868

6969
alert( 2n > 1 ); // true
7070
```
7171

72-
Please note though, as numbers and bigints belong to different types, they can be equal `==`, but not strictly equal `===`:
72+
但是请注意,由於數字跟大數屬於不同類型,因此它們可以等於 `==`,但不能嚴格等於 `===`
7373

7474
```js run
7575
alert( 1 == 1n ); // true
7676

7777
alert( 1 === 1n ); // false
7878
```
7979

80-
## Boolean operations
80+
## 布林值操作
8181

82-
When inside `if` or other boolean operations, bigints behave like numbers.
82+
`if` 中或其他布林值操作,大數行為類似數字。
8383

84-
For instance, in `if`, bigint `0n` is falsy, other values are truthy:
84+
例如,在 `if` 中,大數 `0n` 是屬於假,其他值是屬於真:
8585

8686
```js run
8787
if (0n) {
88-
// never executes
88+
// 永遠不會執行
8989
}
9090
```
9191

92-
Boolean operators, such as `||`, `&&` and others also work with bigints similar to numbers:
92+
布林值運算子,像是 `||``&&` 等也可以用在類似數字的大數:
9393

9494
```js run
95-
alert( 1n || 2 ); // 1 (1n is considered truthy)
95+
alert( 1n || 2 ); // 1 (1n是屬於真)
9696

97-
alert( 0n || 2 ); // 2 (0n is considered falsy)
97+
alert( 0n || 2 ); // 2 (0n是屬於假)
9898
```
9999

100100
## Polyfills
101101

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+
大數的 Polyfilling 是棘手的。原因是許多 JavaScript 運算子,像是 `+``-` 等的行為在大數和一般數字是不同的。
103103

104-
For example, division of bigints always returns a bigint (rounded if necessary).
104+
例如,大數的除法總是傳回大數(必要時會四捨五入)。
105105

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+
要模擬這些行為,這個 polyfill 需要分析程式碼,並使用其函式取代所有運算子。但是這樣很麻煩,而且還會浪費很多效率。
107107

108-
So, there's no well-known good polyfill.
108+
因此,沒有眾所皆知的好 polyfill
109109

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) 程式庫的開發者提供了其他解決辦法。
111111

112-
This library implements big numbers using its own methods. We can use them instead of native bigints:
112+
這個程式庫使用他們自有的方法實做大數。我們可以使用他們替換原生的大數:
113113

114-
| Operation | native `BigInt` | JSBI |
114+
| 操作 | 原生 `BigInt` | JSBI |
115115
|-----------|-----------------|------|
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+
| 以數字建立 | `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)` |
119119
| ... | ... | ... |
120120

121-
...And then use the polyfill (Babel plugin) to convert JSBI calls to native bigints for those browsers that support them.
121+
...然後使用 polyfill (Babel plugin) 將那些原生大數轉換成呼叫 JSBI ,使瀏覽器支援。
122122

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".
123+
換句話說,這方式建議我們使用 JSBI 替代原生的大數來寫程式碼。JSBI 在內部將數字作為大數,按照規格接近的模擬,所以程式碼將 "可用於大數"。
124124

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.
125+
對於大數支援與否,我們可以 "照原樣" 使用 JSBI 程式碼 - polyfill 將轉換呼叫原生大數。
126126

127-
## References
127+
## 參見
128128

129-
- [MDN docs on BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt).
130-
- [Specification](https://tc39.es/ecma262/#sec-bigint-objects).
129+
- [MDN 文件上的 BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
130+
- [規格](https://tc39.es/ecma262/#sec-bigint-objects)

0 commit comments

Comments
 (0)