Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Memory and Utilities

C++ std::complex class template

Reference for std::complex: construct complex numbers, do arithmetic, use abs, arg, conj, polar, sqrt and exp, and the i and if literals.

std::complex<T> (header <complex>) represents a complex number a + bi as a pair of real numbers of type T. T should be float, double or long double; other types are not supported in portable code. The class overloads +, -, *, / and the comparison ==, and the header supplies functions such as abs, arg, conj, polar, sqrt, exp and pow.

You can also write complex literals with the suffixes i, if and il from std::complex_literals.

C++
std::complex<double> z{3.0, 4.0};   // 3 + 4i
double r = std::abs(z);             // 5

Try it

C++ Arithmetic and magnitude

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Polar form and square roots

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Syntax

C++
template<class T> class complex;

complex(const T& re = T(), const T& im = T());
T real() const;     T imag() const;
T abs(const complex<T>&);     T arg(const complex<T>&);     T norm(const complex<T>&);
complex<T> conj(const complex<T>&);
complex<T> polar(const T& rho, const T& theta = T());
Name Description
re, im Real and imaginary parts. Both default to zero.
abs(z) Magnitude, the square root of re*re + im*im, computed without needless overflow.
arg(z) Phase angle in radians, in the range from -pi to pi.
norm(z) Squared magnitude. Cheaper than abs.
polar(rho, theta) Builds a number from magnitude and angle.

Returns: the arithmetic operators and functions return std::complex<T> (or T for abs, arg, norm). Mixed expressions such as z * 2.0 are allowed when the scalar has the matching type T.

Errors: none are thrown; domain problems follow floating-point rules, giving infinities or NaNs.

Usage notes

  • Type must match. std::complex<double> z; z * 2 does not compile because 2 is an int, not a double. Write 2.0.
  • Printing uses the form (re,im), and reading expects the same, so a stream round trip works.
  • Compare with == only for exact values. After arithmetic, compare abs(a - b) with a tolerance.
  • Square root of a negative real needs a complex type. std::sqrt(-4.0) is NaN; std::sqrt(std::complex<double>{-4.0}) returns (0,2).
  • Branch cuts: sqrt, log, pow and arg pick the principal value, with the cut along the negative real axis.
  • Array compatibility: std::complex<T> is layout-compatible with T[2], so you can reinterpret an array of complex as an array of its parts, which helps with FFT libraries and C interop.
  • i literals need using namespace std::complex_literals;. The suffixes are i (double), if (float) and il (long double).