试一试
console.log(Math.exp(0));
// Expected output: 1
console.log(Math.exp(1));
// Expected output: 2.718281828459 (approximately)
console.log(Math.exp(-1));
// Expected output: 0.36787944117144233
console.log(Math.exp(2));
// Expected output: 7.38905609893065
语法
js
Math.exp(x)
参数
x-
一个数字。
返回值
一个非负数,表示 ex,其中 e 是 自然对数的底数。
描述
因为 exp() 是 Math 的静态方法,所以你总是使用 Math.exp() 的形式,而不是使用你创建的 Math 对象的这个方法(Math 不是一个构造函数)。
请注意,e 的一个非常接近 0 的数字的次幂将非常接近 1,并会遭受精度损失。在这种情况下,你可能需要改用 Math.expm1,并获得一个精度高得多的结果的分数部分。
示例
使用 Math.exp()
js
Math.exp(-Infinity); // 0
Math.exp(-1); // 0.36787944117144233
Math.exp(0); // 1
Math.exp(1); // 2.718281828459045
Math.exp(Infinity); // Infinity
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-math.exp |
浏览器兼容性
加载中…