在头文件中定义float atan2f( float y, float x );(1)(since C99)
double atan2( double y, double x );(2)
long double atan2l( long double y, long double x );(3)(since C99)
Defined in header
#define atan2( arg )(4)(since C99)
1-3)y/x使用参数符号计算反正切以确定正确的象限。
4)类型 - 通用宏:如果参数具有类型long double,atan2l则被调用。否则,如果参数具有整数类型或类型double,atan2则调用该参数。否则,atan2f被调用。
参数
x,y-浮点值
返回值
如果没有错误发生,则y/x(arctan(
| y |
|:----|
| x |
))在-π范围内; +π弧度,返回。
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是NaN或yNaN,则返回NaN
笔记
atan2(y, x)相当于carg(x + I*y)。
POSIX指定在发生下溢时y/x返回值,如果不支持,则返回不大于DBL_MIN,FLT_MIN和LDBL_MIN的实现定义值。
例
#include #include
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) polaratan2(0, 0) = 0.000000 atan2(0, -0)=3.141593atan2(7, 0) = 1.570796 atan2(7, -0)=1.570796
参考
C11标准(ISO / IEC 9899:2011):7.12.4.4 atan2函数(p:239)
7.25类型通用数学(p:373-375)
F.10.1.4 atan2函数(p:519)
C99标准(ISO / IEC 9899:1999):7.12.4.4 atan2函数(p:219)
7.22类型通用数学(p:335-337)
F.9.1.4 atan2函数(p:456)
C89 / C90标准(ISO / IEC 9899:1990):4.5.2.4 atan2函数