The Python math.log1p() method is used to calculate the natural logarithm of 1 + x, where x is the argument passed to the method. Mathematically, the method is represented as −
\log1p\:(x)\:=\:\log_{e}({1\:+\:x})\:=\:\ln(1\:+\:x)
Where, e is the base of the natural logarithm (Euler's number). For example, if x = 1, then math.log1p(1) returns ln(1 + 1), which simplifies to ln(2).
Note: To use this function, you need to import math module.
SyntaxFollowing is the basic syntax of the Python math.log1p() method −
math.log1p(x)Parameters
This method accepts either an integer or a floating-point number as a parameter for which you want to calculate the natural logarithm of 1 plus x.
Return ValueThe method returns the natural logarithm of 1 plus x. The return value is a floating-point number.
Example 1In the following example, we calculate the natural logarithm of 1 + 1, which means passing a positive integer as an argument to the log1p() method −
import math result = math.log1p(1) print("The result obtained is:", result)Output
The output obtained is as follows −
The result obtained is: 0.6931471805599453Example 2
Here, we pass a negative integer as an argument to the log1p() method. We calculate the natural logarithm of 1 0.5 −
import math result = math.log1p(-0.5) print("The result obtained is:", result)Output
Following is the output of the above code −
The result obtained is: -0.6931471805599453Example 3
In this example, we calculate the natural logarithm of 1 + 1000, which is a large number −
import math result = math.log1p(1000) print("The result obtained is:", result)Output
We get the output as shown below −
The result obtained is: 6.90875477931522Example 4
Now, we use a variable "x" to store the argument. We then calculate the natural logarithm of 1 + x −
import math x = 2 result = math.log1p(x) print("The result obtained is:", result)Output
The result produced is as shown below −
The result obtained is: 1.0986122886681096
python_maths.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4