JavaScript Math.exp() 方法



JavaScript 中的 Math.exp() 方法用於返回尤拉數(大約等於 2.718)的指定次冪的結果。它計算提供的數字的指數值,其中尤拉數的 x 次冪表示為 e^x。

計算尤拉數的 x 次冪的公式 -

Math.exp(x) = e^x

其中“e”是尤拉數,“x”是要計算指數值的數字。

語法

以下是 JavaScript Math.exp() 方法的語法 -

Math.exp(x)

引數

此方法僅接受一個引數。下面描述了它 -

  • x: 一個數值。

返回值

此方法返回 e 的 x 次冪的值。

示例 1

在以下示例中,我們使用 JavaScript Math.exp() 方法計算 2 的 e 次冪。

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

輸出

如果我們執行上述程式,它將返回“7.3890”作為結果。

示例 2

在此示例中,我們正在計算 -1 的 e 次冪 -

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

輸出

如果我們執行上述程式,它將返回 0.3678 作為結果。

示例 3

在此示例中,我們正在計算 0 的 e 次冪 -

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

輸出

結果將為 1,因為任何數的 0 次冪都為 1。

示例 4

在這裡,我們將“Infinity”和“-Infinity”作為引數傳遞給此方法 -

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

輸出

如果我們執行上述程式,它將分別返回 Infinity 和 0 作為結果。

廣告