C++ cmath atan2,atan2f,atan2l function

The C++ cmath atan2,atan2f and atan2l function compute the arc tangent of value obtained by diving the first argument with the second argument.The declaration of the functions are given below.

1 float atan2(float y, float x);
2 double atan2(double y, double x);
3 long double atan2(long double y, long double x);
4 float atan2f(float y, float x);
5 long double atan2l(long double y, long double x);

y -The first argument of floating point type.

x -The second argument of floating point type.

Return type
floating point value -The arc tan of y/x in the interval [-π,π] radians.

Some points to note:

i)atan2(x) is same as tan2-1(x).

ii)The sign of the arguments is necessary to determine the sign of the resultant value.

iii)If both the arguments are 0 domain error may occur.Note it may,not compulsory.

iv)If the arguments is integer or character literal the double version is called.

Code example

cout<<“Passing two floating point value\n”;
cout<< atan2( 12 , 34) << endl;

cout<<“Passing denominator as 0\n”;
cout<< atan2( 45 , 0) << endl;

cout<<“Passing numerator as 0\n”;
cout<< atan2( 0 , 34) << endl;

cout<<“Passing both arguments as 0\n”;
cout<< atan2( 0 , 0) << endl;

cout<<“Passing one or both arguments as -ve value\n”;
cout<< “atan2( -45/-10 )=” << atan2( -45 , -10) << endl
<< “atan2( 45/-10 )=” << atan2( 45 , -10) << endl
<< “atan2( -45/10 )=” << atan2( -45 , 10) << endl


Passing two floating point value
Passing denominator as 0
Passing numerator as 0
Passing both arguments as 0
Passing one or both arguments as -ve value
atan2( -45/-10 )= -1.78947
atan2( 45/-10 )= 1.78947
atan2( -45/10 )= -1.35213

You can see that the sign of the arguments affect the resultant value of the atan2 computation.


i)The 4th version tan2f is same as the 1st version float tan2(float,float).The ‘f‘ character stands for ‘float’ which signify the argument and return type of the function.

ii)The 5th version tan2l is same as the 3rd version long double tan2(long double,long double).The ‘l‘ character stands for ‘long double’ which signify the argument and return type of the function.

Converting atan2 radian to degree

The atan2 computed value is in radian,we can convert it to angular(degree) type by applying simple conversion rule.

The relation between degree and radian is given below.

1 radian = (180/3.141592) degree

So multiplying the resultant value with 180/3.141592 will convert the radian to degree.

Code example

cout<< atan2(23 , 9)*180/3.141592 << “°” ;