# Math.hypot()

## BaselineWidely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Math.hypot() 函数返回所有参数的平方和的平方根，即：

$𝙼𝚊𝚝𝚑.𝚑𝚢𝚙𝚘𝚝 ( v 1 , v 2 , … , v n ) = ∑ i = 1 n v i 2 = v 1 2 + v 2 2 + … + v n 2 \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}$

## 语法

Math.hypot([value1[,value2, ...]])


## 示例

### Using Math.hypot()

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, the same as Math.abs(-3)


## 向下兼容

js
if (!Math.hypot)
Math.hypot = function () {
var y = 0,
i = arguments.length;
while (i--) y += arguments[i] * arguments[i];
return Math.sqrt(y);
};


js
if (!Math.hypot)
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);
};


## 规范

Specification
ECMAScript Language Specification
# sec-math.hypot

## 浏览器兼容性

BCD tables only load in the browser