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 July 2015.

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

嘗試一下

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

一個數字。

回傳值

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® 2025 Language Specification
# sec-math.atan

瀏覽器相容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
atan

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

參見