C atan2()

C atan2() Prototype

double atan2(double y, double x);

Function atan2() takes two arguments: x-coordinate and y-coordinate, and calculate the angle in radians for the quadrant.

For better understanding of atan2():

[Mathematics] tan-1(y/x) = atan2(y,x) [In C programming] 

Two other functions atan2f() and atan2l() are also present in C to specifically work with float and long double respectively.

The atan2() function is defined in <math.h> header file.


C atan2() range

The arguments of atan2() can be any number, either positive or negative.


Example: C atan2() function

#include <stdio.h>
#include <math.h>
#define PI 3.141592654

int main()
{
  double x, y, result;
  y = 2.53;
  x = -10.2;
  result = atan2(y, x);
  result = result * 180.0/PI;

  printf("Tangent inverse for(x = %.1lf, y = %.1lf) is %.1lf degrees.", x, y, result);
  return 0;
}

Output

Tangent inverse for(x = -10.2, y = 2.53) is 166.1 degrees.

Caution while using atan2()

The value of second argument passed should not be 0. If the second argument passed is 0, the program will not run correctly.

Did you find this article helpful?