std::complex - cppreference.com (original) (raw)

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

Specializations of std::complex for cv-unqualified standard(until C++23) floating-point types are TriviallyCopyable(since C++23) LiteralTypes for representing and manipulating complex number.

Contents

[edit] Template parameters

T - the type of the real and imaginary parts. The behavior is unspecified (and may fail to compile) if T is not a cv-unqualified standard(until C++23) floating-point type and undefined if T is not NumericType.

[edit] Member types

Member type Definition
value_type T

[edit] Member functions

[edit] Non-member functions

operator+operator- applies unary operators to complex numbers (function template) [edit]
operator+operator-operator*operator/ performs complex number arithmetic on two complex values or a complex and a scalar (function template) [edit]
operator==operator!=(removed in C++20) compares two complex numbers or a complex and a scalar (function template) [edit]
operator<> serializes and deserializes a complex number (function template) [edit]
get(std::complex)(C++26) obtains a reference to real or imaginary part from a std::complex (function template) [edit]
real returns the real part (function template) [edit]
imag returns the imaginary part (function template) [edit]
abs(std::complex) returns the magnitude of a complex number (function template) [edit]
arg returns the phase angle (function template) [edit]
norm returns the squared magnitude (function template) [edit]
conj returns the complex conjugate (function template) [edit]
proj(C++11) returns the projection onto the Riemann sphere (function template) [edit]
polar constructs a complex number from magnitude and phase angle (function template) [edit]
Exponential functions
exp(std::complex) complex base e exponential (function template) [edit]
log(std::complex) complex natural logarithm with the branch cuts along the negative real axis (function template) [edit]
log10(std::complex) complex common logarithm with the branch cuts along the negative real axis (function template) [edit]
Power functions
pow(std::complex) complex power, one or both arguments may be a complex number (function template) [edit]
sqrt(std::complex) complex square root in the range of the right half-plane (function template) [edit]
Trigonometric functions
sin(std::complex) computes sine of a complex number (\({\small\sin{z}}\)sin(z)) (function template) [edit]
cos(std::complex) computes cosine of a complex number (\({\small\cos{z}}\)cos(z)) (function template) [edit]
tan(std::complex) computes tangent of a complex number (\({\small\tan{z}}\)tan(z)) (function template) [edit]
asin(std::complex)(C++11) computes arc sine of a complex number (\({\small\arcsin{z}}\)arcsin(z)) (function template) [edit]
acos(std::complex)(C++11) computes arc cosine of a complex number (\({\small\arccos{z}}\)arccos(z)) (function template) [edit]
atan(std::complex)(C++11) computes arc tangent of a complex number (\({\small\arctan{z}}\)arctan(z)) (function template) [edit]
Hyperbolic functions
sinh(std::complex) computes hyperbolic sine of a complex number (\({\small\sinh{z}}\)sinh(z)) (function template) [edit]
cosh(std::complex) computes hyperbolic cosine of a complex number (\({\small\cosh{z}}\)cosh(z)) (function template) [edit]
tanh(std::complex) computes hyperbolic tangent of a complex number (\({\small\tanh{z}}\)tanh(z)) (function template) [edit]
asinh(std::complex)(C++11) computes area hyperbolic sine of a complex number (\({\small\operatorname{arsinh}{z}}\)arsinh(z)) (function template) [edit]
acosh(std::complex)(C++11) computes area hyperbolic cosine of a complex number (\({\small\operatorname{arcosh}{z}}\)arcosh(z)) (function template) [edit]
atanh(std::complex)(C++11) computes area hyperbolic tangent of a complex number (\({\small\operatorname{artanh}{z}}\)artanh(z)) (function template) [edit]

[edit] Helper types

[edit] Array-oriented access

For any object z of type std::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 std::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 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.

[edit] Implementation notes

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

An implementation cannot declare additional non-static data members that would occupy storage disjoint from the real and imaginary parts, and must ensure that the class template specialization does not contain any padding bit. 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.

[edit] Literals

[edit] Notes

Feature-test macro Value Std Feature
__cpp_lib_constexpr_complex 201711L (C++20) constexpr simple complex mathematical functions in
202306L (C++26) More constexpr for
__cpp_lib_tuple_like 202311L (C++26) Add tuple protocol to std::complex

[edit] Example

#include #include #include #include #include   int main() { using namespace std::complex_literals; std::cout << std::fixed << std::setprecision(1);   std::complex z1 = 1i * 1i; // imaginary unit squared std::cout << "i * i = " << z1 << '\n';   std::complex z2 = std::pow(1i, 2); // imaginary unit squared std::cout << "pow(i, 2) = " << z2 << '\n';   const double PI = std::acos(-1); // or std:🔢:pi in C++20 std::complex z3 = std::exp(1i * PI); // Euler's formula std::cout << "exp(i * pi) = " << z3 << '\n';   std::complex z4 = 1.0 + 2i, z5 = 1.0 - 2i; // conjugates std::cout << "(1 + 2i) * (1 - 2i) = " << z4 * z5 << '\n';   const auto zz = {0.0 + 1i, 2.0 + 3i, 4.0 + 5i}; #if __cpp_lib_tuple_like >= 202311L for (double re : zz | std::views::keys) std::cout << re << ' '; std::cout << '\n'; for (double im : zz | std::views::values) std::cout << im << ' '; std::cout << '\n'; #else for (double re : zz | std::views::transform([](auto z){ return z.real(); })) std::cout << re << ' '; std::cout << '\n'; for (double im : zz | std::views::transform([](auto z){ return z.imag(); })) std::cout << im << ' '; std::cout << '\n'; #endif }

Output:

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) 0.0 2.0 4.0 1.0 3.0 5.0

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 387 C++98 std::complex was not guaranteed to be compatible with C complex guaranteed to be compatible

[edit] See also