JavaScript 中 Math.hypot() 函式的作用是什麼?
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
廣告