math.complex
complex numbers like 2 + 3i, for math with square roots of negatives
Complex numbers, which have a real part and an imaginary part, a multiple of i, the square root of -1.
writing
Section titled “writing”Write the imaginary part as a number with i right after it and no space: 3i, 0.5i, and 1i for i itself. abs, exp, ln and sqrt accept complex numbers too.
Tags: algebra
rethe real partimthe imaginary part, as a plain numberconjthe complex conjugate: the same number with its imaginary part negatedargthe angle from the positive real axis to the number, as an angle unitcsqrtsquare root that gives a complex number for negatives, so csqrt(-4) is 2icrootsall n complex nth roots of a number, as a listpolarmake a complex number from its polar form, or with one argument show z in that form
syntax
Section titled “syntax”1# an imaginary number: i touching a number23i3# real part + imaginary part42 + 3i5# + - * / ** work as usual6(1 + 2i) * (3 - 1i)5 + 5i7# abs, exp, ln and sqrt accept complex numbers8sqrt(-4 + 0i)2iFunctions
Section titled “Functions”re(z: num|complex) -> float
the real part
1re(2 + 3i)2See also im
im(z: num|complex) -> float
the imaginary part, as a plain number
1im(2 + 3i)3See also re
conj(z: num|complex) -> complex
the complex conjugate: the same number with its imaginary part negated
1conj(2 + 3i)2 - 3iarg(z: num|complex) -> quantity
the angle from the positive real axis to the number, as an angle unit
1arg(1i) to deg90 degcsqrt(z: num|complex) -> complex
square root that gives a complex number for negatives, so csqrt(-4) is 2i
sqrt(-4) is an error instead, so that a calculation going negative by mistake doesn’t quietly turn complex. Of the two square roots, gives the one with a positive real part
1csqrt(-4)2iSee also sqrt
croots
Section titled “croots”croots(z: num|complex, n: int) -> list
all n complex nth roots of a number, as a list
They sit evenly around a circle, so the first is the principal root and the rest are it turned by 360°/n
1croots(1, 3)[1 + 0i, -0.5 + 0.866025i, -0.5 - 0.866025i]2croots(-4, 2)[2i, -2i]See also csqrt
polar(r: num, theta: num|quantity) -> complex / polar(z: num|complex) -> str
make a complex number from its polar form, or with one argument show z in that form
A complex number seen as a point on a plane, real part across and imaginary part up, has a polar form: its distance from 0 and its angle. With one argument, the same as z to polar
1polar(2, 90 deg)2i2polar(1i)"polar(1, 90 deg)"More examples
Section titled “More examples”complex
Section titled “complex”1# Euler's identity: e to the i pi is -12e ** (1i * pi)-1 + 0i3# roots of x² + 2x + 54[-1 + csqrt(-4) / 2, -1 - csqrt(-4) / 2][-1 + 1i, -1 - 1i]5# as a distance and an angle61 + 1i to polar"polar(1.41421, 45 deg)"Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.