std::polar(std::complex)(3)
π NAME π
okay so bestie, this page is std::polar(std::complex) β the NAME line is literally std::polar(std::complex) - std::polar(std::complex), and it lives in the C++ Standard Libary manual, section 3. iconic, no notes. π
β¨ Synopsis β¨
Defined in header <complex>:
template< class T >
std::complex<T> polar( const T& r, const T& theta = T() );
Returns a complex number with magnitude r and phase angle theta.
π¨ the behavior is undefined ifris negative or NaN, or ifthetais infinite. like, literally undefined β do not do it, bestie. π
π Parameters π
rβ magnitude.thetaβ phase angle.
π¦ Return value π¦
A complex number determined by r and theta. that's the whole vibe. π
πΈ Notes πΈ
std::polar(r, theta) is equivalent to any of the following expressions:
r * std::exp(theta * 1i)r * (cos(theta) + sin(theta) * 1i)std::complex(r * cos(theta), r * sin(theta)).
Using polar instead of exp can be about 4.5x faster in vectorized loops. highkey a slay. π₯
π Example π
the page says // Run this code, and here's the exact code, no cap:
#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 π
and the output is giving:
ΞΈ: β 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)
π₯ Defect reports π₯
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 2459β applied to C++98; behavior as published: behavior unclear for some inputs; correct behavior: made undefined.LWG 2870β applied to C++98; behavior as published: default value of parameterthetanot dependent; correct behavior: made dependent.
π See also π
abs(std::complex)β returns the magnitude of a complex number (function template).argβ returns the phase angle (function template).exp(std::complex)β complex base e exponential (function template).
that's the whole page, bestie! source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::polar(std::complex)(3). mwah πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms