exponential
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.log1p(x) Parameters & edge cases
| Case | Behavior |
|---|---|
| x | A value where 1 + x must be > 0 |
| small x | Preserves precision that Math.log(1 + x) loses when x is tiny |
| x = -1 | Math.log1p(-1) returns -Infinity |
Examples
console.log(Math.log1p(0)); // 0 ln(1) = 0.
console.log(Math.log1p(Math.E - 1)); // 1 ln(e) = 1, using log1p to hit it exactly.
console.log(Math.log1p(1)); // 0.6931471805599453 ln(2).
console.log(Math.log1p(-1)); // -Infinity ln(0) = -Infinity.
Gotcha
Use log1p for interest-rate and probability code where x is a small delta. For everyday values it behaves identically to Math.log(1 + x).
Related
Math.log
Returns the natural logarithm (base e, or ln) of a number. Inverse of Math.exp.
Math.expm1
Returns e^x - 1, computed accurately even for very small x. Introduced in ES2015 to avoid catastrophic cancellation near zero.
Math.exp
Returns e^x, where e is Euler's number (~2.71828). The inverse of Math.log (the natural logarithm).
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).