JS Built-ins
← Math144 / 215
Mathlog1pintermediate

Math.log1p() β€” Domain x > -1

πŸ“– Usage Example
// Natural log of (1 + x), precise for small x
Math.log1p(0)     // β†’ 0
Math.log1p(0.001) // β†’ 0.0009995...

Use Math.log1p(x) instead of Math.log(1+x) for small x to avoid floating-point cancellation errors.

Math.log1p()

Returns the natural logarithm of 1 + x, i.e. ln(1 + x).

Challenge: Observe that Math.log1p(-2) returns NaN since the domain requires x > -1.

πŸ’‘ Hints (1)
  • β€’ log1p(x) = ln(1+x) is undefined for x < -1 since ln requires positive argument