C++ cmath atan,atanf and atanl function

The C++ cmath atan,atanf and atanl function computes the arc tangent of the given value.The declaration of the functions are given below:

1 float atan(float x);
2 double atan(double x);
3 long double atan(long double x);
4 float atanf(float x);
5 long double atanl(long double x);

The first function ‘float atan(float x)’ is called if the argument is float type.

The second ‘double atan(double x)’ is called, if the argument is double type.

The third function is called if the argument is long double type.

The 4th and 5th function are discussed in detail below.

Link : C++ cmath asin , asinf and asinl


Some points to note:

i)-There is no restriction on the range of values that can be used as an argument.

ii)-The returned value is in the interval [-π/2,+π/2] radians.

iii)-The second version is the default version that is called when atan is called wihtout any specific argument type.

iv)-When the argument passed is int type the default version is called.

Note arc tangent means inverse of tangent,so atan(x) is same as tan-1(x).

Include <typeinfo> to call the ‘typeid()’ function. This function checks the type of the argument.

cout<< atan(0.23) << endl //calls atan(double)
 << atan(9090); 

cout<< typeid( atan(0.23) ).name( ) ;

Output,

0.226068
1.57069
d

The ‘d’ in the output means ‘double’.


C++ cmath atanf function

The first version i.e. float atan(float x) function is similar to the atanf function. Both these functions accpet float as the argument type and the return type. The ‘f’ in atanf means float which signify the type of the argument and the return value.

float f=0.2;

cout<< atan(f) << endl
 << atanf(0.2) << endl ;

cout<< typeid( atan(f) ).name( ) << endl
 << typeid( atanf(0) ).name( ) << endl ;

Output

0.197396
0
f
f


Property of acosf

If we pass an argument type other than the float type, say double type to ‘atanf’, then this function will always convert the argument type to the float type first and then compute the arc tangent of the float argument value. The return type is also float type. In other words, this function always constrain the parameter type and the return type to the the float type.

cout<< atanf( (double)40 ) ;//calls atanf(float)

cout<<"\nReturn type:"<< typeid( atanf( (double)40) ).name( ) ;

Output,

1.5458
Return type:f


C++ cmath atanl function

The atanl function is similar to the 3rd version atan function. Both the functions have long double as the argument and the return type. The argument and the return type in atanl function is specified by the character ‘l’ which stands for long double.

long double ld=0.22 ;

cout<< atan(ld) << endl
 << atanl(0.22) << endl ;

cout<< typeid( atan(ld) ).name( ) << endl
 << typeid( atanl(0.22) ).name( ) << endl ;

Output

0.21655
0.21655
e
e


Property of acosl

If we pass an argument type other than the long double type, say double type to ‘atanl’, then this function will always convert the argument type to the long double type first, and then compute the arc tangent of the long double argument value. The return type is also long double type. In other words, this function always constrain the parameter type and the return type to the the long double type.

cout<< atanl( (double)40 ) ;//calls atanl(long double)

cout<<"\nReturn type:"<< typeid( atanl( (double)40) ).name( ) ;

Output,

1.5458
Return type:e


getting degree instead of radian with atan

The value returned by atan is in radian.You can convert it to degree by using the simple conversion rule.

The relation between radian and degree is shown below,

1 radian = (180/3.141592) degree

Multiplying the output obtained with (180/3.141592) gives the degree of the atan computed value.

cout<< “atan(0.5°)=” << ( atan(1)*180 )/3.141592 << “°” << endl ;

Output

atan(1)=45°




Leave a Reply

Your email address will not be published. Required fields are marked *