atan()
方法计算指定角度的反正切(正切的反函数)并返回该值。
示例
let num = Math.atan(1);
console.log(num);
//Output: 0.7853981633974483
atan() 语法
atan()
方法的语法是:
Math.atan(angle)
这里,atan()
是一个静态方法。因此,我们使用类名 Math
来访问该方法。
atan() 参数
atan()
方法接受一个参数:
angle
- 以弧度为单位,计算其反正切值
atan() 返回值
atan()
方法返回:
angle
参数的反正切值- 如果
x
是非数字值,则返回 NaN(非数字)。
注意:对于数值参数,返回的角度始终在 -π/2
到 π/2
的范围内。
示例 1: JavaScript Math.atan()
// compute arctangent of 0
let number1 = Math.atan(0);
console.log(number1);
// compute arctangent of -5
let number2 = Math.atan(-5);
console.log(number2);
// Output:
// 0
// -1.373400766945016
在上面的例子中:
Math.atan(0)
- 计算0
的反正切值Math.atan(-5)
- 计算-5
的反正切值
示例 2: Math.atan() 与 Infinity
// atan() with positive infinity
let number1 = Math.atan(Infinity);
console.log(number1);
// atan() with negative infinity
let number2 = Math.atan(-Infinity);
console.log(number2);
// Output:
// 1.5707963267948966 (π/2)
// -1.5707963267948966 (-π/2)
这里 math.atan()
方法计算无穷大的反正切值。正如你所见,输出值仍然在 -π/2 和 π/2 之间。
示例 3: Math.atan() 与非数字参数
// string variables
let string = "Dwayne"
// atan() with string arguments
let result = Math.atan(string);
console.log(result);
// Output: NaN
在上面的程序中,我们使用了字符串参数 Dwayne
和 atan()
。在这种情况下,输出为 NaN。
另请阅读