C++ cmath tan , tanf and tanl function


The C++ cmath tan,tanf and tanl function compute the tangent of the given value.The declaration of the functions is 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);

Some points to note:

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

ii-The 2nd version is the default tan function that is called whenever we use tan function in our program.

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

Code example

#include <typeinfo>

cout<< tan(45) << endl ;

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

Output

1.61977
d (means’double’)



tanf and tanl

tanf function

The 1st tan function which has the parameter and return type as float is similar to the tanf function.The ‘f’ appended to tan in tanf stands for float.This means the tanf function has float as it’s parameter and return type.There is no actual difference between tan and tanf except the type of it’s parameter and the return value.

Code example

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 float type.

tanl function

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

Code example

#include <typeinfo>

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 (means ‘long double’)
e (means ‘long double’)





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.

Code example

double degree=0;

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

Output

tan(0°)=0