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