Complex numbers are not just of mathematical interest, they are also vital in engineering and science, so a complex type is an important part of any type library. A complex number is made of two parts--the real and imaginary parts. As the name suggests, an imaginary number is not real, and cannot be treated as real.
In mathematics, complex numbers are usually represented as coordinates in two-dimensional space. If a real number can be thought of as being one of an infinite number of points on the x-axis, an imaginary number can be thought of being one of an infinite number of points on the y-axis. The only intersection between these two is the origin and since zero is zero, is nothing, it can be a zero real number or a zero imaginary number. A complex number has both real and imaginary parts, and hence this can be visualized as a Cartesian point. Indeed, another way of visualizing a complex number is as a polar number where the point is represented as a vector of a specified length at a specified angle to the position on the x-axis (the positive real number axis).
The complex class is based on a floating point type, and there are specializations for float, double, and long double. The class is simple; it has a constructor with two parameters for the real and imaginary parts of the number, and it defines operators (member methods and global functions) for assignment, comparisons, +, -, /, and *, acting on the real and imaginary parts.
There are also functions to perform trigonometric operations on complex numbers: sin, cos, tan, sinh, cosh, and tanh; and basic math operations such as log, exp, log10, pow, and sqrt. You can also call functions to create complex numbers and get information about them. So, the polar function will take two floating-point numbers representing the polar coordinates of the length of the vector and the angle. If you have a complex number object you can get the polar coordinates by calling abs (to get the length) and arg (to get the angle).
complex<double> a(1.0, 1.0);
complex<double> b(-0.5, 0.5);
complex<double> c = a + b;
cout << a << " + " << b << " = " << c << endl;
complex<double> d = polar(1.41421, -3.14152 / 4);
cout << d << endl;
The first point to make is that there is an ostream insertion operator defined for complex numbers so you can insert them into the cout stream object. The output from this code is as follows:
(1,1) + (-0.5,0.5) = (0.5,1.5)
(1.00002,-0.999979)
The second line shows the limitations of using just five decimal places for the square root of 2 and -1/4 pi, this number is, in fact, the complex number (1, -1).