atan2() function in C++ STL - GeeksforGeeks

21 Jun, 2021
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
// CPP program to demonstrate the atan2()// function when both parameters are of// same type#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;}
atan2(y/x) = 0.785398 radians
atan2(y/x) = 45 degrees
 
Program 2:  
CPP
// CPP program to demonstrate the atan2()// function when both parameters are of// different types#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;}
atan2(y/x) = 2.35619 radians
atan2(y/x) = 135 degrees
 
Program 3:  
CPP
// CPP program to demonstrate the atan2()// function when y/x is undefined#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;}
atan2(y/x) = 1.5708 radians
atan2(y/x) = 90 degrees
 
Program 4:  
CPP
// CPP program to demonstrate the atan2()// function when both parameters are zero#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;}
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
// CPP program to demonstrate the atan2()// errors and exceptions#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);
 
simmytarika5
CPP-Functions
cpp-math
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects
Operator Overloading in C++
Socket Programming in C/C++
Bitwise Operators in C/C++
Multidimensional Arrays in C / C++
Virtual Function in C++
Constructors in C++
Templates in C++ with Examples