complex_polar

complex_polar (극좌표로 복소수 만들기)

크기 r과 위상각 theta로 복소수를 만들어 주는 함수예요.

출처: cppreference

본문

<complex> 헤더에 정의되어 있고, 시그니처는 다음과 같아요.

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

크기 r과 위상각 theta를 가진 복소수를 반환해요.

r이 음수이거나 NaN이거나 theta가 무한이면 동작이 정의되지 않아요.

매개변수 (Parameters)

  • r — 크기
  • theta — 위상각

반환값 (Return value)

r과 theta로 정해지는 복소수예요.

참고 (Notes)

std::polar(r, theta)는 다음 표현 중 어느 것과도 동일해요:

  • r * std::exp(theta * 1i)
  • r * (cos(theta) + sin(theta) * 1i)
  • std::complex(r * cos(theta), r * sin(theta))

polar를 exp 대신 쓰면 벡터화된 루프에서 약 4.5배 더 빠를 수 있어요.

예제 (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';
    }
}

출력:

   θ: │ 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)

이전에 발표된 C++ 표준에 소급 적용된 동작 변경 결함 보고는 다음과 같아요.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2459 C++98 일부 입력에 대해 동작이 불명확함 정의되지 않음으로 처리
LWG 2870 C++98 매개변수 theta의 기본값이 의존적이지 않음 의존적으로 만듦

더 알아보기 (Learn more)

cppreference