Skip to content

BigInt #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 49 additions & 49 deletions 1-js/99-js-misc/05-bigint/article.md
Original file line number Diff line number Diff line change
@@ -1,130 +1,130 @@
# BigInt
# Typ BigInt

[recent caniuse="bigint"]

`BigInt` is a special numeric type that provides support for integers of arbitrary length.
`BigInt` je speciální číselný typ, který poskytuje podporu celých čísel libovolné délky.

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.
Čí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ě.

```js
const bigint = 1234567890123456789012345678901234567890n;

const sameBigint = BigInt("1234567890123456789012345678901234567890");
const stejnýBigint = BigInt("1234567890123456789012345678901234567890");

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

## Math operators
## Matematické operátory

`BigInt` can mostly be used like a regular number, for example:
`BigInt` lze většinou používat jako obvyklé číslo, například:

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

alert(5n / 2n); // 2
```

Please note: the division `5/2` returns the result rounded towards zero, without the decimal part. All operations on bigints return bigints.
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.

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

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

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

```js run
let bigint = 1n;
let number = 2;
let číslo = 2;

// number to bigint
alert(bigint + BigInt(number)); // 3
// číslo na bigint
alert(bigint + BigInt(číslo)); // 3

// bigint to number
alert(Number(bigint) + number); // 3
// bigint na číslo
alert(Number(bigint) + číslo); // 3
```

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.
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í.

````smart header="The unary plus is not supported on bigints"
The unary plus operator `+value` is a well-known way to convert `value` to a number.
````smart header="Biginty nepodporují unární plus"
Operátor unárního plusu `+hodnota` je dobře známý způsob, jak převést hodnotu `hodnota` na číslo.

In order to avoid confusion, it's not supported on bigints:
Aby nedocházelo ke zmatkům, biginty jej nepodporují:
```js run
let bigint = 1n;

alert( +bigint ); // error
alert( +bigint ); // chyba
```
So we should use `Number()` to convert a bigint to a number.
Ke konverzi bigintu na číslo bychom tedy měli používat `Number()`.
````

## Comparisons
## Porovnávání

Comparisons, such as `<`, `>` work with bigints and numbers just fine:
Porovnávání, např. `<`, `>`, fungují s biginty a čísly správně:

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

alert( 2n > 1 ); // true
```

Please note though, as numbers and bigints belong to different types, they can be equal `==`, but not strictly equal `===`:
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 `===`:

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

alert( 1 === 1n ); // false
```

## Boolean operations
## Booleovské operace

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

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

```js run
if (0n) {
// never executes
// nikdy se nespustí
}
```

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

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

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

## Polyfills
## Polyfilly

Polyfilling bigints is tricky. The reason is that many JavaScript operators, such as `+`, `-` and so on behave differently with bigints compared to regular numbers.
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.

For example, division of bigints always returns a bigint (rounded if necessary).
Například dělení bigintů vrátí vždy bigint (zaokrouhlený, je-li to nutné).

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.
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.

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

Although, the other way around is proposed by the developers of [JSBI](https://github.com/GoogleChromeLabs/jsbi) library.
Jinou cestičku kolem bigintů však nabízejí vývojáři knihovny [JSBI](https://github.com/GoogleChromeLabs/jsbi).

This library implements big numbers using its own methods. We can use them instead of native bigints:
Tato knihovna implementuje velká čísla svými vlastními metodami. Můžeme je používat místo nativních bigintů:

| Operation | native `BigInt` | JSBI |
| Operace | Nativní `BigInt` | JSBI |
|-----------|-----------------|------|
| Creation from Number | `a = BigInt(789)` | `a = JSBI.BigInt(789)` |
| Addition | `c = a + b` | `c = JSBI.add(a, b)` |
| Subtraction | `c = a - b` | `c = JSBI.subtract(a, b)` |
| Vytvoření z čísla | `a = BigInt(789)` | `a = JSBI.BigInt(789)` |
| Sčítání | `c = a + b` | `c = JSBI.add(a, b)` |
| Odčítání | `c = a - b` | `c = JSBI.subtract(a, b)` |
| ... | ... | ... |

...And then use the polyfill (Babel plugin) to convert JSBI calls to native bigints for those browsers that support them.
...A pak použít polyfill (zásuvný modul Babel) ke konverzi volání JSBI na nativní biginty pro prohlížeče, které je podporují.

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".
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“.

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.
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.

## References
## Odkazy

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