exponential
Math.expm1
Returns e^x - 1, computed accurately even for very small x. Introduced in ES2015 to avoid catastrophic cancellation near zero.
Math.expm1(x) Parameters & edge cases
| Case | Behavior |
|---|---|
| x | The exponent |
| small x | Far more accurate than Math.exp(x) - 1 when |x| is near zero |
| large x | For large x, expm1 and exp effectively return the same value |
Examples
console.log(Math.expm1(0)); // 0 e⁰ - 1 = 0 exactly.
console.log(Math.expm1(1)); // 1.718281828459045 e - 1.
console.log(Math.expm1(1e-10)); // 1.00000000005e-10 Precise near zero; Math.exp(1e-10) - 1 loses digits.
console.log(Math.exp(1e-10) - 1); // 1.000000082740371e-10 The naive form is visibly wrong for tiny x.
Gotcha
Only reach for expm1 when your x is close to 0. For x with magnitude > 1e-4, plain `Math.exp(x) - 1` is fine.
Related
Math.exp
Returns e^x, where e is Euler's number (~2.71828). The inverse of Math.log (the natural logarithm).
Math.log1p
Returns the natural log of (1 + x), computed accurately even for very small x. Introduced in ES2015 as the companion to expm1.
Math.log
Returns the natural logarithm (base e, or ln) of a number. Inverse of Math.exp.
Math.log2
Returns the base-2 logarithm of a number. Introduced in ES2015 and useful for information theory, tree depths, and bit counts.
Math.log10
Returns the base-10 logarithm of a number. Introduced in ES2015 for accurate common-log calculations (decibels, pH, orders of magnitude).