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 described 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
#include<bits/stdc++.h>
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
#include <bits/stdc++.h>
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
#include<bits/stdc++.h>
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
#include<bits/stdc++.h>
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
#include<bits/stdc++.h>
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 [2], double&)'
result = atan2("1", x);
Last Updated :
21 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...