Python Tutorial Python Advanced Python References Python Libraries

Python cmath - log() Function



The Python cmath.log() function returns the natural logarithm (base e) or logarithm to the specified base of a given complex number z. It is a function on complex plane, and has a branch cut, from 0 along the negative real axis to -∞, continuous from above.

Syntax

cmath.log(z, base)

Parameters

z Required. Specify the number.
base Optional. Specify the base. Default is e.

Return Value

Returns the natural logarithm or logarithm to the specified base of z.

Example:

In the example below, log() function is used to calculate the natural logarithm of a given complex number.

import cmath

z1 = 2 + 2j
z2 = 2
z3 = 2j

print("cmath.log(z1):", cmath.log(z1))
print("cmath.log(z2):", cmath.log(z2))
print("cmath.log(z3):", cmath.log(z3))

The output of the above code will be:

cmath.log(z1): (1.039720770839918+0.7853981633974483j)
cmath.log(z2): (0.6931471805599453+0j)
cmath.log(z3): (0.6931471805599453+1.5707963267948966j)

Example:

In this example, log() function is used to calculate the specified base logarithm of a given complex number.

import cmath

z = 2 + 3j

#base-2 logarithm
print("cmath.log(z, 2):", cmath.log(z, 2))

#base-10 logarithm
print("cmath.log(z, 10):", cmath.log(z, 10))

The output of the above code will be:

cmath.log(z, 2): (1.850219859070546+1.417871630745722j)
cmath.log(z, 10): (0.5569716761534184+0.42682189085546657j)

❮ Python cMath Module