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 |
浏览器兼容性
加载中…