JS Built-ins
← Math141 / 215
Mathlog1pbeginner

Math.log1p() β€” log1p(0) = 0

πŸ“– 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: Verify that Math.log1p(0) returns 0.

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