C programming tanh,tanhf and tanhl


In C programming the <math.h> tanh ,tanhf and tanhl compute the hyperbolic sine of the argument.The declaration of the functions are given below.

1 float tanh(float x);
2 double tanh(double x);
3 long double tanh(long double x);
4 float tanhf(float x);
5 long double tanhl(long double x);

Parameters:
x – A floating point value.

Return type
floating point -Hyperbolic tangent of x.

All the functions are the same,they compute the hyperbolic tangent of ‘x’.The 1st,2nd and the 3rd function have the same name i.e.’tanh’.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) If the argument x=>8 the value computed is always 1.And for x<=-8 the value computed is always -1.

ii)If NAN is passed ‘nan’ is returned.

iii)If INFINITY is passed 1 is returned.

Code example

double d=50 , x1=-8 ;

float f=5 ;

long double ld=5 ;

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

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

Output in Visual Studio ,

0.999909
0.999909
0.999909
-1.000000
-nan(ind)

When the argument is equal to -8 the output is -1.And the output ‘-nan(ind)’ means it is not representable.It simply stands for “NaN”.

Link :C and C++ nan:myths and conception



tanhf

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

Code example

float x=3 ;

printf(“%f”, tanh( x ) ); //Calls the 1st version
printf(“\n%f”, tanhf( 3 ) ); Calls the 4th version

Output,

0.995055
0.995055


tanhl

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

Code example

long double x= 3 ;

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

Output,

0.995055
0.995055