Math.atan()
Math.atan() 靜態方法返回一個數字的反正切(以弧度為單位),即
試一試
// Calculates angle of a right-angle triangle in radians
function calcAngle(opposite, adjacent) {
return Math.atan(opposite / adjacent);
}
console.log(calcAngle(8, 10));
// Expected output: 0.6747409422235527
console.log(calcAngle(5, 3));
// Expected output: 1.0303768265243125
語法
js
Math.atan(x)
引數
x-
一個數字。
返回值
一個數字的反正切(以弧度為單位,範圍在and之間(含兩端))。如果 x 是 Infinity,則返回。如果 x 是 -Infinity,則返回.
描述
因為 atan() 是 Math 的靜態方法,所以您總是使用 Math.atan() 來呼叫它,而不是作為您建立的 Math 物件的 a 方法(Math 不是一個建構函式)。
示例
使用 Math.atan()
js
Math.atan(-Infinity); // -1.5707963267948966 (-π/2)
Math.atan(-0); // -0
Math.atan(0); // 0
Math.atan(1); // 0.7853981633974483 (π/4)
Math.atan(Infinity); // 1.5707963267948966 (π/2)
// The angle that the line (0,0) -- (x,y) forms with the x-axis in a Cartesian coordinate system
const theta = (x, y) => Math.atan(y / x);
請注意,您可能想避免使用 theta 函式,而是使用 Math.atan2(),它具有更廣的範圍(-π 到 π 之間),並避免在 x 為 0 等情況下輸出 NaN。
規範
| 規範 |
|---|
| ECMAScript® 2026 語言規範 # sec-math.atan |
瀏覽器相容性
載入中…