Python math.log1p() Method



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.

Syntax

Following 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 Value

The method returns the natural logarithm of 1 plus x. The return value is a floating-point number.

Example 1

In 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.6931471805599453

Example 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.6931471805599453

Example 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.90875477931522

Example 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
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy