Difference between revisions of "cpp/numeric/complex"
m (link literal types) |
(example to contrast C.. time to add C++14 options to coliru gadget, by the way) |
||
Line 95: | Line 95: | ||
{{dsc end}} | {{dsc end}} | ||
+ | ===Example=== | ||
+ | {{example | ||
+ | |code= | ||
+ | #include <iostream> | ||
+ | #include <iomanip> | ||
+ | #include <complex> | ||
+ | #include <cmath> | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | using namespace std::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'; | ||
+ | |||
+ | double PI = std::acos(-1); | ||
+ | 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'; | ||
+ | } | ||
+ | |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) | ||
+ | }} | ||
===See also=== | ===See also=== | ||
{{dsc begin}} | {{dsc begin}} |
Revision as of 07:43, 14 October 2014
Defined in header <complex>
|
||
template< class T > class complex; |
(1) | |
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 Template:concepts for representing and manipulating complex numbers.
The effect of instantiating the template complex
for any other type is unspecified.
Contents |
Member types
Member type | Definition |
value_type
|
T
|
Member functions
constructs a complex number (public member function) | |
assigns the contents (public member function) | |
accesses the real part of the complex number (public member function) | |
accesses the imaginary part of the complex number (public member function) | |
compound assignment of two complex numbers or a complex and a scalar (public member function) |
Non-member functions
applies unary operators to complex numbers (function template) | |
performs complex number arithmetic on two complex values or a complex and a scalar (function template) | |
(removed in C++20) |
compares two complex numbers or a complex and a scalar (function template) |
serializes and deserializes a complex number (function template) | |
returns the real part (function template) | |
returns the imaginary part (function template) | |
returns the magnitude of a complex number (function template) | |
returns the phase angle (function template) | |
returns the squared magnitude (function template) | |
returns the complex conjugate (function template) | |
(C++11) |
returns the projection onto the Riemann sphere (function template) |
constructs a complex number from magnitude and phase angle (function template) | |
Exponential functions | |
complex base e exponential (function template) | |
complex natural logarithm with the branch cuts along the negative real axis (function template) | |
complex common logarithm with the branch cuts along the negative real axis (function template) | |
Power functions | |
complex power, one or both arguments may be a complex number (function template) | |
complex square root in the range of the right half-plane (function template) | |
Trigonometric functions | |
computes sine of a complex number (sin(z)) (function template) | |
computes cosine of a complex number (cos(z)) (function template) | |
computes tangent of a complex number (tan(z)) (function template) | |
(C++11) |
computes arc sine of a complex number (arcsin(z)) (function template) |
(C++11) |
computes arc cosine of a complex number (arccos(z)) (function template) |
(C++11) |
computes arc tangent of a complex number (arctan(z)) (function template) |
Hyperbolic functions | |
computes hyperbolic sine of a complex number (sinh(z)) (function template) | |
computes hyperbolic cosine of a complex number (cosh(z)) (function template) | |
computes hyperbolic tangent of a complex number (tanh(z)) (function template) | |
(C++11) |
computes area hyperbolic sine of a complex number (arsinh(z)) (function template) |
(C++11) |
computes area hyperbolic cosine of a complex number (arcosh(z)) (function template) |
(C++11) |
computes area hyperbolic tangent of a complex number (artanh(z)) (function template) |
Non-static data members
For any object For any pointer to an element of an array of These requirements essentially limit implementation of each of the three specializations of std::complex to declaring two and only two non-static data members, of type |
(since C++11) |
Literals
Defined in inline namespace
std::literals::complex_literals | |
a std::complex literal representing purely imaginary number (function) |
Example
#include <iostream> #include <iomanip> #include <complex> #include <cmath> int main() { using namespace std::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'; double PI = std::acos(-1); 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'; }
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)
See also
C documentation for Complex number arithmetic
|