diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:52 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:52 -0500 |
| commit | 074785cea106179cb3305637055ab0a009ca74f2 (patch) | |
| tree | e6ae371cccd642aa2b67f39752a2cdf1fd4eb040 /files/pt-br/web/javascript/reference/global_objects/bigint | |
| parent | da78a9e329e272dedb2400b79a3bdeebff387d47 (diff) | |
| download | translated-content-074785cea106179cb3305637055ab0a009ca74f2.tar.gz translated-content-074785cea106179cb3305637055ab0a009ca74f2.tar.bz2 translated-content-074785cea106179cb3305637055ab0a009ca74f2.zip | |
initial commit
Diffstat (limited to 'files/pt-br/web/javascript/reference/global_objects/bigint')
| -rw-r--r-- | files/pt-br/web/javascript/reference/global_objects/bigint/index.html | 240 | ||||
| -rw-r--r-- | files/pt-br/web/javascript/reference/global_objects/bigint/prototype/index.html | 61 |
2 files changed, 301 insertions, 0 deletions
diff --git a/files/pt-br/web/javascript/reference/global_objects/bigint/index.html b/files/pt-br/web/javascript/reference/global_objects/bigint/index.html new file mode 100644 index 0000000000..fc9cd37807 --- /dev/null +++ b/files/pt-br/web/javascript/reference/global_objects/bigint/index.html @@ -0,0 +1,240 @@ +--- +title: BigInt +slug: Web/JavaScript/Reference/Global_Objects/BigInt +translation_of: Web/JavaScript/Reference/Global_Objects/BigInt +--- +<div>{{JSRef}}</div> + +<p><code>BigInt</code> é um objeto nativo que fornece um modo de representar números inteiros maiores que 2<sup>53</sup>, que é o maior número que o JavaScript consegue, com exatidão, representar com o tipo primitivo {{jsxref("Number")}}.</p> + +<h2 id="Sintaxe">Sintaxe</h2> + +<pre class="brush: js">BigInt(value); +</pre> + +<h3 id="Parâmetros">Parâmetros</h3> + +<dl> + <dt><code>value</code></dt> + <dd>O valor numérico do objeto que está sendo criado. Pode ser uma <em>string</em> ou um número inteiro.</dd> +</dl> + +<div class="blockIndicator note"> +<p><strong>Observação</strong>: <code>BigInt()</code> não é usado com o operador {{jsxref("Operators/new", "new")}}.</p> +</div> + +<dl> +</dl> + +<h2 id="Descrição">Descrição</h2> + +<p>Um <code>BigInt</code> é criado com a acrescentação de <code>n</code> ao final de um inteiro literal — <code>10n</code> — ou chamando a função <code>BigInt()</code>.</p> + +<pre class="brush: js">const theBiggestInt = 9007199254740991n; + +const alsoHuge = BigInt(9007199254740991); +// ↪ 9007199254740991n + +const hugeString = BigInt("9007199254740991"); +// ↪ 9007199254740991n + +const hugeHex = BigInt("0x1fffffffffffff"); +// ↪ 9007199254740991n + +const hugeBin = BigInt("0b11111111111111111111111111111111111111111111111111111"); +// ↪ 9007199254740991n +</pre> + +<p>Isso é parecido com um {{jsxref("Number")}} em algumas partes, mas difere-se em alguns assuntos importantes — ele não pode ser usado com métodos no objeto {{jsxref("Math")}} e não pode ser misturado em operações ou qualquer instância de <code>Number</code>.</p> + +<div class="blockIndicator warning"> +<p>{{jsxref("Number")}} e <code>BigInt</code> não podem ser misturados em operações — eles devem ser manipulados com o mesmo tipo.</p> + +<p>Tenha cuidado com a conversão e desconversão de valores, visto que a precisão de <code>BigInt</code> pode ser perdida com a conversào para <code>Number</code>.</p> +</div> + +<h3 id="Informações_do_tipo">Informações do tipo</h3> + +<p>Quando testado com <code>typeof</code> , um <code>BigInt</code> vai devolver "bigint":</p> + +<pre class="brush: js">typeof 1n === 'bigint'; // true +typeof BigInt('1') === 'bigint'; // true +</pre> + +<p>Quando envolvido em um <code>Object</code>, um <code>BigInt</code> vai ser considerado como um tipo normal de "object".</p> + +<pre class="brush: js">typeof Object(1n) === 'object'; // true +</pre> + +<h3 id="Operadores">Operadores</h3> + +<p>Os seguintes operadores podem ser usados com <code>BigInt</code>s (ou com <code>BigInt</code>s envolvidos em objetos): <code>+</code>, `<code>*</code>`, `<code>-</code>`, `<code>**</code>`, `<code>%</code>` .</p> + +<pre class="brush: js">const previousMaxSafe = BigInt(Number.MAX_SAFE_INTEGER); +// ↪ 9007199254740991 + +const maxPlusOne = previousMaxSafe + 1n; +// ↪ 9007199254740992n + +const theFuture = previousMaxSafe + 2n; +// ↪ 9007199254740993n, isso funciona agora! + +const multi = previousMaxSafe * 2n; +// ↪ 18014398509481982n + +const subtr = multi – 10n; +// ↪ 18014398509481972n + +const mod = multi % 10n; +// ↪ 2n + +const bigN = 2n ** 54n; +// ↪ 18014398509481984n + +bigN * -1n +// ↪ –18014398509481984n +</pre> + +<p>O operador <code>/</code> também funciona com o esperado com números inteiros. No entanto, desde que esses sejam <code>BigInt</code>s e não <code>BigDecimal</code>s, essa operação vai arredondar para 0, o que significa que não vai retornar qualquer valor fracional.</p> + +<div class="blockIndicator warning"> +<p>Uma operação com um resultado fracional será arredondado com <code>BigInt.</code></p> +</div> + +<pre class="brush: js">const expected = 4n / 2n; +// ↪ 2n + +const rounded = 5n / 2n; +// ↪ 2n, e não 2.5n + +</pre> + +<h3 id="Comparações">Comparações</h3> + +<p>Um <code>BigInt</code> não é estritamente igual a um {{jsxref("Global_Objects/Number", "Number")}}, mas é mais ou menos assim.</p> + +<pre class="brush: js">0n === 0 +// ↪ false + +0n == 0 +// ↪ true</pre> + +<p>Um {{jsxref("Global_Objects/Number", "Number")}} e um <code>BigInt</code> podem ser comparado normalmente.</p> + +<pre class="brush: js">1n < 2 +// ↪ true + +2n > 1 +// ↪ true + +2 > 2 +// ↪ false + +2n > 2 +// ↪ false + +2n >= 2 +// ↪ true</pre> + +<p>Eles podem ser misturados em <em>arrays</em> e sorteados.</p> + +<pre class="brush: js">const mixed = [4n, 6, -12n, 10, 4, 0, 0n]; +// ↪ [4n, 6, -12n, 10, 4, 0, 0n] + +mixed.sort(); +// ↪ [-12n, 0, 0n, 10, 4n, 4, 6] +</pre> + +<p>Observe que comparações com <code>BigInt</code>s envolvidos em <code>Object</code> atuam com outros objetos, indicando somente a igualdade onde a mesma instância do objeto é comparada.</p> + +<pre class="brush: js">0n === Object(0n); // false +Object(0n) === Object(0n); // false + +const o = Object(0n); +o === o // true +</pre> + +<h3 id="Conditionals">Conditionals</h3> + +<p>A <code>BigInt</code> behaves like a {{jsxref("Global_Objects/Number", "Number")}} in cases where it is converted to a {{jsxref("Global_Objects/Boolean", "Boolean")}}: via the {{jsxref("Global_Objects/Boolean", "Boolean")}} function; when used with logical operators {{jsxref("Operators/Logical_Operators", "Logical Operators")}} <code>||</code>, `<code>&&</code>`, and <code>!</code>; or within a conditional test like an {{jsxref("Statements/if...else", "if statement")}}.</p> + +<pre class="brush: js">if (0n) { + console.log('Hello from the if!'); +} else { + console.log('Hello from the else!'); +} + +// ↪ "Hello from the else!" + +0n || 12n +// ↪ 12n + +0n && 12n +// ↪ 0n + +Boolean(0n) +// ↪ false + +Boolean(12n) +// ↪ true + +!12n +// ↪ false + +!0n +// ↪ true +</pre> + +<h2 id="Methods">Methods</h2> + +<dl> + <dt><strong><code>BigInt.asIntN()</code></strong></dt> + <dd>Wraps a BigInt between -2<sup>width-1</sup> and 2<sup>width-1</sup>-1</dd> + <dt><code>BigInt.asUintN()</code></dt> + <dd>Wraps a BigInt between 0 and 2<sup>width</sup>-1</dd> +</dl> + +<h2 id="Properties">Properties</h2> + +<dl> + <dt>{{jsxref("BigInt.prototype")}}</dt> + <dd><span style="letter-spacing: -0.00278rem;">Allows the addition of properties to a </span><code style="letter-spacing: -0.00278rem;">BigInt</code><span style="letter-spacing: -0.00278rem;"> object.</span></dd> +</dl> + +<h2 id="BigInt_instances"><code>BigInt</code> instances</h2> + +<p>All <code>BigInt</code> instances inherit from <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">BigInt.prototype</span></font>. The prototype object of the <font face="consolas, Liberation Mono, courier, monospace"><span style="background-color: rgba(220, 220, 220, 0.5);">BigInt</span></font> constructor can be modified to affect all <code>BigInt</code> instances.</p> + +<h3 id="Methods_2">Methods</h3> + +<p>{{page('/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt/prototype', 'Methods')}}</p> + +<h2 id="Examples">Examples </h2> + +<h3 id="Calculating_Primes">Calculating Primes</h3> + +<pre class="brush: js">function isPrime(p) { + for (let i = 2n; i * i <= p; i++) { + if (p % i === 0n) return false; + } + return true; +} + +// Takes a BigInt as an argument and returns a BigInt +function nthPrime(nth) { + let maybePrime = 2n; + let prime = 0n; + + while (nth >= 0n) { + if (isPrime(maybePrime)) { + nth -= 1n; + prime = maybePrime; + } + maybePrime += 1n; + } + + return prime; +} + +nthPrime(20n) +// ↪ 73n</pre> diff --git a/files/pt-br/web/javascript/reference/global_objects/bigint/prototype/index.html b/files/pt-br/web/javascript/reference/global_objects/bigint/prototype/index.html new file mode 100644 index 0000000000..ff8de05541 --- /dev/null +++ b/files/pt-br/web/javascript/reference/global_objects/bigint/prototype/index.html @@ -0,0 +1,61 @@ +--- +title: BigInt.prototype +slug: Web/JavaScript/Reference/Global_Objects/BigInt/prototype +tags: + - BigInt + - JavaScript + - Propriedade + - Prototipo + - Referencia +translation_of: Web/JavaScript/Reference/Global_Objects/BigInt/prototype +--- +<div>{{JSRef}}</div> + +<p>A propriedade <strong><code>BigInt.prototype</code></strong> representa o protótipo para o construtor {{JSxRef("BigInt")}} .</p> + +<p>{{JS_Property_Attributes(0, 0, 0)}}</p> + +<h2 id="Descrição">Descrição</h2> + +<p>Todas instância de {{JSxRef("BigInt")}} herdam de <code>BigInt.prototype</code>. O objeto protótipo do construtor {{JSxRef("BigInt")}} pode ser modificado para afetar todas instâncias de {{JSxRef( "BigInt")}} .</p> + +<h2 id="Propriedades">Propriedades</h2> + +<dl> + <dt><code>BigInt.prototype.constructor</code></dt> + <dd>Retorna a função que cria instâncias deste objeto. Por padrão é o objeto<br> + {{JSxRef("BigInt")}}.</dd> +</dl> + +<h2 id="Métodos">Métodos</h2> + +<dl> + <dt>{{JSxRef("BigInt.prototype.toLocaleString()")}}</dt> + <dd>Retorna uma string com uma representação sensível ao idioma para este número. Sobrescreve o método {{JSxRef("Object.prototype.toLocaleString()")}}<br> + </dd> + <dt>{{JSxRef("BigInt.prototype.toString()")}}</dt> + <dd>Retorna uma string respresentando o objeto específicado em um base específica. Sobrescreve o método {{JSxRef("Object.prototype.toString()")}} .</dd> + <dt>{{JSxRef("BigInt.prototype.valueOf()")}}</dt> + <dd>Retorna o valor primitivo de um objeto específicado. Sobrescreve o método {{JSxRef("Object.prototype.valueOf()")}}.</dd> +</dl> + +<h2 id="Especificações">Especificações</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Especificações</th> + <th scope="col">Estado</th> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-bigint.prototype', 'BigInt.prototype')}}</td> + <td>{{Spec2('ESDraft')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Compatibilidade">Compatibilidade</h2> + +<p class="hidden">Para contribuir para este dado de compatibilidade, por favor escreva um pull resquest para este repositório: <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a>.</p> + +<p>{{Compat("javascript.builtins.BigInt.prototype")}}</p> |
