C programming tan , tanf and tanl


In C programming the <math.h> tan,tanf and tanl compute the tangent of the argument.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);

Parameters:
x -The value whose tangent is to be computed.

Return type
floating point value -The tangent of ‘x’.

All the functions are the same,they compute the tangent of ‘x’.The 1st,2nd and the 3rd function have the same name i.e. ‘tan’.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-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

double d=123 , x1=0 ;

float f=123 ;

long double ld=123 ;

printf(“%lf”, tan( d ) ) ; //calls the 2nd version
printf(“\n%f”, tan( f ) ) ; //calls the 1st version
printf(“\n%Lf”, tan( ld ) ) ; //calls the 3rd version

printf(“\n%lf”, tan(x1 ) ) ;
printf(“\n%lf”, tan(INFINITY) ) ;

Output in Visual Studio,

0.517927
0.517927
0.517927
0.000000
-nan(ind)

The macro ‘INFINITY’ represent the ‘infinity’ value and the output value ‘-nan(ind)’ means the returend value is not representable.


 


tanf

The ‘tanf‘ 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 ‘tanf‘ stands for float and it signify the parameter and return type of the function.

Code example

float x= 100 ;

printf(“%f”, tan( x ) ); //Calls the 1st version
printf(“\n%f”, tanf( 100 ) ); Calls the 4th version

Output,

-0.587214
-0.587214


tanl

The ‘tanl‘ 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 ‘tanl‘ stands for long double and it signify the parameter and return type of the function.

Code example

long double x= 100 ;

printf(“%Lf”, tan( x ) ); //Calls the 3rd version
printf(“\n%Lf”, tanl( 100 ) ); //Calls the 5th version

Output,

-0.587214
-0.587214






Making tan compute any value in degree

The tan,tanf,tanl function compute the sine of the value which is of radian type.In mathematical program you may want to compute the sine of some values which is given in degree.In such case we can convert the degree to radian type by applying a simple conversion rule and compute the sine of the degree.

The relation between degree and radian is given below,

1 degree =(π/180) radian

Multiplying the argument-which is in degree- with (π/180) will yield the corresponding radian value.Computing the sin of this value will yield the sine of the given degree.

Code example

double degree=100 ;

printf(“tanl(%Lf%c)=%lf”, degree , (int)248 , tanl((degree*3.141592) / 180));

Output,

tanl(100.000000°)=-0.587214