arc tangent function of two variables

You're viewing an older version of this page (#5701). View the current version.

View versions (5)

INTERFACE

#include <math.h>

double atan2 ( double y, double x )
long double atan2l ( long double y, long double x )
float atan2f ( float y, float x )

DESCRIPTION

The atan2 function computes the principal value of the arc tangent of y / x, using the signs of both arguments to determine the quadrant of the return value. It produces correct results even when the resulting angle is near $\pi/2$ or $-\pi/2$ (for x near 0).

Example 1
Workings

\code #include <stdio.h> #include <math.h> int main(void) { double result, x = 90.0, y = 15.0; result = atan2(y, x); printf("The arc tangent ratio of %lf is %lf
", (y/x), result); return 0; } \endcode

Solution

Output:

The arc tangent ratio of 0.166667 is 0.165149

SPECIAL VALUES

atan2 ( &plusmn;0, -0 ) returns &plusmn;$\pi$.

atan2 ( &plusmn;0, +0 ) returns &plusmn;0.

atan2 ( &plusmn;0, x ) returns &plusmn;$\pi$ for x < 0.

atan2 ( &plusmn;0, x ) returns &plusmn;0 for x > 0.

atan2 ( y, &plusmn;0 ) returns $\pi/2$ for y > 0.

atan2 ( &plusmn;y, -&infin; ) returns &plusmn;$\pi$ for finite y > 0.

atan2 ( &plusmn;y, +&infin; ) returns &plusmn;0 for finite y > 0.

atan2 ( &plusmn;&infin;, x ) returns &plusmn;$\pi/2$ for finite x

atan2 ( &plusmn;&infin;, -&infin; ) returns &plusmn;$3\pi/4$.

atan2 ( &plusmn;&infin;, +&infin; ) returns &plusmn;$\pi/4$.

NOTES

The atan2 function is used mostly to convert from rectangular (x,y) to polar (r, $\theta$) coordinates that must satisfy $x=r \cos(\theta)$ and $y=r \sin(\theta)$. In general, conversions to polar coordinates should be computed thus:

$$r := \sqrt{x^2 + y^2}$$
(1)
$$\theta := \mathrm{atan2}(y,x)$$
(2)

SEE ALSO

acos, asin, atan, cos, cosh, sin, sinh, tan, tanh

STANDARDS

The atan2 function conforms to ISO/IEC 9899:1999(E).