JS Built-ins
← Math145 / 215
Mathlog1pbeginner

Math.log1p() β€” Return Type

πŸ“– 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: Confirm that Math.log1p() always returns a JavaScript number.

πŸ’‘ Hints (1)
  • β€’ Math.log1p always returns a JavaScript number (including NaN and -Infinity)