此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in English Always switch to English

Math.atan()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

Math.atan() 靜態方法回傳數字的反正切值(單位為弧度),也就是說:

𝙼𝚊𝚝𝚑.𝚊𝚝𝚊𝚗(𝚡)=arctan(x)=the unique y[π2,π2] such that tan(y)=x\mathtt{\operatorname{Math.atan}(x)} = \arctan(x) = \text{the unique } y \in \left[-\frac{\pi}{2}, \frac{\pi}{2}\right] \text{ such that } \tan(y) = x

嘗試一下

// 計算直角三角形的角度(以弧度表示)
function calcAngle(opposite, adjacent) {
  return Math.atan(opposite / adjacent);
}

console.log(calcAngle(8, 10));
// 預期輸出:0.6747409422235527

console.log(calcAngle(5, 3));
// 預期輸出:1.0303768265243125

語法

js
Math.atan(x)

參數

x

一個數字。

回傳值

x 的反正切值(單位為弧度,範圍包含 -π2-\frac{\pi}{2}π2\frac{\pi}{2})。如果 xInfinity,則回傳 π2\frac{\pi}{2}。如果 x-Infinity,則回傳 -π2-\frac{\pi}{2}

描述

由於 atan()Math 的靜態方法,你必須使用 Math.atan() 來呼叫它,而不是呼叫你所建立的 Math 物件的方法(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)

// 計算 (0,0) 到 (x,y) 之間的直線與 x 軸的角度
const theta = (x, y) => Math.atan(y / x);

注意,在某些情況下(例如 x0 時),theta 函式可能會回傳 NaN,因此建議使用 Math.atan2(),因為它的範圍更廣(-π 到 π)且能避免這類問題。

規範

Specification
ECMAScript® 2026 Language Specification
# sec-math.atan

瀏覽器相容性

參見