# 13.11 <complex>

The <complex> header declares the complex class template and specializations for the float, double, and long double types. It also declares mathematical functions that work with complex values.

 abs function template Computes absolute value

 `template T abs(const complex& z)`

The abs function returns the absolute value (or magnitude) of z.

polar function template, abs function in <cmath>

 arg function template Computes argument (angle)

 `template T arg(const complex& z)`

The arg function returns the argument (angle in polar coordinates) of z.

polar function template

 complex class template Complex number template

 ```template class complex { public: typedef T value_type; complex(const T& re = T( ), const T& im = T( )); complex(const complex& z); template complex(const complex& z); T real( ) const; T imag( ) const; complex& operator= (const T& x); complex& operator+=(const T& x); complex& operator-=(const T& x); complex& operator*=(const T& x); complex& operator/=(const T& x); complex& operator=(const complex& z); template complex& operator= (const complex& z); template complex& operator+=(const complex& z); template complex& operator-=(const complex& z); template complex& operator*=(const complex& z); template complex& operator/=(const complex& z); };```

The complex class template represents a complex number. The <complex> header specializes the template for the float, double, and long double types. You can instantiate complex<> for any type that behaves in the manner of the fundamental numeric types.

The type definition is a straightforward representation of a complex number. Basic assignment operators are defined as member functions, and arithmetic operators are defined as global functions.

template<typename X> complex(const complex<X>& z)

Constructs a complex<T> object by copying the members from z. Effectively, this converts a complex object instantiated for one type to a complex object of another type.

T real( ) const

Returns the real part of *this.

T imag( ) const

Returns the imaginary part of *this.

complex& operator=(const T& x)

Assigns x to the real part of *this and 0 to the imaginary part. Returns *this.

complex& operator+=(const T& x)

Adds x to the real part of *this, leaving the imaginary part alone. Returns *this.

complex& operator-=(const T& x)

Subtracts x from the real part of *this, leaving the imaginary part alone. Returns *this.

complex& operator*=(const T& x)

Multiplies the real and imaginary parts of *this by x. Returns *this.

complex& operator/=(const T& x)

Divides the real and imaginary parts of *this by x. Returns *this.

complex& operator=(const complex& z)

Assigns the real and imaginary parts of z to *this. Returns *this.

template<typename X> complex& operator=(const complex<X>& z)

Assigns the real and imaginary parts of z to *this. Returns *this. Note that z and *this can have different template parameter types.

template<typename X> complex& operator+=(const complex<X>& z)

Adds z to *this. Returns *this. Note that z and *this can have different template parameter types.

template<typename X> complex& operator-=(const complex<X>& z)

Subtracts z from *this. Returns *this. Note that z and *this can have different template parameter types.

template<typename X> complex& operator*=(const complex<X>& z)

Multiplies *this by z. Returns *this. Note that z and *this can have different template parameter types.

template<typename X> complex& operator/=(const complex<X>& z)

Divides *this by z. Returns *this. Note that z and *this can have different template parameter types.

 complex template specialization Double-precision complex number

 ```template<> class complex { public: typedef double value_type; complex(double re = 0.0, double im = 0.0); complex(const complex&); explicit complex(const complex&); double real( ) const; double imag( ) const; complex& operator= (double); complex& operator+=(double); complex& operator-=(double); complex& operator*=(double); complex& operator/=(double); complex& operator=(const complex&); template complex& operator= (const complex&); template complex& operator+=(const complex&); template complex& operator-=(const complex&); template complex& operator*=(const complex&); template complex& operator/=(const complex&); };```

The complex<double> class is a straightforward specialization of the complex class template. It changes the operators to pass double parameters by value instead of by reference, and it adds a new constructor:

explicit complex(const complex<long double>& z)

Constructs a complex number by copying from z. Note that you might lose precision or overflow, so the constructor is explicit.

 complex template specialization Single-precision complex number

 ```template<> class complex { public: typedef float value_type; complex(float re = 0.0f, float im = 0.0f); explicit complex(const complex&); explicit complex(const complex&); float real( ) const; float imag( ) const; complex& operator= (float); complex& operator+=(float); complex& operator-=(float); complex& operator*=(float); complex& operator/=(float); complex& operator=(const complex&); template complex& operator= (const complex&); template complex& operator+=(const complex&); template complex& operator-=(const complex&); template complex& operator*=(const complex&); template complex& operator/=(const complex&); };```

