Math.atan()

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

Math.atan() 靜態方法返回一個數字的反正切(以弧度為單位),即

𝙼𝚊𝚝𝚑.𝚊𝚝𝚊𝚗(𝚡)=arctan(x)=唯一y[π2,π2]使得tan(y)=x\mathtt{\operatorname{Math.atan}(x)} = \arctan(x) = \text{唯一滿足 } \tan(y) = x \text{ 的 } y \in \left[-\frac{\pi}{2}, \frac{\pi}{2}\right]

試一試

// 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

一個數字。

返回值

一個數字的反正切(以弧度為單位,範圍在-π2-\frac{\pi}{2}andπ2\frac{\pi}{2}之間(含兩端))。如果 xInfinity,則返回π2\frac{\pi}{2}。如果 x-Infinity,則返回-π2-\frac{\pi}{2}.

描述

因為 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(),它具有更廣的範圍(-π 到 π 之間),並避免在 x0 等情況下輸出 NaN

規範

規範
ECMAScript® 2026 語言規範
# sec-math.atan

瀏覽器相容性

另見