Python Glossary Русский

all Python 3cmathfunction

cmath.rect()

polar coordinates

The inverse of polar: builds a complex number from the modulus r and the angle phi in radians, that is r·(cos φ + i·sin φ).

Syntax

cmath.rect(r, phi)

You will rarely get round numbers from it: rect(2, pi/2) gives (1.2246467991473532e-16+2j), not 2j, because cos(π/2) is not zero in floating point. For output, round each part separately (complex has no round), for checks use cmath.isclose. The angle is in radians; convert degrees with math.radians first.

Examples

import cmath
z = cmath.rect(2, cmath.pi / 2)
print(z)  # → (1.2246467991473532e-16+2j)
print(complex(round(z.real, 10), round(z.imag, 10)))  # → 2j
import cmath
import math
z = cmath.rect(1, math.radians(60))
print(round(z.real, 3), round(z.imag, 3))  # → 0.5 0.866

See also

Open in the full glossarydocs.python.orgReport an inaccuracy