C++ cmath tan, tanf and tanl function

The C++ cmath tan,tanf and tanl functions compute the tangent of the given value. The declaration of the functions are given below:

1 float tan(float x);
2 double tan(double x);
3 long double tan(long double x);
4 float tanf(float x);
5 long double tanl(long double x);

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

The second ‘double tan(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.

Linlk : C++ cmath sin , sinf and sinl


Some points to note(these points are applicable to all the five ‘tan’ functions):

i-The parameter ‘x’ is measured in radians.

ii-The 2nd version is the default function. In other words, if the argument type is not specifically mentioned the ‘double tan(double x)’ function is called.

iii-If the argument passed is int type the default tan version is called.

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


cout<< tan(45) << endl ;

cout<< typeid( tan(89) ).name( ) ; //checking the type of the computed value

Output

1.61977
d (means’double’)


C++ cmath tanf

The 1st ‘tan’ function which has the parameter and the return type as the float type is similar to the tanf function. The ‘f’ appended to ‘tan’ in tanf stands for float. This means the tanf function has float as the parameter and return type.

float f=50;

cout<< tan(f) << endl ///calls the 1st version
 << tanf(50) ;

cout<< endl ;

cout<< typeid( tan(f) ).name() << endl
 << typeid( tan(50) ).name() ;

Output

-0.271900
-0.271900
f (means ‘float’)
f (means ‘float’)

You can see that tan and tanf yield the same value and also the returned type is the float type.


Property of tanf

If we pass an argument type other than the float type, say double type to ‘sinf’, then this function will always convert the argument type to the float type first and then compute the 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<< tanf( (double)90 ); //calls tanf(float)

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

Output,

-1.9952
Return type:d



C++ cmath tanl

Like tanf is similar to the 1st version, tanl is similar to the 3rd version. The 3rd version has the parameter type and the return type as the long double. The ‘l’ in tanl means long double, so it accepts long double parameter and also returns long double type value.

long double ld=50 ;

cout<< tan(ld) << endl //calls the 3rd version
 << tanl(50) << endl ;

cout<< typeid( tan(ld) ).name() << endl
 << typeid( tanl(50) ).name() ;

Output

-0.271900
-0.271900
e
e

The ‘e’ means long double.


Property of tanl

If we pass an argument type other than the long double type, say double type to ‘tanl’, then this function will always convert the argument type to the long double type first, and then compute the 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<< tanl( (double)123 );

cout<< "\nReturn type:" << typeid( tanl( (double)123) ).name() ;

Output,

0.517927
Return type:e


making tan compute any value in degree

The tan function argument is measured in radian. If the argument is in degree we must convert the degree to radian type first. This can be done by applying simple conversion rule.

The relation between radian and degree is given below,

1 degree =(π/180) radian

Multiplying the degree with (π/180) will convert it to radian type.

double degree=0;

cout<< “tan(0°)=” << tan( (degree*3.141592)/180 );

Output

tan(0°)=0




Leave a Reply

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