Skip to content

Commit cd11b15

Browse files
authored
Merge pull request #214 from otmon76/1.14.5
BigInt
2 parents 190a4cf + 6c85108 commit cd11b15

File tree

1 file changed

+49
-49
lines changed

1 file changed

+49
-49
lines changed

1-js/99-js-misc/05-bigint/article.md

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,130 @@
1-
# BigInt
1+
# Typ BigInt
22

33
[recent caniuse="bigint"]
44

5-
`BigInt` is a special numeric type that provides support for integers of arbitrary length.
5+
`BigInt` je speciální číselný typ, který poskytuje podporu celých čísel libovolné délky.
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+
Číslo typu BigInt se vytvoří přidáním písmene `n` na konec celočíselného literálu nebo voláním funkce `BigInt`, která vytváří biginty z řetězců, čísel a podobně.
88

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

12-
const sameBigint = BigInt("1234567890123456789012345678901234567890");
12+
const stejnýBigint = BigInt("1234567890123456789012345678901234567890");
1313

14-
const bigintFromNumber = BigInt(10); // same as 10n
14+
const bigintZČísla = BigInt(10); // totéž jako 10n
1515
```
1616

17-
## Math operators
17+
## Matematické operátory
1818

19-
`BigInt` can mostly be used like a regular number, for example:
19+
`BigInt` lze většinou používat jako obvyklé číslo, například:
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+
Prosíme všimněte si: dělení `5/2` vrací výsledek zaokrouhlený směrem k nule, bez desetinné části. Všechny operace na bigintech vracejí biginty.
2828

29-
We can't mix bigints and regular numbers:
29+
Nemůžeme směšovat biginty a běžná čísla:
3030

3131
```js run
32-
alert(1n + 2); // Error: Cannot mix BigInt and other types
32+
alert(1n + 2); // Chyba: Nelze míchat dohromady BigInt a jiné typy
3333
```
3434

35-
We should explicitly convert them if needed: using either `BigInt()` or `Number()`, like this:
35+
Když je potřeba, měli bychom je explicitně konvertovat použitím buď `BigInt()`, nebo `Number()`, například:
3636

3737
```js run
3838
let bigint = 1n;
39-
let number = 2;
39+
let číslo = 2;
4040

41-
// number to bigint
42-
alert(bigint + BigInt(number)); // 3
41+
// číslo na bigint
42+
alert(bigint + BigInt(číslo)); // 3
4343

44-
// bigint to number
45-
alert(Number(bigint) + number); // 3
44+
// bigint na číslo
45+
alert(Number(bigint) + číslo); // 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+
Konverzní operace jsou vždy tiché a nikdy neohlásí chybu, ale je-li bigint příliš velký a nevejde se do číselného typu, budou přebývající bity odříznuty, takže bychom při takové konverzi měli být opatrní.
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="Biginty nepodporují unární plus"
51+
Operátor unárního plusu `+hodnota` je dobře známý způsob, jak převést hodnotu `hodnota` na číslo.
5252
53-
In order to avoid confusion, it's not supported on bigints:
53+
Aby nedocházelo ke zmatkům, biginty jej nepodporují:
5454
```js run
5555
let bigint = 1n;
5656
57-
alert( +bigint ); // error
57+
alert( +bigint ); // chyba
5858
```
59-
So we should use `Number()` to convert a bigint to a number.
59+
Ke konverzi bigintu na číslo bychom tedy měli používat `Number()`.
6060
````
6161

62-
## Comparisons
62+
## Porovnávání
6363

64-
Comparisons, such as `<`, `>` work with bigints and numbers just fine:
64+
Porovnávání, např. `<`, `>`, fungují s biginty a čísly správně:
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+
Prosíme všimněte si však, že protože čísla a biginty patří k různým typům, mohou si být rovny `==`, ale ne striktně rovny `===`:
7373

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

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

80-
## Boolean operations
80+
## Booleovské operace
8181

82-
When inside `if` or other boolean operations, bigints behave like numbers.
82+
Když jsme uvnitř `if` nebo jiných booleovských operací, biginty se chovají jako čísla.
8383

84-
For instance, in `if`, bigint `0n` is falsy, other values are truthy:
84+
Například v `if` je bigint `0n` nepravdivý, ostatní hodnoty jsou pravdivé:
8585

8686
```js run
8787
if (0n) {
88-
// never executes
88+
// nikdy se nespustí
8989
}
9090
```
9191

92-
Boolean operators, such as `||`, `&&` and others also work with bigints similar to numbers:
92+
Rovněž booleovské operátory, např. `||`, `&&` a jiné, fungují s biginty obdobně jako s čísly:
9393

9494
```js run
95-
alert( 1n || 2 ); // 1 (1n is considered truthy)
95+
alert( 1n || 2 ); // 1 (1n se považuje za pravdivé)
9696

97-
alert( 0n || 2 ); // 2 (0n is considered falsy)
97+
alert( 0n || 2 ); // 2 (0n se považuje za nepravdivé)
9898
```
9999

100-
## Polyfills
100+
## Polyfilly
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+
Polyfillování bigintů je problematické. Příčinou je, že mnoho JavaScriptových operátorů, např. `+`, `-` a podobně, se chová na bigintech odlišně oproti běžným číslům.
103103

104-
For example, division of bigints always returns a bigint (rounded if necessary).
104+
Například dělení bigintů vrátí vždy bigint (zaokrouhlený, je-li to nutné).
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+
Aby polyfill mohl takové chování emulovat, musel by analyzovat kód a nahradit všechny takové operátory svými funkcemi. Provést něco takového je však těžkopádné a značně by to snížilo výkon.
107107

108-
So, there's no well-known good polyfill.
108+
Dosud tedy není dobře znám žádný dobrý polyfill.
109109

110-
Although, the other way around is proposed by the developers of [JSBI](https://github.com/GoogleChromeLabs/jsbi) library.
110+
Jinou cestičku kolem bigintů však nabízejí vývojáři knihovny [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+
Tato knihovna implementuje velká čísla svými vlastními metodami. Můžeme je používat místo nativních bigintů:
113113

114-
| Operation | native `BigInt` | JSBI |
114+
| Operace | Nativní `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+
| Vytvoření z čísla | `a = BigInt(789)` | `a = JSBI.BigInt(789)` |
117+
| Sčítání | `c = a + b` | `c = JSBI.add(a, b)` |
118+
| Odčítání | `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+
...A pak použít polyfill (zásuvný modul Babel) ke konverzi volání JSBI na nativní biginty pro prohlížeče, které je podporují.
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+
Jinými slovy, tento přístup nám navrhuje, abychom místo používání nativních bigintů psali kód v JSBI. Avšak JSBI vnitřně pracuje s čísly jako s biginty a emuluje je způsobem blízkým specifikaci, takže kód bude „připraven pro biginty“.
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+
Takový kód s JSBI můžeme používat beze změny na motorech, které biginty nepodporují, i na těch, které je podporují -- polyfill bude konvertovat volání na nativní biginty.
126126

127-
## References
127+
## Odkazy
128128

129-
- [MDN docs on BigInt](mdn:/JavaScript/Reference/Global_Objects/BigInt).
130-
- [Specification](https://tc39.es/ecma262/#sec-bigint-objects).
129+
- [MDN dokumentace BigIntu](mdn:/JavaScript/Reference/Global_Objects/BigInt).
130+
- [Specifikace](https://tc39.es/ecma262/#sec-bigint-objects).

0 commit comments

Comments
 (0)