C++ atan2

The atan2() is a built-in function of the C++ programming language. This is used to calculate the tangent inverse of two variables in the representation of (y/x), here y leads to the y-axis, y coordinate, whereas x leads to the x-axis, x coordinate. The resultant numeric value lies between –pi and pi which represents the angle in degrees of both x, and y points and the positive x-axis. Pi is the constant that is present in the library cmath header file. The name of the constant in which it is present is M_PI. This article will contain the functionality of the atan2() function.


# Atan2 (double y, double x);

Parameters of the Function

X: It is the floating-point number that shows the value with the proportion of x-coordinate.

Y: It is the floating-point number that shows the value with the proportion of y-coordinate.

This function returns the floating-point value that lies in the range of negative pi to a positive pi value. ‘0’ is obtained when both the x and y are zero values.

Difference Between atan() and atan2()

Both these functions calculate the arctangent of x coordinate and y/x. But the basic difference in both of them is that the atan() function returns a value that lies in the range of –pi/2 and pi/s radians. Whereas the atan2() function returns the resultant value that lies in the range of –pi to positive pi radians.

Implementation of atan2()

Example 1

To demonstrate the working of the Atan2 built-in function of C++, we include a library in the header file that makes us able to calculate the tangent inverse of two variables. This library is a ‘cmath’ header file. As we have learned the sin, cos, tangent values in mathematics, the library is of mathematics. The other file that must be included in the iostream enables the input and output streams to make cin and cout in the working condition.

#include <iostream>
#include <cmath>

Inside the main program, a simple code for returning the tangent inverse value. Two integer type values are taken as a parameter for the function.

Atan2(6.0 , 3.0);

Now, save the code and then compile it with the help of the C++ compiler that is G++. ‘atan. c’ is the name of the file.

$ g++ -o atan atan.c
$ ./atan

The resultant value contains the tangent inverse of the two values given by the variables.

Example 2

In this example, we will get the tangent inverse of the two different values. As in the previous code, we have taken both the values as a positive integer. But now, we will take one positive value and the second one is a negative value. Another variable is introduced here to store the results of the atan2() function. Here, we will use both the variables as the parameter of the function, instead of directly passing the values.

Atan2(y, x);

After the conversion of tangent inverse, we will get the result in the form of degrees as well. The conversion through the atan2 function is also known to be the radians variable. For this, the process of calculation is quite different.

The resultant value obtained from the atan2 function is multiplied by the resultant value obtained from, dividing 180 by the value of pi, which is equivalent to 3.14 value. This is the formula for the calculation of results in degrees. But you first need to calculate the value from the atan2 function. The variable for the degree calculation is also a double data type value.

Double degree = result * (180 / 3.14159);

So the resultant value will contain two values of two variables one is radians and the other is the degree. Now, execute the above source code in the Ubuntu terminal. You can see that two values are obtained. The radian is a negative value, as one of the input values in the variable was negative.

Example 3

Till now both the examples contain the variables of positive and negative values but they were of the same data type. This example will contain the value of different data types. One variable is of float and the second one is of integer data type.

After allocation of values, now we will calculate the atan2 function value, and the resultant will n=be stored in the ‘result’ variable. To display the results in degrees, use the same calculation formula. As we know that to calculate degree, 180 degrees is divided by the value of pi. Here, instead of giving the value of pi directly, we can use the PI itself in the formula.

Results * (180 / PI);

On compilation, you will see the results.

One of the input values was a negative float, but while applying the atan2 function, the value of the resultant is positive.

Example 4

This example leads to the use of a different library that contains bits and stdc++.h in a single header file. This allows the same functionality as the iostream or cmath do.

#include <bits/stdc++.h>

This is the program to demonstrate the working of atan2() when the variables y and x are undefined. Both values are taken as zero here.

So on these undefined values, when we apply the radians value and similarly the degrees, the answer will be undefined in both cases.

On execution, you can see that both the values are zero.

Example 5

This example deals with the exceptions and error occurrence while using the atan2 function and the degree calculation. This time, the function will return no matching function for a call. This happens when a string or a character is passed as an argument from the function.

Both the double data type values contain float values. One variable contains zero while the second one has 10.0 in it. When we call this built-in function, instead of both the values, one value, and one variable is passed directly.

Result = atan2("1" , x);

This ‘1’ is the random value just to check the result of having this step. The calculation for a degree is also applied to the resultant value. Compile the code and then you will see that an error will occur that will declare that there is no matching function for a call.

In the above case, the variable x was used; you may change the variable to see the difference in the results of error occurrence. Instead of x, we have used ‘y’ here.

Now, execute the code by following the change in the function call. You will see that there will be no change in the results. The same error will be displayed.

Hence, it is proved that we need to have some arguments in the function call.


The tutorial ‘C++ atan2()’ is a built-in function, it returns the inverse tangent of both variables (y/x). Its functionality contains function calls only along with two variables. These variables may be of the same data type or different. Along with the radians, the degree is also calculated. We have included some basic examples to demonstrate the working of the atan2() function in each aspect.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.