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() 方法计算 e 的 2 次方。
<html> <body> <script> const result = Math.exp(2); document.write(result); </script> </body> </html>
输出
如果执行上述程序,它将返回结果"7.3890"。
示例 2
在此示例中,我们计算"e"的 -1 次方 -
<html> <body> <script> const result = Math.exp(-1); document.write(result); </script> </body> </html>
输出
如果我们执行上述程序,它将返回 0.3678 作为结果。
示例 3
在此示例中,我们计算 e 的 0 次方 −
<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 作为结果。

