Open In App

JavaScript Math log1p() Method

Last Updated : 20 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Javascript Math.log1p() is an inbuilt function in JavaScript that gives the value of the natural logarithm of 1 + p number. The natural logarithm is of base e, where e is an irrational and transcendental number approximately equal to 2.718. 

Syntax:

Math.log1p(1 + p)

Below example illustrate the JavaScript Math.log1p() Method in JavaScript:

Example 1: In this example, we will find the logarithms of some basic numbers.

javascript




// Different numbers are being taken
// as the parameter of the function.
console.log(Math.log1p(1000));
console.log(Math.log1p(12));
console.log(Math.log1p(26));
console.log(Math.log1p(5));


Output:

6.90875477931522
2.5649493574615367
3.295836866004329
1.791759469228055

Example 2: In this example, we will use a for loop to get the logarithms of numbers from 1 to 19 with an increment of 3.

javascript




// Taken parameter from 1 to 19 incremented by 3.
for (i = 1; i < 20; i += 3) {
    console.log(Math.log1p(i));
}


Output:

0.6931471805599453
1.6094379124341003
2.0794415416798357
2.3978952727983707
2.639057329615259
2.833213344056216
2.995732273553991

Errors and exceptions: Parameters for this function should always be a number otherwise it returns NaN i.e, not a number when its parameter is taken as a string.

Example 1: Here NaN comes in output as we have passed a string in the Math.log1p() function.

javascript




// Parameters for this function should always be a
// number otherwise it return NaN i.e, not a number
// when its parameter taken as string.
console.log(Math.log1p("gfg"));


Output:

 NaN

Example 2: This function gives an error when its parameter is taken as a complex number because it accepts only integer value as the parameter. 

javascript




// Parameters can never be a complex number because
// it accept only integer value as the parameter.
console.log(Math.log1p(1 + 2i));


Output:

Error: Invalid or unexpected token

Example 3: This function returns NaN i.e, not a number if the parameter is less than -1 because the number should be any positive number i.e, greater than 0. 

javascript




// This function return NaN i.e, not a number
// if the parameter is less
// than -1 because number should be
// any positive number i.e, greater than 0.
console.log(Math.log1p(-2));


Output:

 NaN

Supported Browsers: The browsers supported by JavaScript Math.log1p( ) function are listed below:

  • Google Chrome 38 and above
  • Edge 12 and above
  • Firefox 25 and above
  • Opera 25 and above
  • Safari 8 and above

We have a complete list of Javascript Math Object methods, to check those please go through Javascript Math Object Complete reference article.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads