# atan2() function in C++ STL

The atan2() is an inbuilt function in C++ STL which returns tangent inverse of (y/x), where y is the proportion of the y-coordinate and x is the proportion of the x-coordinate. The numeric value lies between – and representing the angle of a (x, y) point and positive x-axis. It is the counterclockwise angle, measured in radian, between the positive X-axis, and the point (x, y).

Syntax:

atan2(data_type y, data_type x)


Parameters:The function accepts two mandatory parameters which are decribed below:

• y – This value specifies y-coordinate.
• x – This value specifies the x-coordinate.

The parameters can be of double, float or long double datatype.

Return Value: The function returns a numeric value between – and representing the angle of a (x, y) point and positive x-axis. It is the counterclockwise angle, measured in radian, between the positive X-axis, and the point (x, y).

Below programs illustrate the atan2() function:

Program 1:

 // CPP program to demonstrate the atan2()  // function when both parameters are of  // same type  #include  using namespace std;     int main()  {      double x = 10.0, y = 10.0, result;      result = atan2(y, x);         cout << "atan2(y/x) = " << result           << " radians" << endl;      cout << "atan2(y/x) = " << result * 180 / 3.141592           << " degrees" << endl;         return 0;  }

Output:

atan2(y/x) = 0.785398 radians
atan2(y/x) = 45 degrees


Program 2:

 // CPP program to demonstrate the atan2()  // function when both parameters are of  // different types  #include  using namespace std;     int main()  {      double result;      float x = -10.0;      int y = 10;      result = atan2(y, x);         cout << "atan2(y/x) = " << result           << " radians" << endl;      cout << "atan2(y/x) = " << result * 180 / 3.141592 << " degrees" << endl;         return 0;  }

Output:

atan2(y/x) = 2.35619 radians
atan2(y/x) = 135 degrees


Program 3:

 // CPP program to demonstrate the atan2()  // function when y/x is undefined  #include  using namespace std;     int main()  {      double x = 0.0, y = 10.0, result;      result = atan2(y, x);         cout << "atan2(y/x) = " << result           << " radians" << endl;      cout << "atan2(y/x) = " << result * 180 / 3.141592           << " degrees" << endl;         return 0;  }

Output:

atan2(y/x) = 1.5708 radians
atan2(y/x) = 90 degrees


Program 4:

 // CPP program to demonstrate the atan2()  // function when both parameters are zero  #include  using namespace std;     int main()  {      double x = 0.0, y = 0.0, result;      result = atan2(y, x);         cout << "atan2(y/x) = " << result           << " radians" << endl;      cout << "atan2(y/x) = " << result * 180 / 3.141592           << " degrees" << endl;         return 0;  }

Output:

atan2(y/x) = 0 radians
atan2(y/x) = 0 degrees


Errors and Exceptions: The function returns no matching function for call to error when a string or character is passed as an argument.
Program 5:

 // CPP program to demonstrate the atan2()  // errors and exceptions  #include  using namespace std;     int main()  {      double x = 0.0, y = 10.0, result;      result = atan2("1", x);         cout << "atan2(y/x) = " << result << " radians" << endl;         cout << "atan2(y/x) = " << result * 180 / 3.141592           << " degrees" << endl;         return 0;  }

Output:

prog.cpp:9:26: error: no matching function for call to 'atan2(const char , double&)'
result = atan2("1", x);

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.