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

← All JS Math methods · Array methods