diff options
Diffstat (limited to 'files/de/web/javascript/reference/global_objects/math/hypot')
| -rw-r--r-- | files/de/web/javascript/reference/global_objects/math/hypot/index.html | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/files/de/web/javascript/reference/global_objects/math/hypot/index.html b/files/de/web/javascript/reference/global_objects/math/hypot/index.html new file mode 100644 index 0000000000..b88861e06d --- /dev/null +++ b/files/de/web/javascript/reference/global_objects/math/hypot/index.html @@ -0,0 +1,128 @@ +--- +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>Die <strong><code>Math.hypot()</code></strong> Funktion gibt die Quadratwurzel von der Summe der quadrierten Argumente zurück. Das bedeutet</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> + +<div>{{EmbedInteractiveExample("pages/js/math-hypot.html")}}</div> + + + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox"><code>Math.hypot([<var>value1</var>[, <var>value2</var>[, ...]]])</code></pre> + +<h3 id="Parameter">Parameter</h3> + +<dl> + <dt><code>value1, value2, ...</code></dt> + <dd>Zahlen.</dd> +</dl> + +<h3 id="Rückgabewert">Rückgabewert</h3> + +<p>Die Quadratwurzel der Summe der quadrierten übergebenen Parameter. Wenn ein Parameter nicht in eine Zahl konvertiert werden kann, wird {{jsxref("NaN")}} zurückgegeben.</p> + +<h2 id="Beschreibung">Beschreibung</h2> + +<p><span id="result_box" lang="de"><span>Das Berechnen der Hypotenuse eines rechtwinkligen Dreiecks oder die Größe einer komplexen Zahl verwendet die Formel <code>Math.sqrt (v1 * v1 + v2 * v2)</code>, wobei v1 und v2 entweder die Seiten des Dreiecks oder die reellen und komplexen Werte sind. </span></span><span id="result_box" lang="de"><span>Zum Berechnen der Entfernung in 2 oder mehr Dimensionen fügt man einfach weitere Quadrate innerhalb des Quadratwurzelzeichens ein, wie z. B. <code>Math.sqrt (v1 * v1 + v2 * v2 + v3 * v3 + v4 * v4)</code>.</span></span></p> + +<p>Diese Funktion macht es ein bisschen einfacher und schneller, man kann einfach <code>Math.hypot(v1, v2)</code> , or <code>Math.hypot(v1, v2, v3, v4, ...)</code> aufrufen.</p> + +<p><span id="result_box" lang="de"><span>Sie vermeidet zudem ein Problem, wenn Zahlen sehr groß werden. Die größte Zahl, die in JavaScript dargestellt werden kann ist </span></span><span style="line-height: 1.5;"><code>Number.MAX_VALUE = 1.797...e+308</code>. Wenn die Zahlen größer als <code>1e154</code> sind, wird das Quadrieren</span> dieser zu dem ergebnis <code><span style="line-height: 1.5;">Infinity</span></code><span style="line-height: 1.5;"> führen und das Ergebnis zerstören. Zum Beispiel: <code>Math.sqrt(1e200*1e200 + 1e200*1e200) = Infinity</code>. Wenn <code>hypot()</code> stattdessen benutzt wird bekommt man ein gutes Ergebnis: <code>Math.hypot(1e200, 1e200) = 1.4142...e+200</code>. Diese gilt auch für sehr kleine Zahlen. <code>Math.sqrt(1e-200*1e-200 + 1e-200*1e-200) = 0</code>, wohingegen </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> eine gute Lösung ist.</span></p> + +<p>Weil <code>hypot()</code> eine statische Funktion von <code>Math</code> ist, wird es immer als <code>Math.hypot()</code> eingesetzt<code>,</code> jedoch nicht als Methode eines erzeugten <code>Math</code> Objektes (<code>Math</code> ist kein Konstruktor).</p> + +<p>Wenn kein Argument übergeben wird, ist das Ergebnis <code>+0</code>.</p> + +<p>Wenn einer der übergebenen Parameter nicht zu einer Zahl konvertiert werden kann, ist das Ergebnis {{jsxref("NaN")}}.</p> + +<p>Wenn nur ein Argument übergeben wird, so sind die Ergebnisse der Funktionen <code>Math.hypot()</code> und {{jsxref("Math.abs()")}} gleich.</p> + +<h2 id="Beispiele">Beispiele</h2> + +<h3 id="Einsatz_von_Math.hypot()">Einsatz von <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' => NaN +Math.hypot(3, 4, '5'); // 7.0710678118654755, +'5' => 5 +Math.hypot(-3); // 3, das gleiche wie Math.abs(-3) +</pre> + +<h2 id="Polyfill">Polyfill</h2> + +<p>Diese Funktion kann folgendermaßen emuliert werden:</p> + +<pre class="brush: js">Math.hypot = Math.hypot || function() { + var y = 0, i = arguments.length; + while (i--) y += arguments[i] * arguments[i]; + return Math.sqrt(y); +}; +</pre> + +<p>Ein Polyfill der Underflows und Overflows vermeidet:</p> + +<pre class="brush: js">Math.hypot = function (x, y) { + // https://bugzilla.mozilla.org/show_bug.cgi?id=896264#c28 + var max = 0; + var s = 0; + for (var i = 0; i < arguments.length; i += 1) { + var arg = Math.abs(Number(arguments[i])); + if (arg > max) { + s *= (max / arg) * (max / arg); + max = arg; + } + s += arg === 0 && max === 0 ? 0 : (arg / max) * (arg / max); + } + return max === 1 / 0 ? 1 / 0 : max * Math.sqrt(s); +}; +</pre> + +<h2 id="Spezifikationen">Spezifikationen</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Spezifikation</th> + <th scope="col">Status</th> + <th scope="col">Kommentar</th> + </tr> + <tr> + <td>{{SpecName('ES2015', '#sec-math.hypot', 'Math.hypot')}}</td> + <td>{{Spec2('ES2015')}}</td> + <td>Initiale Definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-math.hypot', 'Math.hypot')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browserkompatibilität">Browserkompatibilität</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="Siehe_auch">Siehe auch</h2> + +<ul> + <li>{{jsxref("Math.abs()")}}</li> + <li>{{jsxref("Math.pow()")}}</li> + <li>{{jsxref("Math.sqrt()")}}</li> +</ul> |
