Namespaces
Variants

atan2, atan2f, atan2l

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <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 )。

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

(since C23)

目录

参数

x, y - 浮点数值

返回值

If no errors occur, the arc tangent of y / x ( arctan(
y
x
)
) in the range [-π ; +π] radians, is returned.
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)
{
    // 常规用法:两个参数的符号决定象限
    // atan2(1,1) = +pi/4,第一象限
    printf("(+1,+1) cartesian is (%f,%f) polar\n", hypot( 1, 1), atan2( 1, 1));
    // atan2(1, -1) = +3pi/4,第二象限
    printf("(+1,-1) cartesian is (%f,%f) polar\n", hypot( 1,-1), atan2( 1,-1));
    // atan2(-1,-1) = -3pi/4,第三象限
    printf("(-1,-1) cartesian is (%f,%f) polar\n", hypot(-1,-1), atan2(-1,-1));
    // atan2(-1,-1) = -pi/4,第四象限
    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 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函数(第219页)
  • 7.22 泛型数学 <tgmath.h>(第335-337页)
  • F.9.1.4 atan2函数(第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++ 文档 关于 atan2