C++ Standard Library C++ STL Library

C++ <cmath> - atan2() Function



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

Syntax

double atan2 (double y, double x);
float atan2 (float y, float x);
long double atan2 (long double y, long double x);
double atan2 (double y, double x);
float atan2 (float y, float x);
long double atan2 (long double y, long double x);
//additional overloads
double atan2 (Type1 y, Type2 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 <iostream>
#include <cmath>
using namespace std;
 
int main () {
  cout<<atan2(10, 10)<<"\n";
  cout<<atan2(20, 10)<<"\n";
  cout<<atan2(-20, 10)<<"\n"; 
  return 0;
}

The output of the above code will be:

0.785398
1.10715
-1.10715

❮ C++ <cmath> Library