The Math.log1p()
method returns the natural logarithm of 1 plus the given number. It is equivalent to ln(1+p) in mathematics.
Example
// calculate ln(1+p) of 1 which is equivalent to ln(2)
var value = Math.log1p(1); // ln(2)
console.log(value);
// Output: 0.6931471805599453
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).
NaN
for negative numbers and non-numeric arguments.
Example 1: JavaScript Math.log1p()
// find the base e log value of 1 + 1
var value1 = Math.log1p(1);
console.log(value1);
// find the base e log value of 1 + 8
var value2=Math.log1p(8);
console.log(value2)
// find the base 2 log value of 1 + 5
var value3 = Math.log1p(5);
console.log(value3);
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
// find the base e log value of 1 + 0
var value = Math.log1p(0);
console.log(value);
// Output: 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
// find the base e log value of -1
var value = Math.log(-1);
console.log(value);
// Output: NaN
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.
Also Read: