Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::polar(std::complex)

      From cppreference.com
      <cpp‎ |numeric‎ |complex
       
       
       
       
      Defined in header<complex>
      template<class T>
      std::complex<T> polar(const T& r,const T& theta= T());

      Returns a complex number with magnituder and phase angletheta.

      The behavior is undefined ifr is negative or NaN, or iftheta is infinite.

      Contents

      [edit]Parameters

      r - magnitude
      theta - phase angle

      [edit]Return value

      A complex number determined byr andtheta.

      [edit]Notes

      std::polar(r, theta) is equivalent to any of the following expressions:

      Using polar instead of exp can be about4.5x faster in vectorized loops.

      [edit]Example

      Run this code
      #include <cmath>#include <complex>#include <iomanip>#include <iostream>#include <numbers>usingnamespace std::complex_literals; int main(){constexprauto π_2{std::numbers::pi/2.0};constexprauto mag{1.0}; std::cout<<std::fixed<<std::showpos<<std::setprecision(1)<<"   θ: │ polar:      │ exp:        │ complex:    │ trig:\n";for(int n{}; n!=4;++n){constauto θ{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:

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

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

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

      [edit]See also

      returns the magnitude of a complex number
      (function template)[edit]
      returns the phase angle
      (function template)[edit]
      complex basee exponential
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/complex/polar&oldid=160824"

      [8]ページ先頭

      ©2009-2025 Movatter.jp