all Python 3cmathfunction
cmath.exp()
powers and logarithms
The exponential e**z for a complex z. Euler's formula is written with it: exp(i·φ) is a point on the unit circle.
Syntax
cmath.exp(z)
Euler's formula exp(iπ) = -1 does not hold exactly in floating point: you get (-1+1.2246467991473532e-16j), because pi is only an approximation of π. Compare with cmath.isclose and round before printing. Unlike math.exp, it accepts and returns complex; when the real part is too large it raises OverflowError instead of returning inf.
Examples
import cmath
z = cmath.exp(1j * cmath.pi)
print(z) # → (-1+1.2246467991473532e-16j)
print(z == -1, cmath.isclose(z, -1)) # → False True
import cmath
try:
cmath.exp(1000)
except OverflowError as e:
print(type(e).__name__) # → OverflowError
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy