Math.acos()
Math.acos() 靜態方法返回一個數字的反餘弦值(以弧度為單位)。也就是說,
試一試
// Calculates angle of a right-angle triangle in radians
function calcAngle(adjacent, hypotenuse) {
return Math.acos(adjacent / hypotenuse);
}
console.log(calcAngle(8, 10));
// Expected output: 0.6435011087932843
console.log(calcAngle(5, 3));
// Expected output: NaN
語法
js
Math.acos(x)
引數
x-
一個介於 -1 和 1 之間(包括 -1 和 1)的數字,表示角度的餘弦值。
返回值
x 的反餘弦值(以弧度為單位,介於 0 和 π 之間,包括 0 和 π)。如果 x 小於 -1 或大於 1,則返回 NaN。
描述
因為 acos() 是 Math 的一個靜態方法,所以你總是使用 Math.acos() 來呼叫它,而不是透過你建立的 Math 物件來呼叫(Math 不是一個建構函式)。
示例
使用 Math.acos()
js
Math.acos(-2); // NaN
Math.acos(-1); // 3.141592653589793 (π)
Math.acos(0); // 1.5707963267948966 (π/2)
Math.acos(0.5); // 1.0471975511965979 (π/3)
Math.acos(1); // 0
Math.acos(2); // NaN
規範
| 規範 |
|---|
| ECMAScript® 2026 語言規範 # sec-math.acos |
瀏覽器相容性
載入中…