Name

atan2

Synopsis

Calculates the inverse tangent of a quotient

#include <math.h>
doubleatan2( double y, double x );
float atan2f( float y, float x );        (C99)
long double atan2l( long double y, long double x );        (C99)

The atan2() function divides the first argument by the second and returns the arc tangent of the result, or arctan(y/x).

The return value is given in radians, and is in the range -π ≤ atan2(y,x) ≤ π. The signs of x and y determine the quadrant of the result:

x > 0, y > 0:

0 ≤ atan2( y,x) ≤ π/2

x < 0, y > 0:

π/2 ≤ atan2( y,x) ≤ π

x < 0, y < 0:

-π ≤ atan2( y,x) ≤ -π/2

x > 0, y < 0:

-π/2 ≤ atan2( y,x) ≤ 0

If both arguments are zero, then the function may incur a domain error.

Example

/*
 * Calculate an acute angle of a right triangle, given
 * the adjacent and opposite sides.
 */
#define PI 3.141593
#define DEG_PER_RAD (180.0/PI)

double adjacent = 3.0;
double opposite = 4.0;
double angle =atan2( opposite, adjacent ) * DEG_PER_RAD;

printf( "The acute angles of a 3-4-5 right triangle are %4.2f\xB0 "
        "and %4.2f\xB0.\n", angle, 90.0 - angle );

This code produces the following output:

The acute angles of a 3-4-5 right triangle are 53.13° and 36.87°.

See Also

The arc tangent function for a single argument: atan()

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.