std::polar(std::complex)
来自cppreference.com
在标头 <complex> 定义 | ||
template<class T > std::complex<T> polar(const T& r, const T& theta = T()); | ||
返回拥有模 r 和辐角 theta 的复数。
若 r 为负数或 NaN,或若 theta 为无穷大,则行为未定义。
目录 |
[编辑]参数
r | - | 模 |
theta | - | 辐角 |
[编辑]返回值
以 r 和 theta 确定的复数。
[编辑]注解
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.5x 的提速。
[编辑]示例
运行此代码
#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';}}
输出:
θ: │ 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++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 2459 | C++98 | 某些输入的行为不明 | 使之为未定义 |
LWG 2870 | C++98 | 参数 theta 的默认值不依赖于类型 | 使之为依赖 |
[编辑]参阅
返回复数的模 (函数模板) | |
返回辐角 (函数模板) | |
以 e 为底复数的指数 (函数模板) |