C programming math.h atan2,atan2f and atan2l


In C programming the <math.h> atan2,atan2f and atan2l 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);

Parameters:
y -The numerator value.

-The denominator value.

Return type
floating point value -The arc tangent of the ‘y/x’.

All the functions are the same,they compute the arc tangent of ‘y/x’.The 1st,2nd and the 3rd function have the same name i.e. ‘atan2’.The 4th and 5th function have different names.What does having a different names mean is explained in detail below.

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 and not compulsory.

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

Code example

double yd =12 , xd=11 , y1=1 , x1=0 ;

float yf =12 , xf=11 ;

long double yl=12 , xl=11 ;

printf(“%lf”, atan2( yd , xd ) ) ; //calls the 2nd version
printf(“\n%f”, atan2( yf , xf ) ) ; //calls the 1st version
printf(“\n%Lf”, atan2( yl , xl ) ) ; //calls the 3rd version

printf(“\n%lf”, atan2( y1 , x1 ) ) ;
printf(“\n%lf”, atan2(NAN , NAN ) ) ;

Output in Visual Studio,

0.828849
0.828849
0.828849
1.570796
-nan(ind)

The ‘NAN’ macro represent the ‘NaN’ value.And the output ‘-nan(ind)’ signify that the computed value is not representable.


 


atan2f

The ‘atan2f‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘f‘ character appended to ‘atan2f‘ stands for float and it signify the parameter and return type of the function.

Code example

float y=123 , x= 89 ;

printf(“%f”, atan2( y , x ) ); //Calls the 1st version
printf(“\n%f”, atan2f( 123 , x ) ); //same as above

Output in Visual Studio ,

0.944421
0.944421


atan2l

The ‘atan2l‘ function is same as the 3rd function.This means the parameter and return type of the function is long double type.The ‘l‘ character appended to ‘atan2l‘ stands for long double and it signify the parameter and return type of the function.

Code example

long double y=123 , x= 89 ;

printf(“%Lf”, atan2(y , x) ); //Calls the 3rd version
printf(“\n%Lf”, atan2l(123 , 89) ); //same as above

Output,

0.944421
0.944421



Converting atan2 returned value 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

printf( “atan2(90 , 9)=%lf%c”, ( (atan2(90 , 9)*180)/3.141592 ) , (int)248 );

Output ,

atan2(90 , 9)=84.289424°