Python cmath.log() Function



The Python cmath.log() function provides a way to perform logarithm operation on complex numbers. This method returns the logarithm of a complex number with respect to base.

If there is a single argument passed then it calculates the natural logarithm with base e. In case of multiple arguments, this method returns the logarithm of the initial argument with the base of the final argument.

If the specified argument is zero or negative then this returns ValueError, and if it is not a number then this returns TypeError.

Syntax

Following is the syntax of the Python cmath.log() method −

cmath.log(a, base = None)

Parameters

This method accepts following parameters −

  • a: This specifies the value to calculate the logarithm, a is the component of the base.

  • base: This parameter specifies the base. It is not mandatory to mention, this is optional.

Return Value

This function returns the logarithm of the complex number.

Example 1

Following is the basic example of the Python cmath.log() function. Here we are trying to calculate the simple logarithm with base 10.

import cmath
x=cmath.log(5, 10)
print(x)

Output

The output of the above code is as follows −

(0.6989700043360187+0j)

Example 2

In this example, we are calculating logarithm of the given complex number using cmath.log().

import cmath
y=2+3j
x=cmath.log(y)
print(x)

Output

The output obtained is as follows −

(1.2824746787307684+0.982793723247329j)
python_modules.htm
Advertisements