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>

输出

执行后,此方法返回 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 作为结果。

广告

© . All rights reserved.