Math.expm1()

Math.expm1() 静态方法返回 e 的数字次方减去 1。那是

¥The Math.expm1() static method returns e raised to the power of a number, subtracted by 1. That is

𝙼𝚊𝚝𝚑.𝚎𝚡𝚙𝚖𝟷 ( 𝚡 ) = e x 1 \mathtt{\operatorname{Math.expm1}(x)} = \mathrm{e}^x - 1

Try it

语法

¥Syntax

js
Math.expm1(x)

参数

¥Parameters

x

一个号码。

返回值

¥Return value

代表 ex 的数字 - 1,其中 e 是 自然对数的底

¥A number representing ex - 1, where e is the base of the natural logarithm.

描述

¥Description

对于非常小的 x 值,加 1 会降低或消除精度。JS 中使用的双浮点可为你提供大约 15 位的精度。1 + 1e-15 = 1.000000000000001,但 1 + 1e-16 = 1.000000000000000,因此在该算术中恰好是 1.0,因为超过 15 的数字会四舍五入。

¥For very small values of x, adding 1 can reduce or eliminate precision. The double floats used in JS give you about 15 digits of precision. 1 + 1e-15 = 1.000000000000001, but 1 + 1e-16 = 1.000000000000000 and therefore exactly 1.0 in that arithmetic, because digits past 15 are rounded off.

当你计算 e x \mathrm{e}^x</ annotation> 其中 x 是一个非常接近 0 的数字,你应该得到一个非常接近 1 + x 的答案,因为 lim x<mostretchy="false">→ 0 e x 1</ mrow> x = 1 <注释编码="TeX">\lim_{x \to 0} \frac{\mathrm{e}^x - 1}{x} = 1</注释></语义></数学>。如果计算 Math.exp(1.1111111111e-15) - 1,你应该得到接近 1.1111111111e-15 的答案。相反,由于 Math.exp 结果中的最高有效数字是个位 1,所以最终的结果是 1.1102230246251565e-15,只有 3 位正确。相反,如果你计算 Math.exp1m(1.1111111111e-15),你将得到更准确的答案 1.1111111111000007e-15,其精度为 11 位正确数字。

¥When you calculate e x \mathrm{e}^x where x is a number very close to 0, you should get an answer very close to 1 + x, because lim x 0 e x 1 x = 1 \lim_{x \to 0} \frac{\mathrm{e}^x - 1}{x} = 1 . If you calculate Math.exp(1.1111111111e-15) - 1, you should get an answer close to 1.1111111111e-15. Instead, due to the highest significant figure in the result of Math.exp being the units digit 1, the final value ends up being 1.1102230246251565e-15, with only 3 correct digits. If, instead, you calculate Math.exp1m(1.1111111111e-15), you will get a much more accurate answer 1.1111111111000007e-15, with 11 correct digits of precision.

因为 expm1()Math 的静态方法,所以你始终将其用作 Math.expm1(),而不是用作你创建的 Math 对象的方法(Math 不是构造函数)。

¥Because expm1() is a static method of Math, you always use it as Math.expm1(), rather than as a method of a Math object you created (Math is not a constructor).

示例

¥Examples

使用 Math.expm1()

¥Using Math.expm1()

js
Math.expm1(-Infinity); // -1
Math.expm1(-1); // -0.6321205588285577
Math.expm1(-0); // -0
Math.expm1(0); // 0
Math.expm1(1); // 1.718281828459045
Math.expm1(Infinity); // Infinity

规范

Specification
ECMAScript Language Specification
# sec-math.expm1

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看