在 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” 作为结果。