Difference between revisions of "cpp/numeric/complex/pow"
m (Minor fix.) |
Andreas Krug (Talk | contribs) m (fmt, headers sorted) |
||
Line 87: | Line 87: | ||
{{example| | {{example| | ||
|code= | |code= | ||
− | |||
#include <complex> | #include <complex> | ||
+ | #include <iostream> | ||
int main() | int main() | ||
Line 94: | Line 94: | ||
std::cout << std::fixed; | std::cout << std::fixed; | ||
− | std::complex<double> z(1, 2); | + | std::complex<double> z(1.0, 2.0); |
std::cout << "(1,2)^2 = " << std::pow(z, 2) << '\n'; | std::cout << "(1,2)^2 = " << std::pow(z, 2) << '\n'; | ||
− | std::complex<double> z2(-1, 0); // square root of -1 | + | std::complex<double> z2(-1.0, 0.0); // square root of -1 |
std::cout << "-1^0.5 = " << std::pow(z2, 0.5) << '\n'; | std::cout << "-1^0.5 = " << std::pow(z2, 0.5) << '\n'; | ||
− | std::complex<double> z3(-1, -0.0); // other side of the cut | + | std::complex<double> z3(-1.0, -0.0); // other side of the cut |
std::cout << "(-1,-0)^0.5 = " << std::pow(z3, 0.5) << '\n'; | std::cout << "(-1,-0)^0.5 = " << std::pow(z3, 0.5) << '\n'; | ||
− | std::complex<double> i(0, 1); // i^i = exp(-pi/2) | + | std::complex<double> i(0.0, 1.0); // i^i = exp(-pi / 2) |
std::cout << "i^i = " << std::pow(i, i) << '\n'; | std::cout << "i^i = " << std::pow(i, i) << '\n'; | ||
} | } |
Revision as of 07:02, 22 April 2023
Defined in header <complex>
|
||
template< class T > std::complex<T> pow( const std::complex<T>& x, const std::complex<T>& y ); |
(1) | |
template< class T > std::complex<T> pow( const std::complex<T>& x, const T& y ); |
(2) | |
template< class T > std::complex<T> pow( const T& x, const std::complex<T>& y ); |
(3) | |
Additional overloads (since C++11) |
||
Defined in header <complex>
|
||
(A) | ||
template< class T1, class T2 > std::complex</* common-type */> |
(until C++23) | |
template< class T1, class T2 > std::complex<std::common_type_t<T1, T2>> |
(since C++23) | |
(B) | ||
template< class T, class NonComplex > std::complex</* common-type */> |
(until C++23) | |
template< class T, class NonComplex > std::complex<std::common_type_t<T, NonComplex>> |
(since C++23) | |
(C) | ||
template< class T, class NonComplex > std::complex</* common-type */> |
(until C++23) | |
template< class T, class NonComplex > std::complex<std::common_type_t<T, NonComplex>> |
(since C++23) | |
A-C) Additional overloads are provided. base and exponent are treated as complex numbers with positive zero imaginary component.
|
(since C++11) |
Contents |
Parameters
x | - | base as a complex value |
y | - | exponent as a complex value |
base | - | base as a non-complex value |
exponent | - | exponent as a non-complex value |
Return value
Notes
The additional overloads are not required to be provided exactly as (A-C). They only need to be sufficient to ensure that for their first argument base and second argument exponent:
If base and/or exponent has type std::complex<T>:
|
(until C++23) |
If one argument has type std::complex<T1> and the other argument has type If std::common_type_t<T1, T2> is not well-formed, then the program is ill-formed. |
(since C++23) |
Example
#include <complex> #include <iostream> int main() { std::cout << std::fixed; std::complex<double> z(1.0, 2.0); std::cout << "(1,2)^2 = " << std::pow(z, 2) << '\n'; std::complex<double> z2(-1.0, 0.0); // square root of -1 std::cout << "-1^0.5 = " << std::pow(z2, 0.5) << '\n'; std::complex<double> z3(-1.0, -0.0); // other side of the cut std::cout << "(-1,-0)^0.5 = " << std::pow(z3, 0.5) << '\n'; std::complex<double> i(0.0, 1.0); // i^i = exp(-pi / 2) std::cout << "i^i = " << std::pow(i, i) << '\n'; }
Output:
(1,2)^2 = (-3.000000,4.000000) -1^0.5 = (0.000000,1.000000) (-1,-0)^0.5 = (0.000000,-1.000000) i^i = (0.207880,0.000000)
See also
complex square root in the range of the right half-plane (function template) | |
(C++11)(C++11) |
raises a number to the given power (xy) (function) |
applies the function std::pow to two valarrays or a valarray and a value (function template) | |
C documentation for cpow
|