cmath
The Python cmath module provides mathematical functions for complex numbers. Unlike the math module, which raises an exception for operations like the square root of a negative number, cmath always returns a complex result.
Here’s a quick example:
>>> import cmath
>>> cmath.sqrt(-1)
1j
>>> cmath.sqrt(-4)
2j
Key Features
- Computes square roots, logarithms, and exponentials for complex numbers
- Converts between rectangular and polar coordinate representations
- Provides trigonometric and hyperbolic functions for complex arguments
- Includes classification functions to test for infinity, NaN (not a number), and closeness
- Accepts integers, floats, and complex numbers as arguments
- Always returns a complex number, even when the imaginary part is zero
- Exposes mathematical constants including
pi,e,tau,inf, andnan
Frequently Used Classes and Functions
| Object | Type | Description |
|---|---|---|
cmath.sqrt() |
Function | Returns the square root of a complex number |
cmath.exp() |
Function | Returns e raised to the power of a complex number |
cmath.log() |
Function | Returns the natural (or given-base) logarithm of a complex number |
cmath.phase() |
Function | Returns the phase (argument) of a complex number as a float |
cmath.polar() |
Function | Converts a complex number to polar coordinates as an (r, phi) tuple |
cmath.rect() |
Function | Converts polar coordinates to a complex number |
cmath.isclose() |
Function | Tests whether two complex values are close within a given tolerance |
cmath.isfinite() |
Function | Returns True if both real and imaginary parts are finite |
Examples
Converts between rectangular and polar coordinate forms:
>>> import cmath
>>> z = 3 + 4j
>>> r, phi = cmath.polar(z)
>>> r
5.0
>>> cmath.rect(r, phi)
(3.0000000000000004+3.9999999999999996j)
Verifies Euler’s formula using the isclose() function:
>>> cmath.isclose(cmath.exp(cmath.pi * 1j) + 1, 0, abs_tol=1e-9)
True
Checks whether a complex value is finite or NaN:
>>> cmath.isfinite(2 + 3j)
True
>>> cmath.isnan(complex(float("nan"), 0))
True
Common Use Cases
The most common tasks for cmath include:
- Computing square roots and logarithms of negative or complex numbers
- Converting between rectangular and polar representations of complex numbers
- Evaluating trigonometric functions with complex arguments
- Testing the validity of complex values in numerical computing
Real-World Example
Converting a set of phasors from rectangular to polar form to inspect their magnitudes and phases:
>>> import cmath
>>> signals = [1 + 1j, 3 + 4j, -1 + 0j]
>>> for z in signals:
... r, phi = cmath.polar(z)
... print(f"z={z} magnitude={r:.4f} phase={phi:.4f}")
...
z=(1+1j) magnitude=1.4142 phase=0.7854
z=(3+4j) magnitude=5.0000 phase=0.9273
z=(-1+0j) magnitude=1.0000 phase=3.1416
The polar form gives the amplitude and phase angle of each signal directly, which is a common requirement in signal processing and electrical engineering.
Related Resources
Tutorial
Simplify Complex Numbers With Python
In this tutorial, you'll learn about the unique treatment of complex numbers in Python. Complex numbers are a convenient tool for solving scientific and engineering problems. You'll experience the elegance of using complex numbers in Python with several hands-on examples.
For additional information on related topics, take a look at the following resources:
- The Python math Module: Everything You Need to Know (Tutorial)
- Numbers in Python (Tutorial)
- How to Find an Absolute Value in Python (Tutorial)
- Fourier Transforms With scipy.fft: Python Signal Processing (Tutorial)
- Exploring the Python math Module (Course)
- How to Find an Absolute Value in Python (Quiz)
By Leodanis Pozo Ramos • Updated March 2, 2026