我要评分
获取效率
正确性
完整性
易理解

atan2

Compute the arc tangent of y/x.

Interface Definition

C interface:

float atan2f(float y, float x);

double atan2(double y, double x);

Fortran interface:

RES = ATAN2F(Y,X);

RES = ATAN2(Y,X);

Parameters

Parameter

Type

Description

Input/Output

y

  • For atan2f, y is of single-precision floating-point type.
  • For atan2, y is of double-precision floating-point type.

Floating point value of a Y-axis coordinate

Input

x

  • For atan2f, x is of single-precision floating-point type.
  • For atan2, x is of double-precision floating-point type.

Floating point value of an X-axis coordinate

Input

Return Value

An azimuth from the origin to the point (x, y) is returned for atan2(y, x), that is, the included angle with reference to the x-axis. The unit of the return value is radian, and the value range is (-π, +π].

  • If y is ±0 and x is negative or -0, the return value is ±π.
  • If y is ±0 and x is positive or +0, the return value is ±0.
  • If y is ±∞ and x is a finite number, the return value is ±π/2.
  • If y is ±∞ and x is –∞, the return value is ±3π/4.
  • If y is ±∞ and x is +∞, the return value is ±π/4.
  • If x is ±0 and y is negative, the return value is -π/2.
  • If x is ±0 and y is positive, the return value is +π/2.
  • If x is -∞ and y is a finite positive number, the return value is +π.
  • If x is -∞ and y is a finite negative number, the return value is -π.
  • If x is +∞ and y is a finite positive number, the return value is +0.
  • If x is +∞ and y is a finite negative number, the return value is -0.
  • If x or y is NaN, the return value is NaN.

Dependencies

C: "km.h"

Fortran: "km.f03"

Examples

C interface:

    double pi = acos(-1); 
    // typical usage 
    double x1 = 1.0, y1 = pi/2, x2 = -3*pi/4, y2 = 2.0; 
    double x3 = 4.0, y3 = -4.0, x4 = -3.0, y4 = 3.0; 
    // special handling 
    double a = 0.0, b = INFINITY, c = -INFINITY, d = NAN; 
    // print result 
    printf("atan2(pi/2, 1.0) = %.15f\n", atan2(y1, x1)); 
    printf("atan2(2.0, -3*pi/4) = %.15f\n", atan2(y2, x2)); 
    printf("atan2(-4.0, 4.0) = %.15f\n", atan2(y3, x3)); 
    printf("atan2(3.0, -3.0) = %.15f\n", atan2(y4, x4)); 
    printf("atan2(+0.0, +0.0) = %+.15f\n", atan2(a, a)); 
    printf("atan2(+0.0, -0.0) = %+.15f\n", atan2(a, -a)); 
    printf("atan2(-0.0, +0.0) = %+.15f\n", atan2(-a, a)); 
    printf("atan2(-0.0, -0.0) = %+.15f\n", atan2(-a, -a)); 
    printf("atan2(+0.0, -4.0) = %+.15f\n", atan2(a, y3)); 
    printf("atan2(-0.0, +3.0) = %+.15f\n", atan2(-a, y4)); 
    printf("atan2(-4.0, +0.0) = %+.15f\n", atan2(y3, a)); 
    printf("atan2(+3.0, -0.0) = %+.15f\n", atan2(y4, -a)); 
    printf("atan2(INFINITY, 1.0) = %+.15f\n", atan2(b, x1)); 
    printf("atan2(-INFINITY, -1.0) = %+.15f\n", atan2(c, -x1)); 
    printf("atan2(1.0, -INFINITY) = %+.15f\n", atan2(x1, c)); 
    printf("atan2(-3.0, -INFINITY) = %+.15f\n", atan2(x4, c)); 
    printf("atan2(1.0, INFINITY) = %+.15f\n", atan2(x1, b)); 
    printf("atan2(-3.0, INFINITY) = %+.15f\n", atan2(x4, b)); 
    printf("atan2(1.0, NAN) = %+.15f\n", atan2(x1, d)); 
    printf("atan2(NAN, -1.0) = %+.15f\n", atan2(d, -x1)); 
    /* 
     *  atan2(pi/2, 1.0) = 1.003884821853887 
     *  atan2(2.0, -3*pi/4) = 2.437780340738381 
     *  atan2(-4.0, 4.0) = -0.785398163397448 
     *  atan2(3.0, -3.0) = 2.356194490192345 
     *  atan2(+0.0, +0.0) = +1.570796326794897 
     *  atan2(+0.0, -0.0) = +1.570796326794897 
     *  atan2(-0.0, +0.0) = +1.570796326794897 
     *  atan2(-0.0, -0.0) = +1.570796326794897 
     *  atan2(+0.0, -4.0) = +3.141592653589793 
     *  atan2(-0.0, +3.0) = +0.000000000000000 
     *  atan2(-4.0, +0.0) = -1.570796326794897 
     *  atan2(+3.0, -0.0) = +1.570796326794897 
     *  atan2(INFINITY, 1.0) = +1.570796326794897 
     *  atan2(INFINITY, -1.0) = -1.570796326794897 
     *  atan2(1.0, -INFINITY) = +3.141592653589793 
     *  atan2(-3.0, -INFINITY) = -3.141592653589793 
     *  atan2(1.0, INFINITY) = +0.000000000000000 
     *  atan2(-3.0, INFINITY) = -0.000000000000000 
     *  atan2(1.0, NAN) = +nan 
     *  atan2(NAN, -1.0) = +nan 
     * 
     * */

Fortran interface:

    REAL(8) :: X = 3.0 
    REAL(8) :: Y = -3.0 
    PRINT*,  ATAN2(Y, X) 
    ! 
    ! OUTPUT 
    !     2.356194490192345 
    !