The Math.log1p() method returns the natural logarithm of 1 plus the given number. It is equivalent to ln(1+p) in mathematics.
Example
log1p() Syntax
The syntax of the log1p() method is:
Math.log1p(x)
Here, log1p() is a static method. Hence, we need to access the method using the class name, Math.
log1p() Parameters
The log1p() method takes in:
- x - a number
log1p() Return Values
The log1p() method returns:
- natural logarithm (base e) of (1+ given number).
NaNfor negative numbers and non-numeric arguments.
Example 1: JavaScript Math.log1p()
Output
0.6931471805599453 2.1972245773362196 1.791759469228055
In the above example,
Math.log1p(1)- computes the base e log value of 1 + 1Math.log1p(8)- computes the base e log value of 1 + 8Math.log1p(5)- computes the base e log value of 1 + 5
Example 2: log1p() With 0
In the above example, we have used the log1p() method to compute the base e log value of 1 + 0.
The output 0 represents that the base e log value of 1 + 0 is 0.
Example 3: log1p() With Negative Values
In the above example, we have used the log() method to compute the base e log value of a negative number.
The output NaN stands for Not a Number. We get this result because the base e log value of negative numbers is undefined.
Recommended Readings: