JavaScript Math.atanh() 方法接受一个数值并计算数字的双曲反正切(反双曲正切)。提供的数值应介于 -1 和 1 之间,如果数字超出 [-1, 1] 的范围,则结果为 NaN。
如果参数为 1,则此方法返回正 Infinity,如果参数为 -1,则返回负 Infinity。
反双曲正切函数的公式为 −
atanh(x) = (1/2) * ln((1 + x) / (1 - x))
这里,“x” 我们要找到其反双曲正切的输入值。
语法
以下是 JavaScript Math.atanh() 方法的语法 -
Math.atanh(x)
参数
此方法只接受一个参数。下面描述相同 -
- x: 一个数值(介于 -1 和 1 之间,包括 -1 和 1),用于计算双曲反正切值。
返回值
此方法返回所提供数字的反双曲正切值。
示例 1
在下面的示例中,我们将演示 JavaScript 中 Math.atanh() 方法的用法 -
<html>
<body>
<script>
const result1 = Math.atanh(0.5);
const result2 = Math.atanh(-0.5);
document.write(result1, "<br>", result2);
</script>
</body>
</html>
输出
如果我们执行上述程序,此方法返回 0.5 和 -0.5 的反双曲正切值。
示例 2
在这里,我们检索了 “1” 和 “1” 的反双曲正切 -
<html>
<body>
<script>
const result1 = Math.atanh(1);
const result2 = Math.atanh(-1);
document.write(result1, "<br>", result2);
</script>
</body>
</html>
输出
该方法执行后返回 Infinity 和 -Infinity。
示例 3
如果提供的数字小于 -1 或大于 1,则此方法返回 “NaN” 作为结果 −
<html>
<body>
<script>
const result1 = Math.atanh(2);
const result2 = Math.atanh(-2);
document.write(result1, "<br>", result2);
</script>
</body>
</html>
输出
正如我们在输出中看到的,它返回 NaN 作为结果。

