## Syntax

**# 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 <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.

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.

$ ./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.

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.

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.

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.

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.

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.

## Conclusion

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.