Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
C Library
cassert (assert.h)
cctype (ctype.h)
cerrno (errno.h)
cfloat (float.h)
ciso646 (iso646.h)
climits (limits.h)
clocale (locale.h)
cmath (math.h)
csetjmp (setjmp.h)
csignal (signal.h)
cstdarg (stdarg.h)
cstddef (stddef.h)
cstdio (stdio.h)
cstdlib (stdlib.h)
cstring (string.h)
ctime (time.h)
cmath (math.h)
functions:
abs
acos
asin
atan
atan2
ceil
cos
cosh
exp
fabs
floor
fmod
frexp
ldexp
log
log10
modf
pow
sin
sinh
sqrt
tan
tanh
macro constants:
HUGE_VAL


atan2

function
<cmath>
     double atan2 (      double y,      double x );
long double atan2 ( long double y, long double x );
      float atan2 (       float y,       float x );

Compute arc tangent with two parameters

Returns the principal value of the arc tangent of y/x, expressed in radians.

To compute the value, the function uses the sign of both arguments to determine the quadrant.

In C++, this function is overloaded in <valarray> (see valarray atan2).

Parameters

y
Floating point value representing an y-coordinate.
x
Floating point value representing an x-coordinate.
If both arguments passed are zero, a domain error occurs, which sets the global variable ERRNO to the EDOM value.

Return Value

Principal arc tangent of y/x, in the interval [-pi,+pi] radians.

Portability

In C, only the double version of this function exists with this name.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* atan2 example */
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
int main ()
{
  double x, y, result;
  x = -10.0;
  y = 10.0;
  result = atan2 (y,x) * 180 / PI;
  printf ("The arc tangent for (x=%lf, y=%lf) is %lf degrees\n", x, y, result );
  return 0;
}


Output:

The arc tangent for (x=-10.000000, y=10.000000) is 135.000000 degrees.

See also