命名空间
变体
操作

atan2, atan2f, atan2l

来自 cppreference.cn
< 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