C++ Math atan2
The function calculates the arctangent of a set of coordinates.
Suppose the coordinate is (x,y) :
Syntax
Suppose the coordinate is (x,y). Syntax would be:
float atan2(float y, float x);
double atan2(double y, double x);
long double atan2(long double y, long double x);
Promoted atan2(Arithmetic1 y, Arithmetic x );
Parameter
y : It represents the y-coordinate value.
x : It represents the x-coordinate value.
Return value
It provides the result within the range[-?, ?]. If both x and y have a value of zero, the function will return zero.
- In case any parameter is of integer data type, it will be converted to double.
- When any argument is of type long double, it will be converted to long double.
Example 1
Let's consider a basic scenario where both variables x and y are equal to zero.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x=0;
int y=0;
cout<<"Value of tan(y/x) is : "<<tan(y/x)<<'\n';
std::cout << "Value of tan-1(y/x) is : " <<atan2(y,x)<< std::endl;
return 0;
}
Output:
Value of tan(y/x) is : 0
Value of tan-1(y/x) is : 0
In this instance, the atan2 function computes the arctangent when 'x' and 'y' are both zero.
Example 2
Let's examine a straightforward scenario where both 'x' and 'y' have distinct data types.
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
int x=6;
float y=7.8;
cout<<"Value of tan(y/x) is : "<<tan(y/x)<<'\n';
std::cout << "Value of tan-1(y/x) is : " <<atan2(y,x)<< std::endl;
return 0;
}
Output:
Value of tan(y/x) is : 3.6021
Value of tan1(y/x) is : 0.915101
In this instance, the atan2 method calculates the arctangent based on an integer value for x and a floating-point value for y.