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.
Absolute value
template<typename T> T abs(const complex<T>& z)
The abs
function returns the absolute value (or magnitude) of z
.
abs function in <cmath>, polar function template
Argument (angle)
template<typename T> T arg(const complex<T>& z)
The arg
function returns the argument (angle in polar coordinates) of z
.
polar function template
Complex number
template<typename T> class complex { public: typedef T value_type; complex(const T& re = T(), const T& im = T()); complex(const complex& z); template<typename X> complex(const complex<X>& z); T real() const; T imag() const; complex<T>& operator= (const T& x); complex<T>& operator+=(const T& x); complex<T>& operator-=(const T& x); complex<T>& operator*=(const T& x); complex<T>& operator/=(const T& x); complex& operator=(const complex& z); template<typename X> complex<T>& operator= (const complex<X>& z); template<typename X> complex<T>& operator+=(const complex<X>& z); template<typename X> complex<T>& operator-=(const complex<X>& z); template<typename X> complex<T>& operator*=(const complex<X>& z); template<typename X> complex<T>& operator/=(const complex<X>& z); };
The complex
class template represents a complex number. The template parameter can be any type that has value semantics, especially fundamental types. The <complex>
header specializes the template for the float
, double
, and long
double
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)
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
*this
.T imag() const
*this
.complex<T>& operator= (const T& x)
x
to the real part of *this
and zero to the imaginary part. Returns *this
.complex<T>& operator+=(const T& x)
x
to the real part of *this
, leaving the imaginary part alone. Returns *this
.complex<T>& operator-=(const T& x)
x
from the real part of *this
, leaving the imaginary part alone. Returns *this
.complex<T>& operator*=(const T& x)
*this
by x
. Returns *this
.complex<T>& operator/=(const T& x)
*this
by x
. Returns *this
.complex& operator=(const complex& z
)z
to *this
. Returns *this
.template<typename X>
complex<T>& operator= (const complex<X>& z)
z
to *this
. Returns *this
. Note that z
and *this
can have different template parameter types.template<typename X>
complex<T>& operator+=(const complex<X>& z)
z
to *this
. Returns *this
. Note that z
and *this
can have different template parameter types.template<typename X>
complex<T>& operator-=(const complex<X>& z
)z
from *this
.Returns *this
. Note that z
and *this
can have different template parameter types.template<typename X>
complex<T>& operator*=(const complex<X>& z)
*this
by z
. Returns *this
. Note that z
and *this
can have different template parameter types.template<typename X>
complex<T>& operator/=(const complex<X>& z)
*this
by z
. Returns *this
. Note that z
and *this
can have different template parameter types.Complex number
template<> class complex<double> { public: typedef double value_type; complex(double re = 0.0, double im = 0.0); complex(const complex<float>&); explicit complex(const complex<long double>&); double real() const; double imag() const; complex<double>& operator= (double); complex<double>& operator+=(double); complex<double>& operator-=(double); complex<double>& operator*=(double); complex<double>& operator/=(double); complex<double>& operator=(const complex<double>&); template<typename X> complex<double>& operator= (const complex<X>&); template<typename X> complex<double>& operator+=(const complex<X>&); template<typename X> complex<double>& operator-=(const complex<X>&); template<typename X> complex<double>& operator*=(const complex<X>&); template<typename X> complex<double>& operator/=(const complex<X>&); };
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)
z
. Note that you might lose precision or overflow, so the constructor is explicit
.Complex number
template<> class complex<float> { public: typedef float value_type; complex(float re = 0.0f, float im = 0.0f); explicit complex(const complex<double>&); explicit complex(const complex<long double>&); float real() const; float imag() const; complex<float>& operator= (float); complex<float>& operator+=(float); complex<float>& operator-=(float); complex<float>& operator*=(float); complex<float>& operator/=(float); complex<float>& operator=(const complex<float>&); template<typename X> complex<float>& operator= (const complex<X>&); template<typename X> complex<float>& operator+=(const complex<X>&); template<typename X> complex<float>& operator-=(const complex<X>&); template<typename X> complex<float>& operator*=(const complex<X>&); template<typename X> complex<float>& operator/=(const complex<X>&); };
The complex<double>
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)
z
. Note that you might lose precision or overflow, so the constructors are explicit
.Complex number
template<> class complex<long double> { public: typedef long double value_type; complex(long double re = 0.0L, long double im = 0.0L); complex(const complex<float>&); complex(const complex<double>&); long double real() const; long double imag() const; complex<long double>& operator=(const complex<long double>&); complex<long double>& operator= (long double); complex<long double>& operator+=(long double); complex<long double>& operator-=(long double); complex<long double>& operator*=(long double); complex<long double>& operator/=(long double); template<typename X> complex<long double>& operator= (const complex<X>&); template<typename X> complex<long double>& operator+=(const complex<X>&); template<typename X> complex<long double>& operator-=(const complex<X>&); template<typename X> complex<long double>& operator*=(const complex<X>&); template<typename X> complex<long double>& operator/=(const complex<X>&); };
The complex<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
Conjugate
template<typename T> complex<T> conj(const complex<T>& z)
The conj
function returns the complex conjugate of z
.
Cosine
template<typename T> complex<T> cos(const complex<T>& z)
The cos
function returns the complex cosine of z
.
cos function in <cmath>
Hyperbolic cosine
template<typename T> complex<T> cosh(const complex<T>& z)
The cosh
function returns the complex hyperbolic cosine of z
.
cosh function in <cmath>
Exponential
template<typename T> complex<T> exp(const complex<T>& z)
The exp
function returns the exponential of z
, that is, ez.
exp function in <cmath>
Imaginary part
template<typename T> T imag(const complex<T>& z)
The imag
function returns the imaginary part of z
, that is, z.imag()
.
abs function in <cmath>
Natural logarithm
template<typename T> complex<T> log(const complex<T>& 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 [-pi i, pi i].
log function in <cmath>
Common logarithm
template<typename T> complex<T> log10(const complex<T>& 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 [-pi i, pi i].
log10 function in <cmath>
Normalized value
template<typename T> T norm(const complex<T>& z)
The norm
function returns the square of the absolute value of z
.
abs function template
Polar coordinates
template<typename T> complex<T> polar(const T& r, const T& theta)
The polar
function returns a complex object that represents the value given in polar coordinates, where r
is the magnitude and theta
is the angle (in radians).
abs function template, arg function template
Power
template<class T> complex<T> pow(const complex<T>& x, int y); template<class T> complex<T> pow(const complex<T>& x, const T& y); template<class T> complex<T> pow(const complex<T>& x, const complex<T>& y); template<class T> complex<T> pow(const T& x, const complex<T>& y);
The pow
function returns the complex power x
y. If x
and y
are both zero, 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 part
template<typename T> T real(const complex<T>& z)
The real
function returns the real part of z
, that is, z.real()
.
Sine
template<typename T> complex<T> sin(const complex<T>& z)
The sin
function returns the complex sine of z
.
sin function in <cmath>
Hyperbolic sine
template<typename T> complex<T> sinh(const complex<T>& z)
The sinh
function returns the complex hyperbolic sine of z
.
sinh function in <cmath>
Square root
template<typename T> complex<T> sqrt(const complex<T>& 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 non-negative real part.
sqrt function in <cmath>
Tangent
template<typename T> complex<T> tan(const complex<T>& z)
The tan
function returns the complex tangent of z
.
tan function in <cmath>
Hyperbolic tangent
template<typename T> complex<T> tanh(const complex<T>& z)
The tanh
function returns the complex hyperbolic tangent of z
.
tanh function in <cmath>
Unary positive/Addition
template<typename T> complex<T> operator+(const complex<T>& z); template<typename T> complex<T> operator+(const complex<T>& x, const complex<T>& y); template<typename T> complex<T> operator+(const complex<T>& x, const T& y); template<typename T> complex<T> operator+(const T& x, const complex<T>& 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 parameter is interpreted as the real part, with an imaginary part of T()
or 0.
Negation/Subtraction
template<typename T> complex<T> operator-(const complex<T>&); template<typename T> complex<T> operator-( const complex<T>&, const complex<T>&); template<typename T> complex<T> operator-(const complex<T>&, const T&); template<typename T> complex<T> operator-(const T&, const complex<T>&);
The unary negation operator returns -z
.
The binary subtraction operator returns the difference of its operands. If either operand is of type T
, the parameter is interpreted as the real part, with an imaginary part of T()
or 0.
Multiplication
template<typename T> complex<T> operator* (const complex<T>&, const complex<T>&); template<typename T> complex<T> operator*(const complex<T>&, const T&); template<typename T> complex<T> operator*(const T&, const complex<T>&);
The binary *
operator performs complex multiplication. If either operand is of type T
, the parameter is interpreted as the real part, with an imaginary part of T()
or 0.
Division
template<typename T> complex<T> operator/(const complex<T>&, const complex<T>&); template<typename T> complex<T> operator/(const complex<T>&, const T&); template<typename T> complex<T> operator/(const T&, const complex<T>&);
The binary /
operator performs complex division. If either operand is of type T
, the parameter is interpreted as the real part, with an imaginary part of T()
or 0. Division by zero results in undefined behavior.
Equality
template<typename T> bool operator== (const complex<T>&, const complex<T>&); template<typename T> bool operator==(const complex<T>&, const T&); template<typename T> bool operator==(const T&, const complex<T>&);
The ==
operator returns true if the real and imaginary parts are equal. If either operand is of type T
, the parameter is interpreted as the real part, with an imaginary part of T()
or 0.
Inequality
template<typename T> bool operator!=(const complex<T>&, const complex<T>&); template<typename T> bool operator!=(const complex<T>&, const T&); template<typename T> bool operator!=(const T&, const complex<T>&);
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.
Output
template<typename T, typename charT, typename traits> basic_ostream<charT, traits>& operator<<(basic_ostream<charT, traits>&, const complex<T>& z);
The <<
operator prints z
to the output stream, in the form (x, y)
, where x
is the real part, and y
is the imaginary part. Example 13-5 shows how z is formatted. If you want more control over the formatting, you must print the value yourself.
Example 13-5: 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(); }
Input
template<typename T, typename charT, typename traits> basic_istream<charT, traits>& operator>>(basic_istream<charT, traits>&, complex<T>& z);
The >>
operator reads a complex number from an input stream into z
. The input format can be any of the following:
x
x
is the real part, and T()
or 0 is the imaginary part.(x)
x
is the real part, and T()
or 0 is the imaginary part.(x, y)
x
is the real part, and y
is the imaginary part.