Math.hypot()
function returns the square root of the sum of squares of its arguments, that is:
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Math.hypot([value1[, value2[, ...]]])
value1, value2, ...
Numbers.
The square root of the sum of squares of the given arguments. If at least one of the arguments cannot be converted to a number,
NaN
被返回。
Calculating the hypotenuse of a right triangle, or the magnitude of a complex number, uses the formula
Math.sqrt(v1*v1 + v2*v2)
, where v1 and v2 are the lengths of the triangle's legs, or the complex number's real and complex components. The corresponding distance in 2 or more dimensions can be calculated by adding more squares under the square root:
Math.sqrt(v1*v1 + v2*v2 + v3*v3 + v4*v4)
.
This function makes this calculation easier and faster; you simply call
Math.hypot(v1, v2)
,或
Math.hypot(v1, v2, v3, v4, ...)
.
Math.hypot
also avoids overflow/underflow problems if the magnitude of your numbers is very large. The largest number you can represent in JS is
Number.MAX_VALUE
, which is around 10
308
. If your numbers are larger than about 10
154
, taking the square of them will result in Infinity. For example,
Math.sqrt(1e200*1e200 + 1e200*1e200) = Infinity
. If you use
hypot()
instead, you get better answer:
Math.hypot(1e200, 1e200) = 1.4142...e+200
. This is also true with very small numbers.
Math.sqrt(1e-200*1e-200 + 1e-200*1e-200) = 0
, but
Math.hypot(1e-200, 1e-200) =
1.4142...e-200
.
因为
hypot()
is a static method of
Math
, you always use it as
Math.hypot()
, rather than as a method of a
Math
object you created (
Math
is not a constructor).
If no arguments are given, the result is +0. If any of the arguments is ±Infinity, the result is Infinity. If any of the arguments is NaN (unless another argument is ±Infinity), the result is NaN. If at least one of the arguments cannot be converted to a number, the result is
NaN
.
With one argument,
Math.hypot()
相当于
Math.abs()
.
A naive approach that does not handle overflow/underflow issues:
if (!Math.hypot) Math.hypot = function() {
var y = 0, i = arguments.length, containsInfinity = false;
while (i--) {
var arg = arguments[i];
if (arg === Infinity || arg === -Infinity)
containsInfinity = true
y += arg * arg
}
return containsInfinity ? Infinity : Math.sqrt(y)
}
A polyfill that avoids underflows and overflows:
if (!Math.hypot) Math.hypot = function () {
var max = 0;
var s = 0;
var containsInfinity = false;
for (var i = 0; i < arguments.length; ++i) {
var arg = Math.abs(Number(arguments[i]));
if (arg === Infinity)
containsInfinity = true
if (arg > max) {
s *= (max / arg) * (max / arg);
max = arg;
}
s += arg === 0 && max === 0 ? 0 : (arg / max) * (arg / max);
}
return containsInfinity ? Infinity : (max === 1 / 0 ? 1 / 0 : max * Math.sqrt(s));
};
Math.hypot()
Math.hypot(3, 4); // 5 Math.hypot(3, 4, 5); // 7.0710678118654755 Math.hypot(); // 0 Math.hypot(NaN); // NaN Math.hypot(NaN, Infinity); // Infinity Math.hypot(3, 4, 'foo'); // NaN, since +'foo' => NaN Math.hypot(3, 4, '5'); // 7.0710678118654755, +'5' => 5 Math.hypot(-3); // 3, the same as Math.abs(-3)
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Math.hypot' in that specification. |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
hypot
|
Chrome 38 | Edge 12 | Firefox 27 | IE No | Opera 25 | Safari 8 | WebView Android 38 | Chrome Android 38 | Firefox Android 27 | Opera Android 25 | Safari iOS 8 | Samsung Internet Android 3.0 | nodejs 0.12 |
完整支持
不支持
Math
Math.abs()
Math.acos()
Math.acosh()
Math.asin()
Math.asinh()
Math.atan()
Math.atan2()
Math.atanh()
Math.cbrt()
Math.ceil()
Math.clz32()
Math.cos()
Math.cosh()
Math.exp()
Math.expm1()
Math.floor()
Math.fround()
Math.hypot()
Math.imul()
Math.log()
Math.log10()
Math.log1p()
Math.log2()
Math.max()
Math.min()
Math.pow()
Math.random()
Math.round()
Math.sign()
Math.sin()
Math.sinh()
Math.sqrt()
Math.tan()
Math.tanh()
Math.trunc()
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()