名前空間
変種
操作

std::polar(std::complex)

From cppreference.com
< cpp‎ | numeric‎ | complex
 
 
 
 
ヘッダ <complex> で定義
template< class T >
std::complex<T> polar( const T& r, const T& theta = T() );

大きさ r、位相角 theta の複素数を返します。

r が負または NaN の場合、または theta が無限大の場合、未定義の動作となります。

目次

[編集] パラメータ

r - magnitude
theta - 位相角

[編集] 戻り値

rtheta によって決定される複素数。

[編集] メモ

std::polar(r, theta) は、以下のいずれかの式と同等です。

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

exp の代わりに polar を使用すると、ベクトル化されたループで **4.5 倍** 高速になる場合があります。

[編集]

#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)

[編集] 不具合報告

以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。

DR 適用対象 公開された動作 正しい動作
LWG 2459 C++98 一部の入力に対する動作が不明瞭 未定義に変更
LWG 2870 C++98 theta パラメータのデフォルト値が依存していなかった 依存するように変更

[編集] 関連項目

複素数の大きさを返す
(関数テンプレート) [編集]
偏角を返す
(関数テンプレート) [編集]
複素数の底eの指数関数
(関数テンプレート) [編集]
English 日本語 中文(简体) 中文(繁體)