JavaScript 中的 Math.hypot() 函数有什么用?
javascriptobject oriented programmingfront end technology更新于 2025/8/29 9:48:17
Math.hypot()
Math.Hypot() 方法用于求作为参数传递给它的元素平方和的平方根。此方法实际上用于查找直角三角形的斜边,该三角形的边作为参数传递给它。
语法
Math.hypot(arg1, arg2,....);
示例
在下面的示例中,传递了直角三角形的边来查找斜边。如果任何值无法转换为数字,则 NaN 将显示为输出。
<html>
<body>
<script>
document.write(Math.hypot(7, 24));
document.write("</br>");
document.write(Math.hypot(7, "hi"));
</script>
</body>
</html>
输出
25 NaN
此方法甚至接受负值作为参数,并尝试将其某些平方的平方根作为输出。
示例
<html>
<body>
<script>
document.write(Math.hypot(-7, -24));
document.write("</br>")
document.write(Math.hypot(-3, -4))
</script>
</body>
</html>
输出
25 5
此方法可以采用多个值(超过两个),并尝试给出其中一些平方的平方根。
示例
<html> <body> <script> document.write(Math.hypot(1, 2, 3)); </script> </body> </html>
输出
3.74165738677
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

