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" 作為結果。

廣告