std::polar(std::complex)

Header: <complex>

Returns a complex number with magnitude r and phase angle theta.

# Declarations

template< class T >
std::complex<T> polar( const T& r, const T& theta = T() );

# Parameters

# Return value

A complex number determined by r and theta.

# Notes

std::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.

# 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';
    }
}

# Defect reports

DRApplied toBehavior as publishedCorrect behavior
LWG 2459C++98behavior unclear for some inputsmade undefined
LWG 2870C++98default value of parameter theta not dependentmade dependent

# See also