Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

math.complex

complex numbers, 2 + 3i; a number touching i is imaginary

Example results generated on 2026-10-08. Ones using now, today or randomness will differ when you run them: press Run on any example to run it in your browser, after editing it if you like.

literals

# ni
3i
# a + bi
2 + 3i

operators

# + - * / **
(1 + 2i) * (3 - 1i)
# → 5 + 5i

also takes complex

# abs exp ln sqrt
sqrt(-4 + 0i)
# → 2i

Functions

functiondescription
re(z: num|complex)real part
im(z: num|complex)imaginary part
conj(z: num|complex)complex conjugate
arg(z: num|complex)angle from the positive real axis, as an angle
csqrt(z: num|complex)square root that goes complex for negatives (principal root)
polar(r: num, theta: num|quantity) / polar(z: num|complex)a complex number from a length and an angle; with one argument, z in polar form (to polar)

re

re(z: num|complex): real part

re(2 + 3i)
# → 2

See also: im

im

im(z: num|complex): imaginary part

im(2 + 3i)
# → 3

See also: re

conj

conj(z: num|complex): complex conjugate

conj(2 + 3i)
# → 2 - 3i

See also: re, im

arg

arg(z: num|complex): angle from the positive real axis, as an angle

arg(1i) to deg
# → 90 deg

See also: polar, abs

csqrt

csqrt(z: num|complex): square root that goes complex for negatives (principal root)

csqrt(-4)
# → 2i

See also: sqrt

polar

polar(r: num, theta: num|quantity) / polar(z: num|complex): a complex number from a length and an angle; with one argument, z in polar form (to polar)

polar(2, 90 deg)
# → 2i
polar(1i)
# → "polar(1, 90 deg)"

See also: arg, abs

More examples

complex

# Euler's identity
e ** (1i * pi)
# → -1 + 0i
# roots of x² + 2x + 5
[-1 + csqrt(-4) / 2, -1 - csqrt(-4) / 2]
# → [-1 + 1i, -1 - 1i]
# polar form
1 + 1i to polar
# → "polar(1.41421, 45 deg)"