Common mathematical functions | |||||||||||||||||||||||||||||||
Mathematical special functions(C++17) | |||||||||||||||||||||||||||||||
Mathematical constants(C++20) | |||||||||||||||||||||||||||||||
Basic linear algebra algorithms(C++26) | |||||||||||||||||||||||||||||||
Data-parallel types (SIMD)(C++26) | |||||||||||||||||||||||||||||||
Floating-point environment(C++11) | |||||||||||||||||||||||||||||||
Complex numbers | |||||||||||||||||||||||||||||||
Numeric array (valarray ) | |||||||||||||||||||||||||||||||
Pseudo-random number generation | |||||||||||||||||||||||||||||||
Bit manipulation(C++20) | |||||||||||||||||||||||||||||||
Saturation arithmetic(C++26) | |||||||||||||||||||||||||||||||
Factor operations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Interpolations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Generic numeric operations | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
C-style checked integer arithmetic | |||||||||||||||||||||||||||||||
|
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 |
r | - | magnitude |
theta | - | phase angle |
A complex number determined byr andtheta.
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.
#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)
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 2459 | C++98 | behavior unclear for some inputs | made undefined |
LWG 2870 | C++98 | default value of parametertheta not dependent | made dependent |
returns the magnitude of a complex number (function template)[edit] | |
returns the phase angle (function template)[edit] | |
complex basee exponential (function template)[edit] |