Scroll to navigation

std::complex(3) C++ Standard Libary std::complex(3)


std::complex - std::complex


Defined in header <complex>
template< class T > (1)
class complex;
template<> class complex<float>; (2)
template<> class complex<double>; (3)
template<> class complex<long double>; (4)

The specializations std::complex<float>, std::complex<double>, and std::complex<long
double> are LiteralTypes for representing and manipulating complex numbers.

Template parameters

the type of the real and imaginary components. The behavior is unspecified (and
T - may fail to compile) if T is not float, double, or long double and undefined if
T is not NumericType.

Member types

Member type Definition
value_type T

Member functions

constructor constructs a complex number
(public member function)
operator= assigns the contents
(public member function)
real accesses the real part of the complex number
(public member function)
imag accesses the imaginary part of the complex number
(public member function)
operator-= compound assignment of two complex numbers or a complex and a scalar
operator*= (public member function)

Non-member functions

operator+ applies unary operators to complex numbers
operator- (function template)
operator+ performs complex number arithmetics on two complex values or a
operator- complex and a scalar
operator* (function template)
operator== compares two complex numbers or a complex and a scalar
operator!= (function template)
(removed in C++20)
operator<< serializes and deserializes a complex number
operator>> (function template)
real returns the real component
(function template)
imag returns the imaginary component
(function template)
abs(std::complex) returns the magnitude of a complex number
(function template)
arg returns the phase angle
(function template)
norm returns the squared magnitude
(function template)
conj returns the complex conjugate
(function template)
proj returns the projection onto the Riemann sphere
(C++11) (function template)
polar constructs a complex number from magnitude and phase angle
(function template)

Exponential functions

exp(std::complex) complex base e exponential
(function template)
complex natural logarithm with the branch cuts along the
log(std::complex) negative real axis
(function template)
complex common logarithm with the branch cuts along the negative
log10(std::complex) real axis
(function template)

Power functions

pow(std::complex) complex power, one or both arguments may be a complex number
(function template)
sqrt(std::complex) complex square root in the range of the right half-plane
(function template)

Trigonometric functions

sin(std::complex) computes sine of a complex number (\({\small\sin{z} }\)sin(z))
(function template)
cos(std::complex) computes cosine of a complex number (\({\small\cos{z} }\)cos(z))
(function template)
computes tangent of a complex number (\({\small\tan{z}
tan(std::complex) }\)tan(z))
(function template)
asin(std::complex) computes arc sine of a complex number (\({\small\arcsin{z}
(C++11) }\)arcsin(z))
(function template)
acos(std::complex) computes arc cosine of a complex number (\({\small\arccos{z}
(C++11) }\)arccos(z))
(function template)
atan(std::complex) computes arc tangent of a complex number (\({\small\arctan{z}
(C++11) }\)arctan(z))
(function template)

Hyperbolic functions

computes hyperbolic sine of a complex number (\({\small\sinh{z}
sinh(std::complex) }\)sinh(z))
(function template)
computes hyperbolic cosine of a complex number
cosh(std::complex) (\({\small\cosh{z} }\)cosh(z))
(function template)
computes hyperbolic tangent of a complex number
tanh(std::complex) (\({\small\tanh{z} }\)tanh(z))
(function template)
asinh(std::complex) computes area hyperbolic sine of a complex number
(C++11) (\({\small\operatorname{arsinh}{z} }\)arsinh(z))
(function template)
acosh(std::complex) computes area hyperbolic cosine of a complex number
(C++11) (\({\small\operatorname{arcosh}{z} }\)arcosh(z))
(function template)
atanh(std::complex) computes area hyperbolic tangent of a complex number
(C++11) (\({\small\operatorname{artanh}{z} }\)artanh(z))
(function template)

Array-oriented access

For any object z of type complex<T>, reinterpret_cast<T(&)[2]>(z)[0]
is the real part of z and reinterpret_cast<T(&)[2]>(z)[1] is the
imaginary part of z.

For any pointer to an element of an array of complex<T> named p and
any valid array index i, reinterpret_cast<T*>(p)[2*i] is the real part
of the complex number p[i], and reinterpret_cast<T*>(p)[2*i + 1] is (since C++11)
the imaginary part of the complex number p[i]

The intent of this requirement is to preserve binary compatibility
between the C++ library complex number types and the C language
complex number types (and arrays thereof), which have an identical
object representation requirement.

Implementation notes

In order to satisfy the requirements of array-oriented access, an
implementation is constrained to store the real and imaginary
components of a std::complex specialization in separate and adjacent
memory locations. Possible declarations for its non-static data
members include:

* an array of type value_type[2], with the first element holding the
real component and the second element holding the imaginary
component (e.g. Microsoft Visual Studio)
* a single member of type value_type _Complex (encapsulating the
corresponding C language complex number type) (e.g. GNU
libstdc++); (since C++11)
* two members of type value_type, with the same member access,
holding the real and the imaginary components respectively (e.g.
LLVM libc++).

An implementation cannot declare additional non-static data members
that would occupy storage disjoint from the real and imaginary
components, and must ensure that the class template specialization
does not contain any padding. The implementation must also ensure that
optimizations to array access account for the possibility that a
pointer to value_type may be aliasing a std::complex specialization or
array thereof.


Defined in inline namespace std::literals::complex_literals
operator""i A std::complex literal representing pure imaginary number
operator""il (function)


// Run this code

#include <iostream>
#include <iomanip>
#include <complex>
#include <cmath>

int main()
using namespace std::complex_literals;
std::cout << std::fixed << std::setprecision(1);

std::complex<double> z1 = 1i * 1i; // imaginary unit squared
std::cout << "i * i = " << z1 << '\n';

std::complex<double> z2 = std::pow(1i, 2); // imaginary unit squared
std::cout << "pow(i, 2) = " << z2 << '\n';

const double PI = std::acos(-1); // or std::numbers::pi in C++20
std::complex<double> z3 = std::exp(1i * PI); // Euler's formula
std::cout << "exp(i * pi) = " << z3 << '\n';

std::complex<double> z4 = 1. + 2i, z5 = 1. - 2i; // conjugates
std::cout << "(1+2i)*(1-2i) = " << z4*z5 << '\n';


i * i = (-1.0,0.0)
pow(i, 2) = (-1.0,0.0)
exp(i * pi) = (-1.0,0.0)
(1+2i)*(1-2i) = (5.0,0.0)

See also