非常教程

C参考手册

数值 | Numerics

atan2f

在头文件<math.h>中定义

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 <tgmath.h>

#define atan2( arg )

(4)

(since C99)

1-3)y/x使用参数符号计算反正切以确定正确的象限。

4)类型 - 通用宏:如果参数具有类型long doubleatan2l则被调用。否则,如果参数具有整数类型或类型doubleatan2则调用该参数。否则,atan2f被调用。

参数

x,y

-

浮点值

返回值

如果没有错误发生,则y/x(arctan(

| y |

|:----|

| x |

))在-π范围内; +π弧度,返回。

Y参数

返回值

X参数

如果发生域错误,则返回实现定义的值。

如果由于下溢而发生范围错误,则返回正确的结果(舍入后)。

错误处理

按照math_errhandling中的指定报告错误。

如果xy都为零,则可能会出现域错误。

如果实现支持IEEE浮点运算(IEC 60559),

  • 如果xy都为零,则不会发生域错误
  • 如果xy都是零,则范围错误也不会发生
  • 如果y为零,则不会发生极点错误
  • 如果y±0x为负或-0±π返回
  • 如果y±0并且x是肯定的+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或yNaN,则返回NaN

笔记

atan2(y, x)相当于carg(x + I*y)

POSIX指定在发生下溢时y/x返回值,如果不支持,则返回不大于DBL_MIN,FLT_MIN和LDBL_MIN的实现定义值。

#include <stdio.h>
#include <math.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

参考

  • 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函数
C

C 语言是一门通用计算机编程语言,应用广泛。C 语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。