atan2, atan2f, atan2l

< c‎ | numeric‎ | math
在标头 <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 类型,则调用 atan2l。否则,若任何实参拥有整数类型或 double 类型,则调用 atan2。否则调用 atan2f

仅当实现预定义了 __STDC_IEC_60559_DFP__(即实现支持十进制浮点数)时,声明函数 (4-6)

(C23 起)

参数

x, y - 浮点值

返回值

若不出现错误,则返回 y/x[-π ; +π] 弧度范围中的弧(反)正切(arctan(
y
x
))。
Y 实参
返回值
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
  • x 为 NaN 或 y 为 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)
{
    // 正常用法:以两个参数的符号确定象限
    // atan2(1,1) = +pi/4,第 I 象限
    printf("(+1,+1) cartesian is (%f,%f) polar\n", hypot( 1, 1), atan2( 1, 1));
    // atan2(1, -1) = +3pi/4,第 II 象限
    printf("(+1,-1) cartesian is (%f,%f) polar\n", hypot( 1,-1), atan2( 1,-1));
    // atan2(-1,-1) = -3pi/4,第 III 象限
    printf("(-1,-1) cartesian is (%f,%f) polar\n", hypot(-1,-1), atan2(-1,-1));
    // atan2(-1,-1) = -pi/4,第 IV 象限
    printf("(-1,+1) cartesian is (%f,%f) polar\n", hypot(-1, 1), atan2(-1, 1));
 
    // 特殊值
    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 The atan2 functions (第 TBD 页)
  • 7.25 Type-generic math <tgmath.h> (第 TBD 页)
  • F.10.1.4 The atan2 functions (第 TBD 页)
  • C17 标准(ISO/IEC 9899:2018):
  • 7.12.4.4 The atan2 functions (第 174 页)
  • 7.25 Type-generic math <tgmath.h> (第 272-273 页)
  • F.10.1.4 The atan2 functions (第 378 页)
  • C11 标准(ISO/IEC 9899:2011):
  • 7.12.4.4 The atan2 functions (第 239 页)
  • 7.25 Type-generic math <tgmath.h> (第 373-375 页)
  • F.10.1.4 The atan2 functions (第 519 页)
  • C99 标准(ISO/IEC 9899:1999):
  • 7.12.4.4 The atan2 functions (第 219 页)
  • 7.22 Type-generic math <tgmath.h> (第 335-337 页)
  • F.9.1.4 The atan2 functions (第 456 页)
  • C89/C90 标准(ISO/IEC 9899:1990):
  • 4.5.2.4 The atan2 function

参阅

(C99)(C99)
计算反正弦(arcsin(x)
(函数)
(C99)(C99)
计算反余弦(arccos(x)
(函数)
(C99)(C99)
计算反正切(arctan(x)
(函数)
(C99)(C99)(C99)
计算复数的辐角
(函数)