Math.hypot()
La funció Math.hypot()
retorna la rel quadrada de la suma dels quadrats dels seus arguments, és a dir:
Sintaxi
Math.hypot([valor1[, valor2[, ...]]])
Paràmetres
valor1, valor2, ...
- Nombres.
Descripció
Com que que hypot()
és un mètode estàtic de Math
, sempre s'utilitza com a Math.hypot()
, en comptes de com a mètode d'una instància de Math
(Math
no és un constructor).
Si no es passa cap argument, el resultat és +0.
Si al menys un dels arguments no pot ser convertit a nombre el resultat és NaN
.
Quan se li passa només un argument, Math.hypot()
retorna el mateix valor que retornaria Math.abs()
.
Exemples
Utilitzar Math.hypot()
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, el mateix que Math.abs(-3)
Polyfill
Aquest mètode pot emular-se mitjançant la funció següent:
Math.hypot = Math.hypot || function() {
var y = 0;
var length = arguments.length;
for (var i = 0; i < length; i++) {
if (arguments[i] === Infinity || arguments[i] === -Infinity) {
return Infinity;
}
y += arguments[i] * arguments[i];
}
return Math.sqrt(y);
};
Especificacions
Especificació | Estat | Comentaris |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Math.hypot' in that specification. |
Standard | Definició inicial. |
Compatibilitat amb navegadors
We're converting our compatibility data into a machine-readable JSON format.
This compatibility table still uses the old format,
because we haven't yet converted the data it contains.
Find out how you can help! (en-US)
Característica | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Suport bàsic | 38 | 27 (27) | No support | 25 | 7.1 |
Característica | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Suport bàsic | No support | No support | 27.0 (27) | No support | No support | 8 |