名稱空間
變體
操作

atan2, atan2f, atan2l

來自 cppreference.com
< c‎ | 數值‎ | 數學
 
 
 
常用數學函式
函式
基本操作
(C99)
(C99)
(C99)
(C99)(C99)(C99)(C23)
最大值/最小值操作
(C99)
(C99)
指數函式
(C23)
(C99)
(C99)
(C23)
(C23)

(C99)
(C99)(C23)
(C23)
(C23)
冪函式
(C99)
(C23)
(C23)

(C99)
(C23)
(C23)
三角函式和雙曲函式
atan2
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
(C99)
(C99)
(C99)
最近整數浮點數
(C99)(C99)(C99)
(C99)

(C99)(C99)(C99)
(C23)(C23)(C23)(C23)
浮點數操作
(C99)(C99)
(C99)(C23)
(C99)
窄化操作
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
量子與量子指數
十進位制重新編碼函式
總序和載荷函式
分類
(C99)
(C99)
(C99)
(C23)
誤差函式和伽馬函式
(C99)
(C99)
(C99)
(C99)
型別
宏常量
特殊浮點值
(C99)(C23)
引數和返回值
錯誤處理
快速操作指示符
 
定義於標頭檔案 <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) 僅當實現預定義 __STDC_IEC_60559_DFP__(即實現支援十進位制浮點數)時才宣告。

(自 C23 起)

目錄

[編輯] 引數

x, y - 浮點值

[編輯] 返回值

如果沒有發生錯誤,則返回 y / x 的反正切 (arctan(
y
x
)
),範圍為 [-π ; +π] 弧度。
Y 引數
返回值
math-atan2.png
X 引數

若發生定義域錯誤,返回實現定義的值。

如果因下溢發生範圍錯誤,則返回正確結果(舍入後)。

[編輯] 錯誤處理

錯誤按 math_errhandling 中指定的方式報告。

如果 xy 都為零,則可能發生域錯誤。

如果實現支援 IEEE 浮點算術 (IEC 60559)

  • 如果 xy 都為零,則不會發生域錯誤;
  • 如果 xy 都為零,則也不會發生範圍錯誤;
  • 如果 y 為零,則不會發生極點錯誤;
  • 如果 y±0x 為負數或 -0,則返回 ±π
  • 如果 y±0x 為正數或 +0,則返回 ±0
  • 如果 y±∞x 為有限值,則返回 ±π/2
  • 如果 y±∞x-∞,則返回 ±3π/4
  • 如果 y±∞x+∞,則返回 ±π/4
  • 如果 x±0y 為負數,則返回 -π/2
  • 如果 x±0y 為正數,則返回 +π/2
  • 如果 x-∞y 為有限正數,則返回
  • 如果 x-∞y 為有限負數,則返回
  • 如果 x+∞y 為有限正數,則返回 +0
  • 如果 x+∞y 為有限負數,則返回 -0
  • 如果 xy 為 NaN,則返回 NaN。

[編輯] 註記

atan2(y, x) 等價於 carg(x + I*y)

POSIX 指定,在下溢的情況下,返回 y / x 的值;如果不支援,則返回一個不大於 DBL_MINFLT_MINLDBL_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