JavaScript Math.atanh() 方法
JavaScript Math.atanh() 方法接受一个数值并计算该数字的双曲反正切(反双曲正切)。提供的数值应介于 -1 和 1 之间,如果数值超出 [-1, 1] 的范围,则结果为 NaN。
如果参数为 1,此方法返回正无穷大;如果参数为 -1,此方法返回负无穷大。
反双曲正切函数的公式为 −
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>
输出
此方法执行后返回无穷大和负无穷大。
示例 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 作为结果。

