# atan2() function in C++ STL

C++ProgrammingServer Side Programming

The atan2() function returns the tangent inverse of the coordinate in terms of y and x. Here y and x are the values of the y and x coordinates respectively. It is an inbuilt function in C++ STL.

The syntax of the atan2() function is given as follows.

atan2(dataType var1, dataType var2)

As can be seen from the syntax, the function atan2() accepts two parameters var1 and var2 of data type float, double or long double that are y and x point respectively.

The value returned by atan2() is in the range of -pi to pi and is the angle between the (x,y) and the positive x axis.

A program that demonstrates atan2() in C++ is given as follows.

## Example

Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double y = 10, x = 5, ans;
ans = atan2(y,x);
cout << "atan2("<< y <<"/"<< x <<") = " << ans << endl;
return 0;
}

## output

atan2(10/5) = 1.10715

In the above program, first the variables y and x are initialized. Then inverse tangent of y and x is found using atan2() and stored in ans. Finally the value of ans is displayed. This is demonstrated by the following code snippet.

double y = 10, x = 5, ans;
ans = atan2(y,x);
cout << "atan2("<< y <<"/"<< x <<") = " << ans << endl;

The result obtained by using the atan2() function can be converted into degrees and displayed. A program to demonstrate this is as follows.

## Example

Live Demo

#include <iostream>
#include <cmath>
using namespace std;
int main() {
double y = 10, x = 5, ans;
ans = atan2(y,x);
ans = ans*180/3.14159;
cout << "atan2("<< y <<"/"<< x <<") = " << ans << endl;
return 0;
}

## Output

atan2(10/5) = 63.435

In the above program, the inverse tangent of y and x is obtained using atan2(). Then this value is converted into degrees. Finally, the output is displayed. This is demonstrated by the following code snippet.

double y = 10, x = 5, ans;
ans = atan2(y,x);
ans = ans*180/3.14159;
cout << "atan2("<< y <<"/"<< x <<") = " << ans << endl;