C Standard Library

C <math.h> - atan2() Function



The C <math.h> atan2() function returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). The return value will be in the range -𝜋 through 𝜋.

Syntax

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

Parameters

y Specify the ordinate coordinate.
x Specify the abscissa coordinate.

Return Value

Returns theta of the point (r, theta) in polar coordinates that corresponds to the point (x, y) in Cartesian coordinates.

Example:

In the example below, atan2() function is used to calculate the theta of a point.

#include <stdio.h>
#include <math.h>
 
int main (){
  printf("%lf\n", atan2(10, 10));
  printf("%lf\n", atan2(20, 10));
  printf("%lf\n", atan2(-20, 10)); 
  return 0;
}

The output of the above code will be:

0.785398
1.107149
-1.107149

❮ C <math.h> Library

5