The complex<float> class is a straightforward specialization of the complex class template. It changes the operators to pass float parameters by value instead of by reference, and it adds two new constructors:

explicit complex(const complex<double>& z)
explicit complex(const complex<long double>& z)

Constructs a complex number by copying from z. Note that you might lose precision or overflow, so the constructors are explicit.

 complex template specialization Extended-precision complex number

 ```template<> class complex { public: typedef long double value_type; complex(long double re = 0.0L, long double im = 0.0L); complex(const complex&); complex(const complex&); long double real( ) const; long double imag( ) const; complex& operator=(const complex&); complex& operator= (long double); complex& operator+=(long double); complex& operator-=(long double); complex& operator*=(long double); complex& operator/=(long double); template complex& operator= (const complex&); template complex& operator+=(const complex&); template complex& operator-=(const complex&); template complex& operator*=(const complex&); template complex& operator/=(const complex&); };```

The complex<long double> class is a straightforward specialization of the complex class template. It changes the operators to pass long double parameters by value instead of by reference.

 conj function template Computes conjugate

 `template complex conj(const complex& z)`

The conj function returns the complex conjugate of z.

 cos function template Computes cosine

 `template complex cos(const complex& z)`

The cos function returns the complex cosine of z.

cos function in <cmath>

 cosh function template Computes hyperbolic cosine

 `template complex cosh(const complex& z)`

The cosh function returns the complex hyperbolic cosine of z.

cosh function in <cmath>

 exp function template Computes exponential

 `template complex exp(const complex& z)`

The exp function returns the exponential of z, that is, ez.

exp function in <cmath>

 imag function template Returns imaginary part

 `template T imag(const complex& z)`

The imag function returns the imaginary part of z, that is, z.imag( ).

abs function in <cmath>

 log function template Computes natural logarithm

 `template complex log(const complex& z)`

The log function returns the complex natural (base e) logarithm of z. The branch cuts are along the negative real axis, which means the imaginary part of the result is in the range [-i, i].

log function in <cmath>

 log10 function template Computes common logarithm

 `template complex log10(const complex& z)`

The log10 function returns the complex common (base 10) logarithm of z. The branch cuts are along the negative real axis, which means the imaginary part of the result is in the range [-i, i].

log10 function in <cmath>

 norm function template Computes normalized value

 `template T norm(const complex& z)`

The norm function returns the square of the absolute value of z.

abs function template

 operator+ function template Persforms unary positive or addition

 ```template complex operator+(const complex& z); template complex operator+(const complex& x, const complex& y); template complex operator+(const complex& x, const T& y); template complex operator+(const T& x, const complex& y);```

The unary positive operator returns z.

The binary addition operator returns the sum of its operands. If either operand is of type T, the argument is interpreted as the real part, with an imaginary part of T( ) or 0.

 operator- function template Performs negation or subtraction

 ```template complex operator-(const complex&); template complex operator-(const complex&, const complex&); template complex operator-(const complex&, const T&); template complex operator-(const T&, const complex&);```

The unary negation operator returns -z.

The binary subtraction operator returns the difference of its operands. If either operand is of type T, the argument is interpreted as the real part, with an imaginary part of T( ) or 0.

 operator* function template Performs multiplication

 ```template complex operator*(const complex&, const complex&); template complex operator*(const complex&, const T&); template complex operator*(const T&, const complex&);```

The binary * operator performs complex multiplication. If either operand is of type T, the argument is interpreted as the real part, with an imaginary part of T( ) or 0.

 operator/ function template Performs division

 ```template complex operator/(const complex&, const complex&); template complex operator/(const complex&, const T&); template complex operator/(const T&, const complex&);```

