JS Built-ins
← Math143 / 215
Mathlog1pintermediate

Math.log1p() β€” log1p(-1) = -Infinity

πŸ“– 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(-1) returns -Infinity.

πŸ’‘ Hints (1)
  • β€’ log1p(-1) = ln(1 + (-1)) = ln(0) = -Infinity