The Math.expm1()
function returns e^{x} - 1, where x is the argument, and e is the base of natural logarithms.
The Math.expm1()
function returns e^{x} - 1, where x is the argument, and e is the base of natural logarithms.
Math.expm1(x)
x
A number.
A number representing e^{x} - 1, where e is the base of natural logarithms and x is the argument.
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.
When you calculate $\backslash mathrm\{e\}^x$ where x is a number very close to 0, you should get an answer very close to 1 + x, because $\backslash lim\_\{x\; \backslash to\; 0\}\; \backslash frac\{\backslash 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.
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).
Math.expm1(-1); // -0.6321205588285577 Math.expm1(0); // 0 Math.expm1(1); // 1.718281828459045
Specification |
---|
ECMAScript Language Specification # sec-math.expm1 |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
expm1 |
38 |
12 |
25 |
No |
25 |
8 |
38 |
38 |
25 |
25 |
8 |
3.0 |
1.0 |
0.12.0 |
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/expm1