Common mathematical functions | |||||||||||||||||||||||||||||||
Mathematical special functions(C++17) | |||||||||||||||||||||||||||||||
Mathematical constants(C++20) | |||||||||||||||||||||||||||||||
Basic linear algebra algorithms(C++26) | |||||||||||||||||||||||||||||||
Data-parallel types (SIMD)(C++26) | |||||||||||||||||||||||||||||||
Floating-point environment(C++11) | |||||||||||||||||||||||||||||||
Complex numbers | |||||||||||||||||||||||||||||||
Numeric array (valarray ) | |||||||||||||||||||||||||||||||
Pseudo-random number generation | |||||||||||||||||||||||||||||||
Bit manipulation(C++20) | |||||||||||||||||||||||||||||||
Saturation arithmetic(C++26) | |||||||||||||||||||||||||||||||
Factor operations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Interpolations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Generic numeric operations | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
C-style checked integer arithmetic | |||||||||||||||||||||||||||||||
|
Defined in header <complex> | ||
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<longdouble>; | (4) | (until C++23) |
Specializations ofstd::complex
for cv-unqualifiedstandard(until C++23)floating-point types areTriviallyCopyable(since C++23)LiteralTypes for representing and manipulatingcomplex number.
Contents |
T | - | the type of the real and imaginary parts. The behavior is unspecified (and may fail to compile) ifT is not a cv-unqualifiedstandard(until C++23) floating-point type and undefined ifT is notNumericType. |
Member type | Definition |
value_type | T |
constructs a complex number (public member function)[edit] | |
assigns the contents (public member function)[edit] | |
accesses the real part of the complex number (public member function)[edit] | |
accesses the imaginary part of the complex number (public member function)[edit] | |
compound assignment of two complex numbers or a complex and a scalar (public member function)[edit] |
applies unary operators to complex numbers (function template)[edit] | |
performs complex number arithmetic on two complex values or a complex and a scalar (function template)[edit] | |
(removed in C++20) | compares two complex numbers or a complex and a scalar (function template)[edit] |
serializes and deserializes a complex number (function template)[edit] | |
(C++26) | obtains a reference to real or imaginary part from astd::complex (function template)[edit] |
returns the real part (function template)[edit] | |
returns the imaginary part (function template)[edit] | |
returns the magnitude of a complex number (function template)[edit] | |
returns the phase angle (function template)[edit] | |
returns the squared magnitude (function template)[edit] | |
returns the complex conjugate (function template)[edit] | |
(C++11) | returns the projection onto the Riemann sphere (function template)[edit] |
constructs a complex number from magnitude and phase angle (function template)[edit] | |
Exponential functions | |
complex basee exponential (function template)[edit] | |
complex natural logarithm with the branch cuts along the negative real axis (function template)[edit] | |
complex common logarithm with the branch cuts along the negative real axis (function template)[edit] | |
Power functions | |
complex power, one or both arguments may be a complex number (function template)[edit] | |
complex square root in the range of the right half-plane (function template)[edit] | |
Trigonometric functions | |
computes sine of a complex number (\({\small\sin{z}}\)sin(z)) (function template)[edit] | |
computes cosine of a complex number (\({\small\cos{z}}\)cos(z)) (function template)[edit] | |
computes tangent of a complex number (\({\small\tan{z}}\)tan(z)) (function template)[edit] | |
(C++11) | computes arc sine of a complex number (\({\small\arcsin{z}}\)arcsin(z)) (function template)[edit] |
(C++11) | computes arc cosine of a complex number (\({\small\arccos{z}}\)arccos(z)) (function template)[edit] |
(C++11) | computes arc tangent of a complex number (\({\small\arctan{z}}\)arctan(z)) (function template)[edit] |
Hyperbolic functions | |
computes hyperbolic sine of a complex number (\({\small\sinh{z}}\)sinh(z)) (function template)[edit] | |
computes hyperbolic cosine of a complex number (\({\small\cosh{z}}\)cosh(z)) (function template)[edit] | |
computes hyperbolic tangent of a complex number (\({\small\tanh{z}}\)tanh(z)) (function template)[edit] | |
(C++11) | computes area hyperbolic sine of a complex number (\({\small\operatorname{arsinh}{z}}\)arsinh(z)) (function template)[edit] |
(C++11) | computes area hyperbolic cosine of a complex number (\({\small\operatorname{arcosh}{z}}\)arcosh(z)) (function template)[edit] |
(C++11) | computes area hyperbolic tangent of a complex number (\({\small\operatorname{artanh}{z}}\)artanh(z)) (function template)[edit] |
obtains the size of astd::complex (class template specialization)[edit] | |
obtains the underlying real and imaginary number type of astd::complex (class template specialization)[edit] |
For any objectz of typestd::complex<T>
,reinterpret_cast<T(&)[2]>(z)[0] is the real part ofz andreinterpret_cast<T(&)[2]>(z)[1] is the imaginary part ofz.
For any pointer to an element of an array ofstd::complex<T>
namedp and any valid array indexi,reinterpret_cast<T*>(p)[2* i] is the real part of the complex numberp[i], andreinterpret_cast<T*>(p)[2* i+1] is the imaginary part of the complex numberp[i].
The intent of this requirement is to preserve binary compatibility between the C++ library complex number types and theC language complex number types (and arrays thereof), which have an identical object representation requirement.
In order to satisfy the requirements of array-oriented access, an implementation is constrained to store the real and imaginary parts of astd::complex
specialization in separate and adjacent memory locations. Possible declarations for its non-static data members include:
value_type[2]
, with the first element holding the real part and the second element holding the imaginary part (e.g. Microsoft Visual Studio);value_type _Complex
(encapsulating the correspondingC language complex number type) (e.g. GNU libstdc++);value_type
, with the same member access, holding the real and the imaginary parts respectively (e.g. LLVM libc++).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 anypadding bit. The implementation must also ensure that optimizations to array access account for the possibility that a pointer tovalue_type
may be aliasing astd::complex
specialization or array thereof.
Defined in inline namespace std::literals::complex_literals | |
astd::complex literal representing purely imaginary number (function)[edit] |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_constexpr_complex | 201711L | (C++20) | constexpr simple complex mathematical functions in<complex> |
202306L | (C++26) | Moreconstexpr for<complex> | |
__cpp_lib_tuple_like | 202311L | (C++26) | Add tuple protocol tostd::complex |
#include <cmath>#include <complex>#include <iomanip>#include <iostream>#include <ranges> int main(){usingnamespace std::complex_literals;std::cout<<std::fixed<<std::setprecision(1); std::complex<double> z1= 1i* 1i;// imaginary unit squaredstd::cout<<"i * i = "<< z1<<'\n'; std::complex<double> z2=std::pow(1i,2);// imaginary unit squaredstd::cout<<"pow(i, 2) = "<< z2<<'\n'; constdouble PI=std::acos(-1);// or std::numbers::pi in C++20 std::complex<double> z3=std::exp(1i* PI);// Euler's formulastd::cout<<"exp(i * pi) = "<< z3<<'\n'; std::complex<double> z4=1.0+ 2i, z5=1.0- 2i;// conjugatesstd::cout<<"(1 + 2i) * (1 - 2i) = "<< z4* z5<<'\n'; constauto zz={0.0+ 1i,2.0+ 3i,4.0+ 5i};#if __cpp_lib_tuple_like >= 202311Lfor(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';#elsefor(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.01.0 3.0 5.0
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 Ccomplex | guaranteed to be compatible |
C documentation forComplex number arithmetic |