atanh()
方法计算指定值的双曲反正切并返回它。
示例
// hyperbolic arctangent of 0.75
let number = Math.atanh(0.75);
console.log(number);
// Output: 0.9729550745276566
atanh() 语法
Math.atanh()
方法的语法是:
Math.atanh(number)
这里,atanh()
是一个静态方法。因此,我们使用类名 Math
来访问该方法。
atanh() 参数
atanh()
方法接受一个参数:
number
- 要计算其双曲反正切的值,取值范围为 -1 到 1。
Math.atanh()返回值
atanh()
方法返回:
- 参数
number
的双曲反正切值。 - 如果参数是非数字,或者大于 1,或者小于 -1,则返回 NaN(非数字)。
示例 1:JavaScript Math.atanh() 配合 -1 和 1 之间的参数
// hyperbolic arctangent of 0
let number1 = Math.atanh(0);
console.log(number1);
// hyperbolic arctangent of 0.5
let number2 = Math.atanh(0.5);
console.log(number2);
// Output:
// 0
// 0.5493061443340548
在上面的例子中:
Math.atanh(0)
- 计算 0 的双曲反正切。Math.atanh(0.5)
- 计算 0.5 的双曲反正切。
注意:双曲反正切,即 atanh(number)
,是范围 [-1, 1] 内的一个唯一数字 'x',使得 tanh(x)
= number
。
示例 2:Math.atanh() 配合不在 -1 到 1 范围内的参数
// argument greater than 1
let number1 = Math.atanh(2);
console.log(number1);
// argument less than -1
let number2 = Math.atanh(-4);
console.log(number2);
// Output:
// NaN
// NaN
atanh()
方法返回 NaN,因为参数 2 和 -4 都不在 -1 和 1 的范围内。
示例 3:Math.atanh() 配合参数 -1 和 1
// atanh() with argument -1
let number1 = Math.atanh(-1);
console.log(number1);
// atanh() with argument 1
let number2 = Math.atanh(1);
console.log(number2);
// Output:
// -Infinity
// Infinity
atanh()
方法返回:
- 如果参数是 1,则返回 Infinity。
- 如果参数是 -1,则返回 -Infinity。
示例 4:Math.atanh() 配合非数字参数
let string = "Harry";
// atanh() with string argument
let value = Math.atanh(string);
console.log(value);
// Output: NaN
在上面的例子中,我们尝试计算字符串 "Harry"
的双曲反正切。因此,输出为 NaN。
另请阅读