The binary / operator performs complex division. If either operand is of type T, the argument is interpreted as the real part, with an imaginary part of T( ) or 0. Division by zero results in undefined behavior.

 operator== function template Checks equality

 ```template bool operator==(const complex&, const complex&); template bool operator==(const complex&, const T&); template bool operator==(const T&, const complex&);```

The == operator returns true if the real and imaginary parts of both values are equal. If either operand is of type T, the argument is interpreted as the real part, with an imaginary part of T( ) or 0.

 operator!= function template Checks inequality

 ```template bool operator!=(const complex&, const complex&); template bool operator!=(const complex&, const T&); template bool operator!=(const T&, const complex&);```

The != operator returns true if the real or imaginary parts are not equal. If either operand is of type T, the parameter is interpreted as the real part, with an imaginary part of T( ) or 0.

 operator<< function template Writes a complex number

 ```template basic_ostream& operator<<(basic_ostream&, const complex& z);```

The << operator prints z to the output stream in the form (x, y), in which x is the real part, and y is the imaginary part. Example 13-6 shows how z is formatted. If you want more control over the formatting, you must print the value yourself.

#### Example

##### Example 13-6. Formatting a complex number
```template<class T, class charT, class traits>

std::basic_ostream<charT, traits>&

operator<<(std::basic_ostream<charT, traits>& o,

const std::complex<T>& x)

{

std::basic_ostringstream<charT, traits> s;

s.flags(o.flags(  ));

s.imbue(o.getloc(  ));

s.precision(o.precision(  ));

s << "(" << x.real(  ) << "," << x.imag(  ) << ")";

return o << s.str(  );

}```
 operator>> function template Reads a complex number

 ```template basic_istream& operator>>(basic_istream&, complex& z);```

The >> operator reads a complex number from an input stream into z. The input format can be any of the following:

x

The value x is the real part, and T( ) or 0 is the imaginary part.

(x)

The value x is the real part, and T( ) or 0 is the imaginary part.

(x, y)

The value x is the real part, and y is the imaginary part.

 polar function template Converts to polar coordinates

 ```template complex polar(const T& r, const T& theta)```

The polar function returns a complex object that represents the value given in polar coordinates, in which r is the magnitude and theta is the angle (in radians). The resulting value has the following real and imaginary parts:

```real = r * cos(theta)

imag = r * sin(theta)```

abs function template, arg function template

 pow function template Computes power

 ```template complex pow(const complex& x, int y); template complex pow(const complex& x, const T& y); template complex pow(const complex& x, const complex& y); template complex pow(const T& x, const complex& y);```

The pow function returns the complex power xy. If x and y are both 0, the result is implementation-defined; otherwise, the result is exp(y * log(x)). The branch cuts are along the negative real axis.

exp function template, log function template, pow function in <cmath>

 real function template Returns real part

 `template T real(const complex& z)`

The real function returns the real part of z, that is, z.real( ).

 sin function template Computes sine

 `template complex sin(const complex& z)`

The sin function returns the complex sine of z.

sin function in <cmath>

 sinh function template Computes hyperbolic sine

 `template complex sinh(const complex& z)`

The sinh function returns the complex hyperbolic sine of z.

sinh function in <cmath>

 sqrt function template Computes square root

 `template complex sqrt(const complex& z)`

The sqrt function returns the complex square root of z.The branch cuts are along the negative real axis. The result always has a nonnegative real part.

sqrt function in <cmath>

 tan function template Computes tangent

 `template complex tan(const complex& z)`

The tan function returns the complex tangent of z.

tan function in <cmath>

 tanh function template Computes hyperbolic tangent

 `template complex tanh(const complex& z)`

The tanh function returns the complex hyperbolic tangent of z.

tanh function in <cmath>

 Chapter 1. Language Basics
 Chapter 2. Declarations
 Chapter 3. Expressions
 Chapter 4. Statements
 Chapter 5. Functions
 Chapter 6. Classes
 Chapter 7. Templates
 Chapter 8. Standard Library
 Chapter 9. Input and Output
 Chapter 10. Containers, Iterators, and Algorithms
 Chapter 11. Preprocessor Reference
 Chapter 12. Language Reference
 Appendix A. Compiler Extensions
 Appendix B. Projects
 Glossary