In this C++ tutorial, you will learn how to find the inverse tangent of a coordinate using atan2() function of cmath, with syntax and examples.
C++ atan2()
C++ atan2() returns inverse tangent of a coordinate in radians.
Syntax
The syntax of C++ atan2() is
atan2(x, y)
where
Parameter | Description |
---|---|
x | X-coordinate value. |
y | Y-coordinate value. |
Returns
The return value depends on the type of value passed for parameters x and y. The return value of atan2(x, y) is
- double if x and y are double.
- float if x and y are float.
- long double if x and y are long double.
The synopsis of atan2() function is
double atan2(double y, double x);
float atan2(float y, float x);
long double atan2(long double y, long double x);
double atan2(Type1 y, Type2 x); // for combinations of other numeric types
atan2() is a function of cmath library. Include cmath library in the program, if using atan2().
Example
In this example, we read x and y values from the user and find the inverse tangent of this coordinate (x, y) using atan2() function.
C++ Program
#include <iostream>
#include<cmath>
using namespace std;
int main() {
double x, y;
cout << "Enter X : ";
cin >> x;
cout << "Enter Y : ";
cin >> y;
double result = atan2(x, y);
cout << "atan2(" << x << ", " << y << ") : " << result << endl;
}
Output
Enter X : 5
Enter Y : 5
atan2(5, 5) : 0.785398
Program ended with exit code: 0
Enter X : 10
Enter Y : 14.56
atan2(10, 14.56) : 0.601821
Program ended with exit code: 0
Conclusion
In this C++ Tutorial, we learned the syntax of C++ atan2(), and how to use this function to find the inverse tangent of a coordinate, with the help of examples.