all Python 3Numbers & mathterm
cmath
cmath
The cmath module provides mathematical functions for complex numbers.
Syntax
import cmath
cmath.sqrt(-1) # → 1j
cmath functions such as sqrt, exp and log always hand back a complex, even when the answer is really a real number: cmath.sqrt(4) is (2+0j), which will not slot into code expecting a float. Reach for it exactly where math raises ValueError — roots and logs of negative numbers; and complex values cannot be ordered, so < or > raises TypeError and you have to compare abs() instead.
Examples
import cmath
cmath.sqrt(-1) # → 1j
cmath.sqrt(-4) # → 2j
cmath.phase(1+1j) # → 0.785... (π/4)
cmath.polar(1+1j) # → (1.414..., 0.785...)
cmath.exp(1j * cmath.pi) # → (-1+0j) формула Эйлера
See also
Open in the full glossarydocs.python.orgReport an inaccuracy