JavaScript 中的 Math.exp() 方法用于返回将欧拉数(大约等于 2.718)提高到指定数的幂的结果。它计算所提供数字的指数值,其中 Euler 数提高到 x 的幂表示为 e^x。
计算 Euler 数的公式提高到 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 作为结果。