std::atan2, std::atan2f, std::atan2l
定義於標頭檔案 <cmath> |
||
(1) | ||
float atan2 ( float y, float x ); double atan2 ( double y, double x ); |
(直至 C++23) | |
/*floating-point-type*/ atan2 ( /*floating-point-type*/ y, |
(C++23 起) (C++26 起為 constexpr) |
|
float atan2f( float y, float x ); |
(2) | (C++11 起) (C++26 起為 constexpr) |
long double atan2l( long double y, long double x ); |
(3) | (C++11 起) (C++26 起為 constexpr) |
SIMD 過載 (C++26 起) |
||
定義於標頭檔案 <simd> |
||
template< class V0, class V1 > constexpr /*math-common-simd-t*/<V0, V1> |
(S) | (C++26 起) |
額外過載 (自 C++11 起) |
||
定義於標頭檔案 <cmath> |
||
template< class Integer > double atan2 ( Integer y, Integer x ); |
(A) | (C++26 起為 constexpr) |
std::atan2
的過載。(C++23 起)
S) SIMD 過載對 v_y 和 v_x 執行逐元素
std::atan2 。
|
(C++26 起) |
A) 為所有整數型別提供了額外的過載,它們被視為 double。
|
(C++11 起) |
目錄 |
[編輯] 引數
y, x | - | 浮點數或整數值 |
[編輯] 返回值
如果沒有錯誤發生,則返回 y / x 的反正切(arctan(y |
x |
如果發生域錯誤,則返回實現定義的值 (支援 NaN 時返回 NaN)。
如果因下溢發生範圍錯誤,則返回正確結果(舍入後)。
[編輯] 錯誤處理
錯誤按 math_errhandling 指定的方式報告。
如果 x 和 y 都為零,可能會發生定義域錯誤。
如果實現支援 IEEE 浮點運算 (IEC 60559),
- 如果 x 和 y 都為零,則*不會*發生定義域錯誤。
- 如果 x 和 y 都為零,也不會發生值域錯誤。
- 如果 y 為零,則不會發生極點錯誤。
- 如果 y 為 ±0 且 x 為負數或 -0,則返回 ±π。
- 如果 y 為 ±0 且 x 為正數或 +0,則返回 ±0。
- 如果 y 為 ±∞ 且 x 為有限值,則返回 ±π/2。
- 如果 y 為 ±∞ 且 x 為 -∞,則返回 ±3π/4。
- 如果 y 為 ±∞ 且 x 為 +∞,則返回 ±π/4。
- 如果 x 為 ±0 且 y 為負數,則返回 -π/2。
- 如果 x 為 ±0 且 y 為正數,則返回 +π/2。
- 如果 x 為 -∞ 且 y 為有限正數,則返回 +π。
- 如果 x 為 -∞ 且 y 為有限負數,則返回 -π。
- 如果 x 為 +∞ 且 y 為有限正數,則返回 +0。
- 如果 x 為 +∞ 且 y 為有限負數,則返回 -0。
- 如果 x 或 y 為 NaN,則返回 NaN。
[編輯] 注意
std::atan2(y, x) 等價於 std::arg(std::complex<std::common_type_t<decltype(x), decltype(y)>>(x, y))。
POSIX 指定,在下溢的情況下,返回 y / x 的值;如果不支援,則返回一個不大於 DBL_MIN、FLT_MIN 和 LDBL_MIN 的實現定義值。
不要求完全按照 (A) 提供額外的過載。它們只需要足以確保其第一個引數 num1 和第二個引數 num2
|
(直至 C++23) |
如果 num1 和 num2 具有算術型別,則 std::atan2(num1, num2) 的效果與 std::atan2(static_cast</*common-floating-point-type*/>(num1), 如果不存在具有最高等級和次等級的浮點型別,則過載決議不會從提供的過載中產生可用的候選函式。 |
(C++23 起) |
[編輯] 示例
#include <cmath> #include <iostream> void print_coordinates(int x, int y) { std::cout << std::showpos << "(x:" << x << ", y:" << y << ") cartesian is " << "(r:" << std::hypot(x, y) << ", phi:" << std::atan2(y, x) << ") polar\n"; } int main() { // normal usage: the signs of the two arguments determine the quadrant print_coordinates(+1, +1); // atan2( 1, 1) = +pi/4, Quad I print_coordinates(-1, +1); // atan2( 1, -1) = +3pi/4, Quad II print_coordinates(-1, -1); // atan2(-1, -1) = -3pi/4, Quad III print_coordinates(+1, -1); // atan2(-1, 1) = -pi/4, Quad IV // special values std::cout << std::noshowpos << "atan2(0, 0) = " << atan2(0, 0) << '\n' << "atan2(0,-0) = " << atan2(0, -0.0) << '\n' << "atan2(7, 0) = " << atan2(7, 0) << '\n' << "atan2(7,-0) = " << atan2(7, -0.0) << '\n'; }
輸出
(x:+1, y:+1) cartesian is (r:1.41421, phi:0.785398) polar (x:-1, y:+1) cartesian is (r:1.41421, phi:2.35619) polar (x:-1, y:-1) cartesian is (r:1.41421, phi:-2.35619) polar (x:+1, y:-1) cartesian is (r:1.41421, phi:-0.785398) polar atan2(0, 0) = 0 atan2(0,-0) = 3.14159 atan2(7, 0) = 1.5708 atan2(7,-0) = 1.5708
[編輯] 參閱
(C++11)(C++11) |
計算反正弦(arcsin(x)) (函式) |
(C++11)(C++11) |
計算反餘弦(arccos(x)) (函式) |
(C++11)(C++11) |
計算反正切(arctan(x)) (函式) |
返回相角 (函式模板) | |
將函式 std::atan2 應用於 valarray 和一個值 (函式模板) | |
C 文件 關於 atan2
|