# <complex> <complex> # `` * * * **`abs· arg· complex· complex· complex· complex· conj· cos· cosh· exp· imag· log· log10· norm· operator!=· operator*· operator+· operator-· operator/· operator<<· operator==· operator>>· polar· pow· real· sin· sinh· sqrt· tan· tanh· __STD_COMPLEX`** * * * Include the standard header **``** 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]`. Many of the functions declared in this header have **additional overloads**. The following functions have such additional overloads: abs conj polar acos cos pow acosh cosh real arg exp sin asin imag sinh asinh log sqrt atan log10 tan atanh norm tanhCopy to clipboard * * * namespace std { #define __STD_COMPLEX // TEMPLATE CLASSES template class complex; template<> class complex; template<> class complex; template<> class complex; // TEMPLATE FUNCTIONS template complex operator+(const complex& left, const complex& right); template complex operator+(const complex& left, const Ty& right); template complex operator+(const Ty& left, const complex& right); template complex operator-(const complex& left, const complex& right); template complex operator-(const complex& left, const Ty& right); template complex operator-(const Ty& left, const complex& right); template complex operator*(const complex& left, const complex& right); template complex operator*(const complex& left, const Ty& right); template complex operator*(const Ty& left, const complex& right); template complex operator/(const complex& left, const complex& right); template complex operator/(const complex& left, const Ty& right); template complex operator/(const Ty& left, const complex& right); template complex operator+(const complex& left); template complex operator-(const complex& left); template bool operator==(const complex& left, const complex& right); template bool operator==(const complex& left, const Ty& right); template bool operator==(const Ty& left, const complex& right); template bool operator!=(const complex& left, const complex& right); template bool operator!=(const complex& left, const Ty& right); template bool operator!=(const Ty& left, const complex& right); template basic_istream& operator>>(basic_istream& istr, complex& right); template basic_ostream& operator<<(basic_ostream& ostr, const complex& right); template Ty real(const complex& left); template Ty imag(const complex& left); template Ty abs(const complex& left); template Ty arg(const complex& left); template Ty norm(const complex& left); template complex conj(const complex& left); template complex polar(const Ty& rho, const Ty& theta = 0); template complex cos(const complex& left); template complex cosh(const complex& left); template complex exp(const complex& left); template complex log(const complex& left); template complex log10(const complex& left); template complex pow(const complex& left, int right); template complex pow(const complex& left, const Ty& right); template complex pow(const complex& left, const complex& right); template complex pow(const Ty& left, const complex& right); template complex sin(const complex& left); template complex sinh(const complex& left); template complex sqrt(const complex& left); } // namespace stdCopy to clipboard ## `abs` template Ty abs(const complex& left);Copy to clipboard The function returns the magnitude of `left`. ## `arg` template Ty arg(const complex& left);Copy to clipboard The function returns the phase angle of `left`. ## `complex` template class complex { public: typedef Ty value_type; Ty real() const; Ty imag() const; complex(const Ty& realval = 0, const Ty& imagval = 0); template complex(const complex& right); template complex& operator=(const complex& right); template complex& operator+=(const complex& right); template complex& operator-=(const complex& right); template complex& operator*=(const complex& right); template complex& operator/=(const complex& 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); };Copy to clipboard 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`: - has a public default constructor, destructor, copy constructor, and assignment operator -- with conventional behavior - can be assigned integer or floating-point values, or type cast to such values -- with conventional behavior - defines the arithmetic operators and math functions, as needed, that are defined for the floating-point types -- with conventional behavior 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](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#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 complex(const complex& right);Copy to clipboard 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()`. In this [implementation](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#implementation), if a translator does not support member template functions, the template: template complex(const complex& right);Copy to clipboard is replaced by: complex(const complex& right);Copy to clipboard which is the copy constructor. ### `complex::imag` Ty imag() const;Copy to clipboard The member function returns the stored imaginary part. ### `complex::operator*=` template complex& operator*=(const complex& right); complex& operator*=(const Ty& right);Copy to clipboard 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`. In this [implementation](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#implementation), if a translator does not support member template functions, the template: template complex& operator*=(const complex& right);Copy to clipboard is replaced by: complex& operator*=(const complex& right);Copy to clipboard ### `complex::operator+=` template complex& operator+=(const complex& right); complex& operator+=(const Ty& right);Copy to clipboard 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`. In this [implementation](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#implementation), if a translator does not support member template functions, the template: template complex& operator+=(const complex& right);Copy to clipboard is replaced by: complex& operator+=(const complex& right);Copy to clipboard ### `complex::operator-=` template complex& operator-=(const complex& right); complex& operator-=(const Ty& right);Copy to clipboard 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`. In this [implementation](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#implementation), if a translator does not support member template functions, the template: template complex& operator-=(const complex& right);Copy to clipboard is replaced by: complex& operator-=(const complex& right);Copy to clipboard ### `complex::operator/=` template complex& operator/=(const complex& right); complex& operator/=(const Ty& right);Copy to clipboard 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`. In this [implementation](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#implementation), if a translator does not support member template functions, the template: template complex& operator/=(const complex& right);Copy to clipboard is replaced by: complex& operator/=(const complex& right);Copy to clipboard ### `complex::operator=` template complex& operator=(const complex& right); complex& operator=(const Ty& right);Copy to clipboard 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`. In this [implementation](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#implementation), if a translator does not support member template functions, the template: template complex& operator=(const complex& right);Copy to clipboard is replaced by: complex& operator=(const complex& right);Copy to clipboard which is the default assignment operator. ### `complex::real` Ty real() const;Copy to clipboard The member function returns the stored real part. ### `complex::value_type` typedef Ty value_type;Copy to clipboard The type is a synonym for the template parameter `Ty`. ## `complex` template<> class complex { public: complex(double realval = 0, double imagval = 0); complex(const complex& right); explicit complex(const complex& right); // rest same as template class complex };Copy to clipboard 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` template<> class complex { public: complex(float realval = 0, float imagval = 0); explicit complex(const complex& right); explicit complex(const complex& right); // rest same as template class complex };Copy to clipboard 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` template<> class complex { public: complex(long double realval = 0, long double imagval = 0); complex(const complex& right); complex(const complex& right); // rest same as template class complex };Copy to clipboard 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 complex conj(const complex& left);Copy to clipboard The function returns the conjugate of `left`. ## `cos` template complex cos(const complex& left);Copy to clipboard The function returns the cosine of `left`. ## `cosh` template complex cosh(const complex& left);Copy to clipboard The function returns the hyperbolic cosine of `left`. ## `exp` template complex exp(const complex& left);Copy to clipboard The function returns the exponential of `left`. ## `imag` template Ty imag(const complex& left);Copy to clipboard The function returns the imaginary part of `left`. ## `log` template complex log(const complex& left);Copy to clipboard The function returns the logarithm of `left`. The branch cuts are along the negative real axis. ## `log10` template complex log10(const complex& left);Copy to clipboard The function returns the base 10 logarithm of `left`. The branch cuts are along the negative real axis. ## `norm` template Ty norm(const complex& left);Copy to clipboard The function returns the squared magnitude of `left`. ## `operator!=` template bool operator!=(const complex& left, const complex& right); template bool operator!=(const complex& left, const Ty& right); template bool operator!=(const Ty& left, const complex& right);Copy to clipboard The operators each return true only if `real(left) != real(right) || imag(left) != imag(right)`. ## `operator*` template complex operator*(const complex& left, const complex& right); template complex operator*(const complex& left, const Ty& right); template complex operator*(const Ty& left, const complex& right);Copy to clipboard The operators each convert both operands to the return type, then return the complex product of the converted `left` and `right`. ## `operator+` template complex operator+(const complex& left, const complex& right); template complex operator+(const complex& left, const Ty& right); template complex operator+(const Ty& left, const complex& right); template complex operator+(const complex& left);Copy to clipboard 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 complex operator-(const complex& left, const complex& right); template complex operator-(const complex& left, const Ty& right); template complex operator-(const Ty& left, const complex& right); template complex operator-(const complex& left);Copy to clipboard 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 complex operator/(const complex& left, const complex& right); template complex operator/(const complex& left, const Ty& right); template complex operator/(const Ty& left, const complex& right);Copy to clipboard The operators each convert both operands to the return type, then return the complex quotient of the converted `left` and `right`. ## `operator<<` template basic_ostream& operator<<(basic_ostream& ostr, const complex& right);Copy to clipboard The template function inserts the complex value `right` in the output stream `os`, effectively by executing: basic_ostringstream osstr; osstr.flags(ostr.flags()); osstr.imbue(ostr.imbue()); osstr.precision(ostr.precision()); osstr << '(' << real(right) << ',' << imag(right) << ')'; ostr << osstr.str().c_str();Copy to clipboard 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 bool operator==(const complex& left, const complex& right); template bool operator==(const complex& left, const Ty& right); template bool operator==(const Ty& left, const complex& right);Copy to clipboard The operators each return true only if `real(left) == real(right) && imag(left) == imag(right)`. ## `operator>>` template basic_istream& operator>>(basic_istream& istr, complex& right);Copy to clipboard 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 == ')'Copy to clipboard 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 complex polar(const Ty& rho, const Ty& theta = 0);Copy to clipboard The function returns the complex value whose magnitude is `rho` and whose phase angle is `theta`. ## `pow` template complex pow(const complex& left, int right); template complex pow(const complex& left, const Ty& right); template complex pow(const complex& left, const complex& right); template complex pow(const Ty& left, const complex& right);Copy to clipboard 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. ## `real` template Ty real(const complex& left);Copy to clipboard The function returns the real part of `left`. ## `sin` template complex sin(const complex& left);Copy to clipboard The function returns the sine of `left`. ## `sinh` template complex sinh(const complex& left);Copy to clipboard The function returns the hyperbolic sine of `left`. ## `sqrt` template complex sqrt(const complex& left);Copy to clipboard 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_COMPLEXCopy to clipboard The macro is defined, with an unspecified expansion, to indicate compliance with the specifications of this header. ## `tan` template complex tan(const complex& left);Copy to clipboard The function returns the tangent of `left`. ## `tanh` template complex tanh(const complex& left);Copy to clipboard The function returns the hyperbolic tangent of `left`. * * * See also the **[Table of Contents](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_index.html#Table%20of%20Contents)** and the **[Index](https://docs.qualcomm.com/doc/80-N3040-23/topic/_cpp_index.html)**. *[Copyright](https://docs.qualcomm.com/doc/80-N3040-23/topic/cpp_crit_pjp.html) © 1992-2006by P.J. Plauger. All rights reserved.* Last Published: Oct 06, 2026 Previous Topic Next Topic