std:: polar (std::complex)
|
定义于头文件
<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.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) << " θ: │ 极坐标: │ 指数形式: │ 复数形式: │ 三角形式:\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'; } }
输出:
θ: │ 极坐标: │ 指数形式: │ 复数形式: │ 三角形式: +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
为底的指数函数
(函数模板) |