Back

Math.log1p

Math.log1p

The Math.log1p() function returns the natural logarithm (base e) of 1 + a number, that is

x>-1,Math.log1p(x)=ln(1+x)\forall x > -1, \mathtt{\operatorname{Math.log1p}(x)} = \ln(1 + x)

Syntax

Math.log1p(x)

Parameters

x
A number.

Description

If the value of x is less than -1, the return value is always NaN.

Because log1p() is a static method of Math, you always use it as Math.log1p(), rather than as a method of a Math object you created (Math is not a constructor).

Examples

Using Math.log1p()

Math.log1p(1);  // 0.6931471805599453
Math.log1p(0);  // 0
Math.log1p(-1); // -Infinity
Math.log1p(-2); // NaN

Polyfill

This can be emulated with the following function:

Math.log1p = Math.log1p || function(x) {
  return Math.log(1 + x);
};

  Created by Mozilla Contributors, license: CC-BY-SA 2.5