<cmath> (math.h)

function
<cmath> <ctgmath>

atan2

double atan2(double y, double x);
     double atan2  (double y     , double x);
      float atan2f (float y      , float x);
long double atan2l (long double y, long double x);
     double atan2 (double y     , double x);
      float atan2 (float y      , float x);
long double atan2 (long double y, long double x);
     double atan2 (double y     , double x);
      float atan2 (float y      , float x);
long double atan2 (long double y, long double x);
     double atan2 (Type1 y      , Type2 x);       // additional overloads
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 takes into account the sign of both arguments in order to determine the quadrant.

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

Header <tgmath.h> provides a type-generic macro version of this function.
This function is overloaded in <valarray> (see valarray atan2).
Additional overloads are provided in this header (<cmath>) for other combinations of arithmetic types (Type1 and Type2): These overloads effectively cast its arguments to double before calculations, except if at least one of the arguments is of type long double (in which case both are casted to long double instead).

This function is also overloaded in <valarray> (see valarray atan2).

Parameters

y
Value representing the proportion of the y-coordinate.
x
Value representing the proportion of the x-coordinate.
If both arguments passed are zero, a domain error occurs.

Return Value

Principal arc tangent of y/x, in the interval [-pi,+pi] radians.
One radian is equivalent to 180/PI degrees.

If a domain error occurs, the global variable errno is set to EDOM.
If a domain error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to EDOM.
- And math_errhandling has MATH_ERREXCEPT set: FE_INVALID is raised.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* atan2 example */
#include <stdio.h>      /* printf */
#include <math.h>       /* atan2 */

#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=%f, y=%f) is %f degrees\n", x, y, result );
  return 0;
}


Output:

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

See also