JavaScript | Math.log1p()
The Math.log1p() is an inbuilt function in JavaScript which gives the value of natural logarithm of 1 + p number. Natural logarithm is of base e, where e is an irrational and transcendental number approximately equal to 2.718.
Syntax:
Math.log1p(1 + p)
Parameters: In parameter 1+p, p is any number whose natural logarithm of base e is being calculated.
Returns: It returns the value of base e of logarithm of any number added with 1.
Examples:
Input : Math.log1p(5) Output : 1.791759469228055
Explanation:
Here value of natural logarithm of number 5 is 1.791759469228055 as shown output.
Input : Math.log1p(10) Output : 2.3978952727983707
Let’s see some JavaScript code on this function:
- Example 1:
<script>// Different numbers are being taken// as the parameter of the function.document.write(Math.log1p(1000) +"<br>");document.write(Math.log1p(12) +"<br>");document.write(Math.log1p(26) +"<br>");document.write(Math.log1p(5));</script>chevron_rightfilter_noneOutput:
6.90875477931522 2.5649493574615367 3.295836866004329 1.791759469228055
- Example 2:
<script>// Taken parameter from 1 to 19 incremented by 3.for(i = 1; i < 20; i += 3){document.write(Math.log1p(i) +"<br>");}</script>chevron_rightfilter_noneOutput:
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:
<script>// Parameters for this function should always be a// number otherwise it return NaN i.e, not a number// when its parameter taken as string.document.write(Math.log1p("gfg"));</script>chevron_rightfilter_noneOutput:
NaN
- Example 2: This function gives error when its parameter is taken as complex number because it accepts only integer value as the parameter.
<script>// Parameters can never be a complex number because// it accept only integer value as the parameter.document.write(Math.log1p(1 + 2i));</script>chevron_rightfilter_noneOutput:
Error: Invalid or unexpected token
- Example 3: This function returns NaN i.e, not a number if the parameter is less than -1 because number should be any positive number i.e, greater then 0.
<script>// 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 then 0.document.write(Math.log1p(-2));</script>chevron_rightfilter_noneOutput:
NaN
- Example 1:
<script>// taking parameter as number 14 and calculated// in the form of function.functionvalue_of_base_e_logarithms_of_any_number() {returnMath.log10(14);} document.write(value_of_base_e_logarithms_of_any_number());</script>chevron_rightfilter_noneOutput:
1.146128035678238
Application:Whenever we need to find the value of natural logarithm of 1 + p number that time we take the help of this function. Its value needed many times in mathematics problem.
Let’s see JavaScript code for this application:
Supported Browsers: The browsers supported by JavaScript Math.log1p( ) function are listed below:
- Google Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Recommended Posts:
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Understanding Code Structure in JavaScript
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Conditional Operator in JavaScript
- JavaScript Course | Data Types in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Functions in JavaScript
- JavaScript Course | JavaScript Prompt Example
- JavaScript Course | Variables in JavaScript
- JavaScript Course | Objects in JavaScript
- JavaScript Course | Operators in JavaScript
- JavaScript vs Python : Can Python Overtop JavaScript by 2020?
- How to include a JavaScript file in another JavaScript file ?
- this in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



