aboutsummaryrefslogtreecommitdiff
path: root/files/pt-br/web/javascript/reference/global_objects/math/hypot
diff options
context:
space:
mode:
Diffstat (limited to 'files/pt-br/web/javascript/reference/global_objects/math/hypot')
-rw-r--r--files/pt-br/web/javascript/reference/global_objects/math/hypot/index.html115
1 files changed, 115 insertions, 0 deletions
diff --git a/files/pt-br/web/javascript/reference/global_objects/math/hypot/index.html b/files/pt-br/web/javascript/reference/global_objects/math/hypot/index.html
new file mode 100644
index 0000000000..b83197c861
--- /dev/null
+++ b/files/pt-br/web/javascript/reference/global_objects/math/hypot/index.html
@@ -0,0 +1,115 @@
+---
+title: Math.hypot()
+slug: Web/JavaScript/Reference/Global_Objects/Math/hypot
+tags:
+ - JavaScript
+ - Math
+ - Method
+ - Reference
+translation_of: Web/JavaScript/Reference/Global_Objects/Math/hypot
+---
+<div>{{JSRef}}</div>
+
+<p>A função <strong><code>Math.hypot()</code></strong> retorna a raiz quadrada do somátorio do quadrado de seus parâmetros, ou seja</p>
+
+<p><math display="block"><semantics><mrow><mstyle mathvariant="monospace"><mrow><mo lspace="0em" rspace="thinmathspace">Math.hypot</mo><mo stretchy="false">(</mo><msub><mi>v</mi><mn>1</mn></msub><mo>,</mo><msub><mi>v</mi><mn>2</mn></msub><mo>,</mo><mo>…</mo><mo>,</mo><msub><mi>v</mi><mi>n</mi></msub><mo stretchy="false">)</mo></mrow></mstyle><mo>=</mo><msqrt><mrow><munderover><mo>∑</mo><mrow><mi>i</mi><mo>=</mo><mn>1</mn></mrow><mi>n</mi></munderover><msubsup><mi>v</mi><mi>i</mi><mn>2</mn></msubsup></mrow></msqrt><mo>=</mo><msqrt><mrow><msubsup><mi>v</mi><mn>1</mn><mn>2</mn></msubsup><mo>+</mo><msubsup><mi>v</mi><mn>2</mn><mn>2</mn></msubsup><mo>+</mo><mo>…</mo><mo>+</mo><msubsup><mi>v</mi><mi>n</mi><mn>2</mn></msubsup></mrow></msqrt></mrow><annotation encoding="TeX">\mathtt{\operatorname{Math.hypot}(v_1, v_2, \dots, v_n)} = \sqrt{\sum_{i=1}^n v_i^2} = \sqrt{v_1^2 + v_2^2 + \dots + v_n^2}</annotation></semantics></math></p>
+
+<h2 id="Sintaxe">Sintaxe</h2>
+
+<pre class="syntaxbox"><code>Math.hypot([<var>value1</var>[, <var>value2</var>[, ...]]])</code></pre>
+
+<h3 id="Parâmetros">Parâmetros</h3>
+
+<dl>
+ <dt><code>value1, value2, ...</code></dt>
+ <dd>Números.</dd>
+</dl>
+
+<h3 id="Valor_retornado">Valor retornado</h3>
+
+<p>A raiz quadrada do somátorio do quadrado dos parâmetros recebidos. Se um ou mais argumentos não puderem ser convertidos para um número, {{jsxref("NaN")}} é retornado.</p>
+
+<h2 id="Descrição">Descrição</h2>
+
+<p>Para calcular a hipotenusa de um triângulo retângulo, ou o módulo de um número complexo, é usada a fórmula <code>Math.sqrt(v1*v1 + v2*v2)</code> (<math><semantics><msqrt><mrow><mi>v</mi><msup><mn>1</mn><mn>2</mn></msup><mo>+</mo><mi>v</mi><msup><mn>2</mn><mn>2</mn></msup></mrow></msqrt><annotation encoding="TeX">\sqrt{v1^2 + v2^2}</annotation></semantics></math>) onde v1 e v2 são, ou os lados de um triângulo, ou a parte real e a imaginário de um número complexo. Para calcular a distância entre duas ou mais dimensões, basta adicionar mais exponenciações dentro da raiz quadrada, por exemplo <code>Math.sqrt(v1*v1 + v2*v2 + v3*v3 + v4*v4)</code> (<math><semantics><msqrt><mrow><mi>v</mi><msup><mn>1</mn><mn>2</mn></msup><mo>+</mo><mi>v</mi><msup><mn>2</mn><mn>2</mn></msup><mo>+</mo><mi>v</mi><msup><mn>3</mn><mn>2</mn></msup><mo>+</mo><mi>v</mi><msup><mn>4</mn><mn>2</mn></msup></mrow></msqrt><annotation encoding="TeX">\sqrt{v1^2 + v2^2 + v3^2 + v4^2}</annotation></semantics></math>).</p>
+
+<p>A função Math.hypot() torna esta tarefa mais rápida e mais fácil, basta executar <code>Math.hypot(v1, v2)</code> , or <code>Math.hypot(v1, v2, v3, v4, ...)</code> .</p>
+
+<p>Dessa maneira também se evita problemas se a magnitude dos seus número for muito grande. O maio número que se pode representar em um <em>double float</em> em JavasScript é <code>Number.MAX_VALUE</code> = <span style="line-height: 1.5;">1.797...e+308</span>. Se os seu números são maior que 1e154, calcular o quadrado deles resultará em <code>Infinity</code>, estragando os seus resultados. Por exemplo, <span style="line-height: 1.5;"><code>Math.sqrt(1e200*1e200 + 1e200*1e200) = Infinity</code>. Se você usar a função <code>Math.hypot()</code>, você receberá uma resposta aceitável: <code>Math.hypot(1e200, 1e200) = 1.4142...e+200</code>.</span> Isto também é verdade para número muito pequenos. <span style="line-height: 1.5;"><code>Math.sqrt(1e-200*1e-200 + 1e-200*1e-200) = 0</code>, mas </span><code><span style="line-height: 1.5;">Math.hypot(1e-200, 1e-200) = </span></code><span style="line-height: 1.5;"><code>1.4142...e-200</code> é uma boa resposta.</span></p>
+
+<div class="note">
+<p>Por <code>hypot()</code> ser um método estático de <code>Math</code>, deve-se sempre usá-lo como <code>Math.hypot()</code>, e não como um método de um objeto <code>Math</code> que você criou.</p>
+</div>
+
+<p>Se nenhum parâmetro for passado, o resultado é +0.</p>
+
+<p>Se um ou mais parâmetros não puderem ser convertidos para um número, o resultado será {{jsxref("NaN")}}.</p>
+
+<p>Com apenas um parâmetro, <code>Math.hypot()</code> se comporta como <code>Math.abs()</code>.</p>
+
+<h2 id="Examples">Examples</h2>
+
+<h3 id="Usando_Math.hypot()">Usando <code>Math.hypot()</code></h3>
+
+<pre class="brush: js">Math.hypot(3, 4); // 5
+Math.hypot(3, 4, 5); // 7.0710678118654755
+Math.hypot(); // 0
+Math.hypot(NaN); // NaN
+Math.hypot(3, 4, 'foo'); // NaN, +'foo' =&gt; NaN
+Math.hypot(3, 4, '5'); // 7.0710678118654755, +'5' =&gt; 5
+Math.hypot(-3); // 3, the same as Math.abs(-3)
+</pre>
+
+<h2 id="Polyfill">Polyfill</h2>
+
+<p>O comportamento de <code>Math.hypot()</code> pode ser emulado com a seguinte função:</p>
+
+<pre class="brush: js">Math.hypot = Math.hypot || function() {
+ var y = 0;
+ var length = arguments.length;
+
+ for (var i = 0; i &lt; length; i++) {
+ if (arguments[i] === Infinity || arguments[i] === -Infinity) {
+ return Infinity;
+ }
+ y += arguments[i] * arguments[i];
+ }
+ return Math.sqrt(y);
+};
+</pre>
+
+<h2 id="Especificações">Especificações</h2>
+
+<table class="standard-table">
+ <tbody>
+ <tr>
+ <th scope="col">Specification</th>
+ <th scope="col">Status</th>
+ <th scope="col">Comment</th>
+ </tr>
+ <tr>
+ <td>{{SpecName('ES2015', '#sec-math.hypot', 'Math.hypot')}}</td>
+ <td>{{Spec2('ES2015')}}</td>
+ <td>Definição inicial.</td>
+ </tr>
+ <tr>
+ <td>{{SpecName('ESDraft', '#sec-math.hypot', 'Math.hypot')}}</td>
+ <td>{{Spec2('ESDraft')}}</td>
+ <td> </td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Compatibilidade_no_navegador">Compatibilidade no navegador</h2>
+
+<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p>
+
+<p>{{Compat("javascript.builtins.Math.hypot")}}</p>
+
+<h2 id="Ver_também">Ver também</h2>
+
+<ul>
+ <li>{{jsxref("Math.abs()")}}</li>
+ <li>{{jsxref("Math.pow()")}}</li>
+ <li>{{jsxref("Math.sqrt()")}}</li>
+</ul>