The Complex Arithmetic Library |
3 |
![]() |
3.2 + 4i 1 + 3i 1 + 2.3i |
In the degenerate case, 0 + 3i is an entirely imaginary number generally written as 3i, and 5 + 0i is an entirely real number generally written as 5. You can represent complex numbers using the complex data type.
The Complex Library
To use the complex library, include the header file complex.h in your program, and link with the -lcomplex or -library=complex option.
Type complex
The complex arithmetic library defines one class: class complex. An object of class complex can hold a single complex number. The complex number is constructed of two parts: the real part and the imaginary part. The numerical values of each part are held in fields of type double. Here is the relevant part of the definition of complex:
class complex { double re, im; }; |
The value of an object of class complex is a pair of double values. The first value represents the real part; the second value represents the imaginary part.
Constructors of Class complex
There are two constructors for complex. Their definitions are:
complex::complex() { re=0.0; im=0.0; } complex::complex(double r, double i = 0.0) { re=r; im=i; } |
complex aComp; |
complex aComp(4.533); |
creates a complex variable with the value:
4.533 + 0i |
complex aComp(8.999, 2.333); |
creates a complex variable with the value:
8.999 + 2.333i |
You can also create a complex number using the polar function, which is provided in the complex arithmetic library (see "Mathematical Functions" on page 44). The polar function creates a complex value given a pair of polar coordinates, magnitude and angle.
There is no destructor for type complex.
Arithmetic Operators
The complex arithmetic library defines all the basic arithmetic operators. Specifically, the following operators work in the usual way and with the usual precedence: + - / * =
The operator - has its usual binary and unary meanings. += -= *= /=
However, these last four operators do not produce values that you can use in expressions. For example, the following does not work:
complex a, b; ... if ((a+=2)==0) {...}; // illegal b = a *= b; // illegal |
You can also use the following equality operators in their regular meaning:
== !=
When you mix real and complex numbers in an arithmetic expression, C++ uses the complex operator function and converts the real values to complex values.
Mathematical Functions
The complex arithmetic library provides a number of mathematical functions. Some are peculiar to complex numbers; the rest are complex-number versions of functions in the standard C mathematical library.
Error Handling
The complex library has these definitions for error handling:
extern int errno; int complex_error(c_exception&); |
The external variable errno is the global error state from the C library. errno can take on the values listed in the standard header errno.h (see the man page perror(3)). No function sets errno to zero, but many functions set it to other values. To determine whether a particular operation fails, set errno to zero before the operation, then test it afterward.
Default error handling is described in the man pages cplxtrig(3C++) and cplxexp(3C++). It is also summarized in Table 3-3
Input and Output
The complex arithmetic library provides default extractors and inserters for complex numbers, as shown in the following example:
ostream& operator<<(ostream&, const complex&); //inserter istream& operator>>(istream&, complex&); //extractor |
See sections "Basic Structure of Iostream Interaction" on page 51 and "Output Using Iostreams" on page 53 for basic information on extractors and inserters.
complex x; cin >> x; |
and the input (3.45, 5), the value of x is equivalent to 3.45 + 5.0i. The reverse is true for inserters. Given complex x(3.45, 5), cout<<x prints (3.45, 5).
Mixed-Mode Arithmetic
Type complex is designed to fit in with the built-in arithmetic types in mixed-mode expressions. Arithmetic types are silently converted to type complex, and there are complex versions of the arithmetic operators and most mathematical functions. For example:
int i, j; double x, y; complex a, b; a = sin((b+i)/y) + x/j; |
The expression b+i is mixed-mode. Integer i is coverted to type complex via the constructor complex::complex(double,double=0), the integer first being converted to type double. The result is to be divided by y, a double, so y is also converted to complex and the complex divide operation is used. The quotient is thus type complex, so the complex sine routine is called, yielding another complex result, and so on.
complex a, b; a == b // OK a != b // OK a < b // error: operator < cannot be applied to type complex a >= b // error: operator >= cannot be applied to type complex |
complex a; double f(double); f(abs(a)); // OK f(a); // error: no match for f(complex) |
Efficiency
The design of the complex class addresses efficiency concerns.
double x; complex x = sqrt(x); |
In this example, the standard math function sqrt(double) is called, and the result is converted to type complex, rather than converting to type complex first and then calling sqrt(complex). This result falls right out of the overload resolution rules, and is precisely the result you want.
Complex Man Pages
The man pages listed in Table 3-4 comprise the remainder of the documentation of the complex arithmetic library.