JavaScript Math.expm1() 方法



在 JavaScript 中,Math.expm1() 方法用于计算 e^x - 1 的值,其中 "e" 是欧拉数 (约等于 2.7183),"x" 是传递给函数的参数。

Math.expm1() 方法的数学公式为:

expm1(x)=e^x −1

其中:

  • e 是欧拉数,自然对数的底数 (约等于 2.718)。
  • x 是传递给函数的参数。

语法

以下是 JavaScript Math.expm1() 方法的语法:

Math.expm1(x)

参数

此方法只接受一个参数,如下所述:

  • x: 表示指数的一个数字。

返回值

此方法返回 ex − 1,其中 "e" 是自然对数的底数 (约等于 2.718)。

示例 1

在下面的示例中,我们使用 JavaScript Math.expm1() 方法计算 e 的 5 次方减 1:

<html>
<body>
<script>
   const result = Math.expm1(5);
   document.write(result);
</script>
</body>
</html>

输出

如果我们执行上面的程序,它将返回约 147.413 的结果。

示例 2

当参数为 0 时,结果为 0,因为 e0 - 1 等于 0:

<html>
<body>
<script>
   const result = Math.expm1(0);
   document.write(result);
</script>
</body>
</html>

输出

正如我们看到的输出,它返回 0 作为结果。

示例 3

下面的示例计算 e(-1) - 1:

<html>
<body>
<script>
   const result = Math.expm1(-1);
   document.write(result);
</script>
</body>
</html>

输出

结果将约等于 -0.6321。

示例 4

在这里,我们将 "-Infinity" 和 "Infinity" 作为参数传递给此方法:

<html>
<body>
<script>
   const result1 = Math.expm1(-Infinity);
   const result2 = Math.expm1(Infinity);
   document.write(result1, "<br>", result2);
</script>
</body>
</html>

输出

它分别返回 "-1" 和 "Infinity" 作为结果。

广告
© . All rights reserved.