all Python 3cmathfunction
cmath.log()
powers and logarithms
The natural logarithm of a complex number; the second argument sets the base. The logarithm of a negative number exists here.
Syntax
cmath.log(z[, base])
cmath.log(-1) returns 3.141592653589793j, the principal value, while math.log(-1) raises ValueError. But log(0) is still an error here: ValueError, not -inf. The result is always complex, even cmath.log(100, 10) gives (2+0j); and the logarithm is multivalued, so the function returns only the branch with the imaginary part in (-π, π].
Examples
import cmath
print(cmath.log(-1)) # → 3.141592653589793j
print(cmath.log(100, 10)) # → (2+0j)
import cmath
try:
cmath.log(0)
except ValueError as e:
print(type(e).__name__) # → ValueError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy