std::polar(std::complex)(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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 if r is negative or NaN, or if theta is 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 parameter theta not 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