函式
<cmath> <ctgmath>

atan2

double atan2(double y, double x);
     double atan2  (double y     , double x);      float atan2f (float y      , float x);long double atan2l (long double y, long double x);
     double atan2 (double y     , double x);      float atan2 (float y      , float x);long double atan2 (long double y, long double x);
     double atan2 (double y     , double x);      float atan2 (float y      , float x);long double atan2 (long double y, long double x);     double atan2 (Type1 y      , Type2 x);       // additional overloads
計算帶兩個引數的反正切
返回 y/x 的反正切的主值,以弧度表示。

為了計算該值,函式會考慮兩個引數的符號,以確定象限。

在C++中,此函式在 <valarray> 中過載(請參閱 valarray atan2)。

標頭檔案 <tgmath.h> 提供了此函式的型別通用宏版本。
此函式在 <valarray> 中過載(請參閱 valarray atan2)。
此標頭(<cmath>)中為其他 算術型別Type1Type2)的組合提供了額外的過載:這些過載實際上會在計算前將引數轉換為 double,除非至少有一個引數的型別為 long double(在這種情況下,兩者都會被轉換為 long double)。

此函式也在 <valarray> 中過載(請參閱 valarray atan2)。

引數

y
表示 y 座標比例的值。
x
表示 x 座標比例的值。
如果傳入的兩個引數都為零,則會發生定義域錯誤

返回值

y/x 的主反正切,在 [-pi,+pi] 弧度區間內。
弧度等於 180/PI

如果發生定義域錯誤,全域性變數 errno 會被設定為 EDOM
如果發生定義域錯誤
- 並且 math_errhandling 設定了 MATH_ERRNO:全域性變數 errno 會被設定為 EDOM
- 並且 math_errhandling 設定了 MATH_ERREXCEPT:將引發 FE_INVALID

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* atan2 example */
#include <stdio.h>      /* printf */
#include <math.h>       /* atan2 */

#define PI 3.14159265

int main ()
{
  double x, y, result;
  x = -10.0;
  y = 10.0;
  result = atan2 (y,x) * 180 / PI;
  printf ("The arc tangent for (x=%f, y=%f) is %f degrees\n", x, y, result );
  return 0;
}

輸出

The arc tangent for (x=-10.000000, y=10.000000) is 135.000000 degrees.


另見