Returns a complex number with magnitude r and phase angle theta.
The behavior is undefined if r is negative or NaN, or if theta is infinite.
[edit] Parameters r - magnitude theta - phase angle [edit] Return valueA complex number determined by r and theta.
[edit] Notesstd::polar(r, theta) is equivalent to any of the following expressions:
Using polar instead of exp can be about 4.5x faster in vectorized loops.
[edit] Example#include <cmath> #include <complex> #include <iomanip> #include <iostream> #include <numbers> using namespace std::complex_literals; int main() { constexpr auto Ï_2{std::numbers::pi / 2.0}; constexpr auto mag{1.0}; std::cout << std::fixed << std::showpos << std::setprecision(1) << " θ: â polar: â exp: â complex: â trig:\n"; for (int n{}; n != 4; ++n) { const auto θ{n * Ï_2}; std::cout << std::setw(4) << 90 * n << "° â " << std::polar(mag, θ) << " â " << mag * std::exp(θ * 1.0i) << " â " << std::complex(mag * cos(θ), mag * sin(θ)) << " â " << mag * (cos(θ) + 1.0i * sin(θ)) << '\n'; } }
Output:
θ: â polar: â exp: â complex: â trig: +0° â (+1.0,+0.0) â (+1.0,+0.0) â (+1.0,+0.0) â (+1.0,+0.0) +90° â (+0.0,+1.0) â (+0.0,+1.0) â (+0.0,+1.0) â (+0.0,+1.0) +180° â (-1.0,+0.0) â (-1.0,+0.0) â (-1.0,+0.0) â (-1.0,+0.0) +270° â (-0.0,-1.0) â (-0.0,-1.0) â (-0.0,-1.0) â (-0.0,-1.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 2459 C++98 behavior unclear for some inputs made undefined LWG 2870 C++98 default value of parameter theta not dependent made dependent [edit] See alsoRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4