Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
otmon76 merged 1 commit intojavascript-tutorial:masterfromotmon76:1.14.5
Jun 4, 2025
Merged
Changes fromall 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 deletions1-js/99-js-misc/05-bigint/article.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,130 @@
# BigInt
#TypBigInt

[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;

constsameBigint = BigInt("1234567890123456789012345678901234567890");
conststejnýBigint = BigInt("1234567890123456789012345678901234567890");

constbigintFromNumber = BigInt(10); //same as 10n
constbigintZČí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 mixBigIntand other types
alert(1n + 2); //Chyba: Nelze míchat dohromadyBigInta 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;
letnumber = 2;
letčíslo = 2;

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

// bigintto number
alert(Number(bigint) +number); // 3
// bigintna číslo
alert(Number(bigint) +číslo); // 3
```

The conversion operations are always silent, never give errors, but if thebigintis 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-libigintpří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` jebigint `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 (1nis considered truthy)
alert( 1n || 2 ); // 1 (1nse považuje za pravdivé)

alert( 0n || 2 ); // 2 (0nis considered falsy)
alert( 0n || 2 ); // 2 (0nse 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 abigint (rounded if necessary).
Například dělení bigintů vrátí vždybigint (zaokrouhlený, je-li to nutné).

To emulate such behavior, apolyfillwould 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.
Abypolyfillmohl 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 thepolyfill (Babel plugin) to convertJSBIcalls to native bigints for those browsers that support them.
...A pak použítpolyfill (zásuvný modul Babel) ke konverzi voláníJSBIna 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 JSBIworks 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 JSBIvnitř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 suchJSBIcode "as is" for engines that don't support bigints and for those that do support - thepolyfillwill convert the calls to native bigints.
Takový kód sJSBImůžeme používat beze změny na motorech, které biginty nepodporují, i na těch, které je podporují --polyfillbude konvertovat volání na nativní biginty.

##References
##Odkazy

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

[8]ページ先頭

©2009-2025 Movatter.jp