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

浏览器兼容性

另见