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.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++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2459 | C++98 | 某些輸入的行為不明確 | 改為未定義 |
LWG 2870 | C++98 | 引數 theta 的預設值不依賴於型別 | 改為依賴於型別 |
[編輯] 另請參閱
返回複數的模 (函式模板) | |
返回相角 (函式模板) | |
複數 e 的指數 (函式模板) |