<complex>


abs · acos · acosh · arg · asin · asinh · atan · atanh · complex · complex<double> · complex<float> · complex<long double> · conj · cos · cosh · exp · imag · log · log10 · norm · operator!= · operator* · operator+ · operator- · operator/ · operator<< · operator== · operator>> · polar · pow · proj · real · sin · sinh · sqrt · tan · tanh · __STD_COMPLEX


Include the standard header <complex> to define template class complex and a host of supporting template functions. Unless otherwise specified, functions that can return multiple values return an imaginary part in the half-open interval (-pi, pi].

Beginning with C++11, some functions and constructors declared in this header use constexpr to signal that they are treated as compile-time constants.

Many of the functions declared in this header have additional overloads, which behave much like the generic functions defined in the C99 header <tgmath.h>. The following functions have such additional overloads:

abs      conj     polar    tanh
acos     cos      pow
acosh    cosh     proj
arg      exp      real
asin     imag     sin
asinh    log      sinh
atan     log10    sqrt
atanh    norm     tan

[These additional overloads are added with C++11]


namespace std {
#define __STD_COMPLEX

        // TEMPLATE CLASSES
template<class Ty>
    class complex;
template<>
    class complex<float>;
template<>
    class complex<double>;
template<>
    class complex<long double>;

        // TEMPLATE FUNCTIONS
template<class Ty>
    complex<Ty> operator+(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator+(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator+(const Ty& left,
        const complex<Ty>& right);

template<class Ty>
    complex<Ty> operator-(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator-(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator-(const Ty& left,
        const complex<Ty>& right);

template<class Ty>
    complex<Ty> operator*(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator*(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator*(const Ty& left,
        const complex<Ty>& right);

template<class Ty>
    complex<Ty> operator/(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator/(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator/(const Ty& left,
        const complex<Ty>& right);

template<class Ty>
    complex<Ty> operator+(const complex<Ty>& left);
template<class Ty>
    complex<Ty> operator-(const complex<Ty>& left);

template<class Ty>
    bool operator==(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    bool operator==(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    bool operator==(const Ty& left,
        const complex<Ty>& right);

template<class Ty>
    bool operator!=(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    bool operator!=(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    bool operator!=(const Ty& left,
        const complex<Ty>& right);

template<class Ty, class Elem, class Tr>
    basic_istream<Elem, Tr>&
        operator>>(basic_istream<Elem, Tr>& istr,
            complex<Ty>& right);
template<class Ty, class Elem, class Tr>
    basic_ostream<Elem, Tr>&
        operator<<(basic_ostream<Elem, Tr>& ostr,
            const complex<Ty>& right);

template<class Ty>
    Ty real(const complex<Ty>& left);
template<class Ty>
    Ty imag(const complex<Ty>& left);
template<class Ty>
    Ty abs(const complex<Ty>& left);
template<class Ty>
    Ty arg(const complex<Ty>& left);
template<class Ty>
    Ty norm(const complex<Ty>& left);

template<class Ty>
    complex<Ty> conj(const complex<Ty>& left);
double conj(const double&left); [added with C++11]
float conj(const float&left); [added with C++11]
long double conj(const long double&left); [added with C++11]

template<class Ty>
    complex<Ty> proj(const complex<Ty>& left);
double proj(const double&left); [added with C++11]
float proj(const float&left); [added with C++11]
long double proj(const long double&left); [added with C++11]

template<class Ty>
    complex<Ty> acos(const complex<Ty>& left); [added with C++11]
template<class Ty>
    complex<Ty> acosh(const complex<Ty>& left); [added with C++11]
template<class Ty>
    complex<Ty> asin(const complex<Ty>& left); [added with C++11]
template<class Ty>
    complex<Ty> asinh(const complex<Ty>& left); [added with C++11]
template<class Ty>
    complex<Ty> atan(const complex<Ty>& left); [added with C++11]
template<class Ty>
    complex<Ty> atanh(const complex<Ty>& left); [added with C++11]

template<class Ty>
    complex<Ty> polar(const Ty& rho, const Ty& theta = 0);
template<class Ty>
    complex<Ty> cos(const complex<Ty>& left);
template<class Ty>
    complex<Ty> cosh(const complex<Ty>& left);
template<class Ty>
    complex<Ty> exp(const complex<Ty>& left);
template<class Ty>
    complex<Ty> log(const complex<Ty>& left);
template<class Ty>
    complex<Ty> log10(const complex<Ty>& left);
template<class Ty>
    complex<Ty> pow(const complex<Ty>& left, const Ty& right);
template<class Ty>
    complex<Ty> pow(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> pow(const Ty& left, const complex<Ty>& right);
template<class Ty>
    complex<Ty> sin(const complex<Ty>& left);
template<class Ty>
    complex<Ty> sinh(const complex<Ty>& left);
template<class Ty>
    complex<Ty> sqrt(const complex<Ty>& left);
}  // namespace std

abs

template<class Ty>
    Ty abs(const complex<Ty>& left);

The function returns the magnitude of left.

acos

template<class Ty>
    complex<Ty> acos(const complex<Ty>& left); [added with C++11]

The function returns the arccosine of left.

acosh

template<class Ty>
    complex<Ty> acosh(const complex<Ty>& left); [added with C++11]

The function returns the hyperbolic arccosine of left.

arg

template<class Ty>
    Ty arg(const complex<Ty>& left);

The function returns the phase angle of left.

asin

template<class Ty>
    complex<Ty> asin(const complex<Ty>& left); [added with C++11]

The function returns the arcsine of left.

asinh

template<class Ty>
    complex<Ty> asinh(const complex<Ty>& left); [added with C++11]

The function returns the hyperbolic arcsine of left.

atan

template<class Ty>
    complex<Ty> atan(const complex<Ty>& left); [added with C++11]

The function returns the arctangent of left.

atanh

template<class Ty>
    complex<Ty> atanh(const complex<Ty>& left); [added with C++11]

The function returns the hyperbolic arctangent of left.

complex

template<class Ty>
    class complex {
public:
    typedef Ty value_type;

    Ty real() const;
    void real(Ty val);
    Ty imag() const;
    void imag(Ty val);

    complex(const Ty& realval = 0, const Ty& imagval = 0);
    template<class Other>
        complex(const complex<Other>& right);
    template<class Other>
        complex& operator=(const complex<Other>& right);
    template<class Other>
        complex& operator+=(const complex<Other>& right);
    template<class Other>
        complex& operator-=(const complex<Other>& right);
    template<class Other>
        complex& operator*=(const complex<Other>& right);
    template<class Other>
        complex& operator/=(const complex<Other>& right);
    complex& operator=(const Ty& right);
    complex& operator+=(const Ty& right);
    complex& operator-=(const Ty& right);
    complex& operator*=(const Ty& right);
    complex& operator/=(const Ty& right);
    };

The template class describes an object that stores two objects of type Ty, one that represents the real part of a complex number and one that represents the imaginary part. An object of class Ty:

In particular, no subtle differences may exist between copy construction and default construction followed by assignment. And none of the operations on objects of class Ty may throw exceptions.

Explicit specializations of template class complex exist for the three floating-point types. In this implementation, a value of any other type Ty is type cast to double for actual calculations, with the double result assigned back to the stored object of type Ty.

complex::complex

complex(const Ty& realval = 0, const Ty& imagval = 0);
template<class Other>
    complex(const complex<Other>& right);

The first constructor initializes the stored real part to realval and the stored imaginary part to imagval. The second constructor initializes the stored real part to right.real() and the stored imaginary part to right.imag().

complex::imag

Ty imag() const;
void imag(Ty val);

The first member function returns the stored imaginary part. The second member function stores val in the imaginary part.

complex::operator*=

template<class Other>
    complex& operator*=(const complex<Other>& right);
complex& operator*=(const Ty& right);

The first member function replaces the stored real and imaginary parts with those corresponding to the complex product of *this and right. It then returns *this.

The second member function multiplies both the stored real part and the stored imaginary part with right. It then returns *this.

complex::operator+=

template<class Other>
    complex& operator+=(const complex<Other>& right);
complex& operator+=(const Ty& right);

The first member function replaces the stored real and imaginary parts with those corresponding to the complex sum of *this and right. It then returns *this.

The second member function adds right to the stored real part. It then returns *this.

complex::operator-=

template<class Other>
    complex& operator-=(const complex<Other>& right);
complex& operator-=(const Ty& right);

The first member function replaces the stored real and imaginary parts with those corresponding to the complex difference of *this and right. It then returns *this.

The second member function subtracts right from the stored real part. It then returns *this.

complex::operator/=

template<class Other>
    complex& operator/=(const complex<Other>& right);
complex& operator/=(const Ty& right);

The first member function replaces the stored real and imaginary parts with those corresponding to the complex quotient of *this and right. It then returns *this.

The second member function multiplies both the stored real part and the stored imaginary part with right. It then returns *this.

complex::operator=

template<class Other>
    complex& operator=(const complex<Other>& right);
complex& operator=(const Ty& right);

The first member function replaces the stored real part with right.real() and the stored imaginary part with right.imag(). It then returns *this.

The second member function replaces the stored real part with right and the stored imaginary part with zero. It then returns *this.

complex::real

Ty real() const;
void real(Ty val);

The first member function returns the stored real part. The second member function stores val in the real part.

complex::value_type

typedef Ty value_type;

The type is a synonym for the template parameter Ty.

complex<double>

template<>
    class complex<double> {
public:
    constexpr complex(double realval = 0, double imagval = 0);
    constexpr complex(const complex<float>& right);
    constexpr explicit complex(const complex<long double>& right);

    constexpr double real() const;
    constexpr double imag() const;
// rest same as template class complex
    };

The explicitly specialized template class describes an object that stores two objects of type double, one that represents the real part of a complex number and one that represents the imaginary part. The explicit specialization differs only in the constructors it defines. The first constructor initializes the stored real part to realval and the stored imaginary part to imagval. The remaining two constructors initialize the stored real part to right.real() and the stored imaginary part to right.imag().

complex<float>

template<>
    class complex<float> {
public:
    constexpr complex(float realval = 0, float imagval = 0);
    constexpr explicit complex(const complex<double>& right);
    constexpr explicit complex(const complex<long double>& right);

    constexpr float real() const;
    constexpr float imag() const;
// rest same as template class complex
    };

The explicitly specialized template class describes an object that stores two objects of type float, one that represents the real part of a complex number and one that represents the imaginary part. The explicit specialization differs only in the constructors it defines. The first constructor initializes the stored real part to realval and the stored imaginary part to imagval. The remaining two constructors initialize the stored real part to right.real() and the stored imaginary part to right.imag().

complex<long double>

template<>
    class complex<long double> {
public:
    constexpr complex(long double realval = 0, long double imagval = 0);
    constexpr complex(const complex<float>& right);
    constexpr complex(const complex<double>& right);

    constexpr long double real() const;
    constexpr long double imag() const;
// rest same as template class complex
    };

The explicitly specialized template class describes an object that stores two objects of type long double, one that represents the real part of a complex number and one that represents the imaginary part. The explicit specialization differs only in the constructors it defines. The first constructor initializes the stored real part to realval and the stored imaginary part to imagval. The remaining two constructors initialize the stored real part to right.real() and the stored imaginary part to right.imag().

conj

template<class Ty>
    complex<Ty> conj(const complex<Ty>& left);
double conj(const double&left); [added with C++11]
float_complex conj(const float_complex& left);
float conj(const float&left); [added with C++11]
long double conj(const long double&left); [added with C++11]

The function returns the conjugate of left.

cos

template<class Ty>
    complex<Ty> cos(const complex<Ty>& left);

The function returns the cosine of left.

cosh

template<class Ty>
    complex<Ty> cosh(const complex<Ty>& left);

The function returns the hyperbolic cosine of left.

exp

template<class Ty>
    complex<Ty> exp(const complex<Ty>& left);

The function returns the exponential of left.

imag

template<class Ty>
    Ty imag(const complex<Ty>& left);

The function returns the imaginary part of left.

log

template<class Ty>
    complex<Ty> log(const complex<Ty>& left);

The function returns the logarithm of left. The branch cuts are along the negative real axis.

log10

template<class Ty>
    complex<Ty> log10(const complex<Ty>& left);

The function returns the base 10 logarithm of left. The branch cuts are along the negative real axis.

norm

template<class Ty>
    Ty norm(const complex<Ty>& left);

The function returns the squared magnitude of left.

operator!=

template<class Ty>
    bool operator!=(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    bool operator!=(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    bool operator!=(const Ty& left,
        const complex<Ty>& right);

The operators each return true only if real(left) != real(right) || imag(left) != imag(right).

operator*

template<class Ty>
    complex<Ty> operator*(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator*(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator*(const Ty& left,
        const complex<Ty>& right);

The operators each convert both operands to the return type, then return the complex product of the converted left and right.

operator+

template<class Ty>
    complex<Ty> operator+(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator+(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator+(const Ty& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator+(const complex<Ty>& left);

The binary operators each convert both operands to the return type, then return the complex sum of the converted left and right.

The unary operator returns left.

operator-

template<class Ty>
    complex<Ty> operator-(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator-(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator-(const Ty& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator-(const complex<Ty>& left);

The binary operators each convert both operands to the return type, then return the complex difference of the converted left and right.

The unary operator returns a value whose real part is -real(left) and whose imaginary part is -imag(left).

operator/

template<class Ty>
    complex<Ty> operator/(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> operator/(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> operator/(const Ty& left,
        const complex<Ty>& right);

The operators each convert both operands to the return type, then return the complex quotient of the converted left and right.

operator<<

template<class Ty, class Elem, class Tr>
    basic_ostream<Elem, Tr>&
        operator<<(basic_ostream<Elem, Tr>& ostr,
            const complex<Ty>& right);

The template function inserts the complex value right in the output stream os, effectively by executing:

basic_ostringstream<Elem, Tr> osstr;
osstr.flags(ostr.flags());
osstr.imbue(ostr.imbue());
osstr.precision(ostr.precision());
osstr << '(' << real(right) << ','
    << imag(right) << ')';
ostr << osstr.str().c_str();

Thus, if ostr.width() is greater than zero, any padding occurs either before or after the parenthesized pair of values, which itself contains no padding. The function returns ostr.

operator==

template<class Ty>
    bool operator==(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    bool operator==(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    bool operator==(const Ty& left,
        const complex<Ty>& right);

The operators each return true only if real(left) == real(right) && imag(left) == imag(right).

operator>>

template<class Ty, class Elem, class Tr>
    basic_istream<Elem, Tr>&
        operator>>(basic_istream<Elem, Tr>& istr,
            complex<Ty>& right);

The template function attempts to extract a complex value from the input stream istr, effectively by executing:

istr >> ch && ch == '('
    && istr >> re >> ch && ch == ','
    && istr >> im >> ch && ch == ')'

Here, ch is an object of type Elem, and re and im are objects of type Ty.

If the result of this expression is true, the function stores re in the real part and im in the imaginary part of right. In any event, the function returns istr.

polar

template<class Ty>
    complex<Ty> polar(const Ty& rho,
        const Ty& theta = 0);

The function returns the complex value whose magnitude is rho and whose phase angle is theta.

pow

template<class Ty>
    complex<Ty> pow(const complex<Ty>& left,
        const Ty& right);
template<class Ty>
    complex<Ty> pow(const complex<Ty>& left,
        const complex<Ty>& right);
template<class Ty>
    complex<Ty> pow(const Ty& left,
        const complex<Ty>& right);

The functions each effectively convert both operands to the return type, then return the converted left to the power right. The branch cut for left is along the negative real axis.

proj

template<class Ty>
    complex<Ty> proj(const complex<Ty>& left);
double proj(const double&left); [added with C++11]
float_complex proj(const float_complex& left);
float proj(const float&left); [added with C++11]
long double proj(const long double&left); [added with C++11]

The function returns the projection of left on the Riemann sphere:

real

template<class Ty>
    Ty real(const complex<Ty>& left);

The function returns the real part of left.

sin

template<class Ty>
    complex<Ty> sin(const complex<Ty>& left);

The function returns the sine of left.

sinh

template<class Ty>
    complex<Ty> sinh(const complex<Ty>& left);

The function returns the hyperbolic sine of left.

sqrt

template<class Ty>
    complex<Ty> sqrt(const complex<Ty>& left);

The function returns the square root of left, with phase angle in the half-open interval (-pi/2, pi/2]. The branch cuts are along the negative real axis.

__STD_COMPLEX

#define __STD_COMPLEX

The macro is defined, with an unspecified expansion, to indicate compliance with the specifications of this header.

tan

template<class Ty>
    complex<Ty> tan(const complex<Ty>& left);

The function returns the tangent of left.

tanh

template<class Ty>
    complex<Ty> tanh(const complex<Ty>& left);

The function returns the hyperbolic tangent of left.


See also the Table of Contents and the Index.

Copyright © 1992-2013 by P.J. Plauger. All rights reserved.