atan2, atan2f, atan2l
來自 cppreference.com
定義於標頭檔案 <math.h> |
||
float atan2f( float y, float x ); |
(1) | (C99 起) |
double atan2( double y, double x ); |
(2) | |
long double atan2l( long double y, long double x ); |
(3) | (C99 起) |
_Decimal32 atan2d32( _Decimal32 y, _Decimal32 x ); |
(4) | (自 C23 起) |
_Decimal64 atan2d64( _Decimal64 y, _Decimal64 x ); |
(5) | (自 C23 起) |
_Decimal128 atan2d128( _Decimal128 y, _Decimal128 x ); |
(6) | (自 C23 起) |
定義於標頭檔案 <tgmath.h> |
||
#define atan2( y, x ) |
(7) | (C99 起) |
1-6) 計算 y / x 的反正切,使用引數的符號來確定正確的象限。
7) 泛型宏:如果任何引數的型別為 long double,則呼叫 (3) (
atan2l
)。否則,如果任何引數具有整數型別或型別為 double,則呼叫 (2) (atan2
)。否則,呼叫 (1) (atan2f
)。
函式 (4-6) 僅當實現預定義 |
(自 C23 起) |
目錄 |
[編輯] 引數
x, y | - | 浮點值 |
[編輯] 返回值
如果沒有發生錯誤,則返回 y / x 的反正切 (arctan(y |
x |
若發生定義域錯誤,返回實現定義的值。
如果因下溢發生範圍錯誤,則返回正確結果(舍入後)。
[編輯] 錯誤處理
錯誤按 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。
[編輯] 註記
atan2(y, x) 等價於 carg(x + I*y)。
POSIX 指定,在下溢的情況下,返回 y / x 的值;如果不支援,則返回一個不大於 DBL_MIN、FLT_MIN 和 LDBL_MIN 的實現定義值。
[編輯] 示例
執行此程式碼
#include <math.h> #include <stdio.h> int main(void) { // normal usage: the signs of the two arguments determine the quadrant // atan2(1,1) = +pi/4, Quad I printf("(+1,+1) cartesian is (%f,%f) polar\n", hypot( 1, 1), atan2( 1, 1)); // atan2(1, -1) = +3pi/4, Quad II printf("(+1,-1) cartesian is (%f,%f) polar\n", hypot( 1,-1), atan2( 1,-1)); // atan2(-1,-1) = -3pi/4, Quad III printf("(-1,-1) cartesian is (%f,%f) polar\n", hypot(-1,-1), atan2(-1,-1)); // atan2(-1,-1) = -pi/4, Quad IV printf("(-1,+1) cartesian is (%f,%f) polar\n", hypot(-1, 1), atan2(-1, 1)); // special values printf("atan2(0, 0) = %f atan2(0, -0)=%f\n", atan2(0,0), atan2(0,-0.0)); printf("atan2(7, 0) = %f atan2(7, -0)=%f\n", atan2(7,0), atan2(7,-0.0)); }
輸出
(+1,+1) cartesian is (1.414214,0.785398) polar (+1,-1) cartesian is (1.414214,2.356194) polar (-1,-1) cartesian is (1.414214,-2.356194) polar (-1,+1) cartesian is (1.414214,-0.785398) polar atan2(0, 0) = 0.000000 atan2(0, -0)=3.141593 atan2(7, 0) = 1.570796 atan2(7, -0)=1.570796
[編輯] 參考
- C23 標準 (ISO/IEC 9899:2024)
- 7.12.4.4 atan2 函式(p: TBD)
- 7.25 型別通用數學 <tgmath.h> (p: TBD)
- F.10.1.4 atan2 函式(p: TBD)
- C17 標準 (ISO/IEC 9899:2018)
- 7.12.4.4 atan2 函式(p: 174)
- 7.25 型別通用數學 <tgmath.h> (p: 272-273)
- F.10.1.4 atan2 函式(p: 378)
- C11 標準 (ISO/IEC 9899:2011)
- 7.12.4.4 atan2 函式(p: 239)
- 7.25 型別通用數學 <tgmath.h> (p: 373-375)
- F.10.1.4 atan2 函式(p: 519)
- C99 標準 (ISO/IEC 9899:1999)
- 7.12.4.4 atan2 函式(p: 219)
- 7.22 型別通用數學 <tgmath.h> (p: 335-337)
- F.9.1.4 atan2 函式(p: 456)
- C89/C90 標準 (ISO/IEC 9899:1990)
- 4.5.2.4 atan2 函式
[編輯] 另請參閱
(C99)(C99) |
計算反正弦 (arcsin(x)) (函式) |
(C99)(C99) |
計算反餘弦 (arccos(x)) (函式) |
(C99)(C99) |
計算反正切 (arctan(x)) (函式) |
(C99)(C99)(C99) |
計算複數的幅角 (函式) |
C++ 文件 for atan2
|