atan2
Compute the radian corresponding to the arctangent of y/x.
Interface Definition
C interface:
float atan2f(float y, float x);
double atan2(double y, double x);
long double atan2l(long double y, long double x);
Fortran interface:
RES = ATAN2F(Y,X);
RES = ATAN2(Y,X);
Parameters
Parameter |
Type |
Description |
Input/Output |
|---|---|---|---|
y |
|
Floating-point value of a y-axis coordinate. |
Input |
x |
|
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.
Dependency
C: "km.h"
Examples
C interface:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | 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
!
Parent topic: Trigonometric Functions