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 –\pi and \pi representing the angle \theta 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 –\pi and \pi representing the angle \theta 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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

Program 4:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

pawanasipugmailcom

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.