Math.hypot()

Math.hypot() 静态方法返回其参数平方和的平方根。那是,

¥The Math.hypot() static method returns the square root of the sum of squares of its arguments. That is,

𝙼𝚊𝚝𝚑.𝚑𝚢𝚙𝚘𝚝 ( 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}

Try it

语法

¥Syntax

js
Math.hypot()
Math.hypot(value1)
Math.hypot(value1, value2)
Math.hypot(value1, value2, /* …, */ valueN)

参数

¥Parameters

value1, …, valueN

数字。

返回值

¥Return value

给定参数的平方和的平方根。如果任何参数为 ±Infinity,则返回 Infinity。否则,如果至少一个参数是或已转换为 NaN,则返回 NaN。如果未给出参数或所有参数均为 ±0,则返回 0

¥The square root of the sum of squares of the given arguments. Returns Infinity if any of the arguments is ±Infinity. Otherwise, if at least one of the arguments is or is converted to NaN, returns NaN. Returns 0 if no arguments are given or all arguments are ±0.

描述

¥Description

使用公式 Math.sqrt(v1*v1 + v2*v2) 计算直角三角形的斜边或复数的大小,其中 v1 和 v2 是三角形边的长度,或复数的实数和复数分量。二维或更多维度中的相应距离可以通过在平方根下添加更多平方来计算:Math.sqrt(v1*v1 + v2*v2 + v3*v3 + v4*v4)

¥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).

该功能使计算变得更容易、更快捷;你调用 Math.hypot(v1, v2)Math.hypot(v1, /* …, */, vN)

¥This function makes this calculation easier and faster; you call Math.hypot(v1, v2), or Math.hypot(v1, /* …, */, vN).

如果数字的大小非常大,Math.hypot 还可以避免上溢/下溢问题。在 JS 中可以表示的最大数字是 Number.MAX_VALUE,大约是 10308。如果你的数字大于大约 10154,则取它们的平方将得到无穷大。例如,Math.sqrt(1e200*1e200 + 1e200*1e200) = Infinity。如果你改用 hypot(),你会得到更好的答案:Math.hypot(1e200, 1e200) = 1.4142...e+200. 对于极少数的情况也是如此。Math.sqrt(1e-200*1e-200 + 1e-200*1e-200) = 0,但是 Math.hypot(1e-200, 1e-200) = 1.4142...e-200

¥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 10308. If your numbers are larger than about 10154, 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 a 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.

通过一个参数,Math.hypot() 相当于 Math.abs()Math.hypot.length 是 2,这微弱地表明它被设计为处理至少两个参数。

¥With one argument, Math.hypot() is equivalent to Math.abs(). Math.hypot.length is 2, which weakly signals that it's designed to handle at least two parameters.

因为 hypot()Math 的静态方法,所以你始终将其用作 Math.hypot(),而不是用作你创建的 Math 对象的方法(Math 不是构造函数)。

¥Because 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).

示例

¥Examples

使用 Math.hypot()

¥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(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)

规范

Specification
ECMAScript Language Specification
# sec-math.hypot

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看