Skip to content
Varlig Guide
Varlig home

Complex numbers

A complex number has a real part and an imaginary part, written with i, the square root of −1. This page covers writing complex numbers, doing arithmetic with them, taking them apart into modulus and argument, finding square roots of negative numbers, and the maths functions that accept complex values.

Reach for it when a quadratic equation has no real roots, when you’re working with AC circuits or signals, or when you’re checking complex analysis coursework.

Writing complex numbers

calc
2 + 3i gives 2 + 3i
complex(3, -4) gives 3 - 4i
i^2 gives -1

Put i straight after a number for the imaginary part, as in 3i or 1.5i, or use i on its own. complex(real, imaginary) builds the same value from two parts, which is handy when the parts are names: complex(resistance, reactance). Both parts must be plain numbers without units.

i means the imaginary unit only while nothing else in the note is called i. Once you define a name or a custom unit called i, it takes over:

calc
i = 5
2 + 3i gives 17
complex(2, 3) gives 2 + 3i

Here 3i became 3 × 5, and the line gave 17. Names carry across the calc blocks of a note, so an i = … anywhere earlier in the note has the same effect. Avoid using i as a name in notes that use complex numbers. complex(2, 3) doesn’t use the name, so it still works.

Arithmetic

calc
(2 + 3i) + (1 - 5i) gives 3 - 2i
(1 + 2i) * (3 - i) gives 5 + 5i
(2 + 3i) * (2 - 3i) gives 13
10 / (3 + 4i) gives 1.2 - 1.6i
(1 + i)^8 gives 16

Addition, subtraction, multiplication, division and whole-number powers are exact. When the imaginary part of a result is zero, as with (2 + 3i) * (2 - 3i), the answer shows as an ordinary number. Use brackets around each complex number when you multiply or divide, so that (2 + 3i) * (2 - 3i) isn’t read as 2 + (3i * 2) - 3i.

Parts of a complex number

calc
z = 3 + 4i
real(z) gives 3
imag(z) gives 4
conj(z) gives 3 - 4i
abs(z) gives 5
arg(1 + i) gives 0.7853981634
arg(1 + i) rad in degrees gives 45°
Function Gives
real(z) The real part
imag(z) The imaginary part, as a plain number
conj(z) The complex conjugate: same real part, imaginary part with its sign flipped
abs(z) The modulus: the distance from zero
arg(z) The argument: the angle from the positive real axis, in radians

arg returns a plain number of radians between −π and π. To see it in degrees, mark it as radians and convert, as in the last line. To go the other way, from modulus and angle back to a complex number, use exp: 2 * exp(i * pi / 3) is 1 + 1.7320508076i.

Square roots of negative numbers

A square root of an ordinary negative number is a real-number calculation, so sqrt(-16) shows an error that tells you what to write instead: Error: imaginary number. For a complex answer, write sqrt(-16 + 0i). To ask for the complex root, write the number as a complex one: add 0i, or use complex(…, 0):

calc
sqrt(-16 + 0i) gives 4i
sqrt(complex(-16, 0)) gives 4i
sqrt(3 + 4i) gives 2 + i
sqrt(2i) gives 1 + i

sqrt returns the principal root, the one with a positive real part (or, for negative numbers, a positive imaginary part). The other square root is its negative.

A value you write as complex stays complex, even when its imaginary part is zero. You can store it in a name or carry on calculating with it first: after x = -16 + 0i, sqrt(x) is 4i, even though x displays as -16.

This makes the quadratic formula work for any quadratic:

calc
# Roots of x^2 + 2x + 5 = 0
a = 1
b = 2
c = 5
disc = b^2 - 4*a*c gives -16
(-b + sqrt(disc + 0i)) / (2*a) gives -1 + 2i
(-b - sqrt(disc + 0i)) / (2*a) gives -1 - 2i

Cube roots follow the same principal-root rule, which can surprise you with negative numbers:

calc
cbrt(-8) gives -2
cbrt(-8 + 0i) gives 1 + 1.7320508076i

Both are cube roots of −8. The real version gives the real root; the complex version gives the principal complex root.

Functions of a complex number

calc
exp(i * pi) gives -1
ln(-1 + 0i) gives 3.1415926536i
i^i gives 0.2078795764
cos(i) gives 1.5430806348
sinh(i) gives 0.8414709848i
2^i gives 0.7692389014 + 0.6389612763i

These functions accept complex values:

  • sin, cos, tan and their inverses asin, acos, atan
  • sinh, cosh, tanh and their inverses asinh, acosh, atanh
  • exp, ln, log (or log10), log2, sqrt and cbrt
  • powers with fractional or complex exponents, such as i^0.5 or 2^i

They work in binary floating point, so answers are close approximations. Round-off leftovers too small to matter show as 0, so exp(i * pi) reads as −1, the answer you’d expect from the textbook. The stored value keeps every digit, so when you compare such results, check that the difference is tiny, as in abs(exp(i * pi) + 1) < 1e-9, rather than using ==.

Each function gives a single answer, from its principal branch. A complex number has infinitely many logarithms and several roots, and you get the standard one. As with sqrt, real inputs stay real: ln(-1) and log(-100) show the imaginary-number error with the complex form to try, while ln(-1 + 0i) gives πi. The logarithm of zero is an error.

Functions that only make sense for real numbers, such as floor and round, don’t accept complex values.

Comparing complex numbers

calc
2 + 3i == complex(2, 3) gives true
2 + 3i != 2 - 3i gives true
complex(3, 0) >= 3 gives true
abs(3 + 4i) > abs(1 + 2i) gives true

== and != work on any complex values. <, <=, > and >= only work when both sides have an imaginary part of zero, because complex numbers have no natural order: i < 2 is an error. To compare sizes, compare the moduli with abs, as in the last line. Complex numbers can’t be sorted.

The full rules are in the advanced mathematics reference.

Putting it together

Here is a check on an AC circuit problem: a 30 Ω resistor and an inductor with 40 Ω of reactance in series, on a 10 V supply:

calc
# Series circuit: 30 Ω resistor, 40 Ω reactance, 10 V supply
resistance = 30
reactance = 40
z = complex(resistance, reactance) gives 30 + 40i
abs(z) gives 50
current = 10 / z gives 0.12 - 0.16i
abs(current) gives 0.2
arg(z) rad in degrees to 1 dp gives 53.1°

The impedance is 30 + 40i, with a magnitude of 50 Ω, so the current is 0.2 A and lags the voltage by about 53.1°. Engineers often write j for the imaginary unit; in Varlig it is always i. The ohms, volts and amps live in the comment, because the parts of a complex number can’t carry units.