JavaScript Math.log1p() Method

Shubham Vora Jan 30, 2023
  1. Syntax of JavaScript Math.log1p():
  2. Example Code: Use the Math.log1p() Method With Positive Values
  3. Example Code: Use the Math.log1p() Method With Values Between -1 to 0
  4. Example Code: Use the Math.log1p() Method With Infinity Values
  5. Example Code: Use the Math.log1p() Method With -1, 0, and 1 Values
JavaScript Math.log1p() Method

The Math.log1p(number) method is useful to find the natural logarithm of the 1+number value. The natural logarithm means the logarithm of value with base e, where e is the Euler’s number.

The Math.log1p(number) method represents the ln(1+number), where number>=-1.

Syntax of JavaScript Math.log1p():

let value = Math.log1p(number)

Parameters

number This is the value at which the natural logarithm of 1+number is calculated.

Return

The Math.log1p(number) method returns the natural logarithm of 1+number. If the number is less than 1, it returns Nan; if the number is -1, it returns -Infinity.

Example Code: Use the Math.log1p() Method With Positive Values

In this example, we have used different positive values with the Math.log1p(number) method. You can see that method returns the natural logarithm of 1+number in the output.

let value1 = Math.log1p(4.90);
let value2 = Math.log1p(2);
let value3 = Math.log1p(0.975);
console.log(value1);
console.log(value2);
console.log(value3);

Output:

1.7749523509116738
1.0986122886681096
0.6805683983530851

Example Code: Use the Math.log1p() Method With Values Between -1 to 0

When we take the values between -1 to 0, it always returns the negative output value as it calculates the natural logarithm of values 1+(-1) to 1+0. When we try to use the values less than -1 as a number value, the method always returns NaN as an output.

let value1 = Math.log1p(-0.90);
let value2 = Math.log1p(-0.766);
let value3 = Math.log1p(-1.234);
console.log(value1);
console.log(value2);
console.log(value3);

Output:

-2.302585092994046
-1.4524341636244358
NaN

Example Code: Use the Math.log1p() Method With Infinity Values

The Math.log1p(number) method returns the Infinity values for the (positive) Infinity parameter value and NaN for the -Infinity parameter value.

Also, if we try to find the natural logarithm of Infinity after adding some value, it returns the Infinity that users can see in the output.

let num1 = Math.log1p(Infinity);
let num2 = Math.log1p(-Infinity);
console.log(num1);
console.log(num2);
console.log(Math.log1p(2+Infinity));

Output:

Infinity
NaN
Infinity

Example Code: Use the Math.log1p() Method With -1, 0, and 1 Values

In the example below, we used the values of -1, 0, and 1 as parameters number. For the -1 value, the method returns the -Infinity, as it calculates the natural logarithm of (-1+1=0).

For the 0 and 1 values Math.log1p() method calculates the natural logarithm of 1 and 2 respectively and returns the output value accordingly.

console.log(Math.log1p(-1));
console.log(Math.log1p(0));
console.log(Math.log1p(1));

Output:

-Infinity
0
0.6931471805599453

The Math.log2(number) method is compatible with all modern browsers.

Author: Shubham Vora
Shubham Vora avatar Shubham Vora avatar

Shubham is a software developer interested in learning and writing about various technologies. He loves to help people by sharing vast knowledge about modern technologies via different platforms such as the DelftStack.com website.

LinkedIn GitHub

Related Article - JavaScript Math