C++ cmath tanh ,tanhf and tanhl function

The C++ <cmath> tanh,tanhf and tanhl function compute the hyperbolic tangent of the given value.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);

x – A floating point value.

Return type
floating point -Hyperbolic tangent of x.

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

/*passing 3 */
cout<< “tanh(3)=” << tanh(3) << endl

/*passing 8 */
cout<< “tanh(8)=” << tanh(8) << endl

/*passing NAN */
cout<< “tanh(NAN)=” << tanh(NAN) << endl

/*passing INFINITY */
cout<< “tanh(INFINITY)=” << tanh(INFINITY) << endl


tanh(3) = 0.995055
tanh(8) = 1
tanh(NAN) = nan
tanh(INFINITY) = 1

tanhf and tanhl


i)The 4th version tanhf is same as the 1st version float tanh(float).The ‘f‘ character stands for ‘float’ which signify the argument and return type of the function.

ii)The 5th version tanhl is same as the 3rd version long double tanh(long double).The ‘l‘ character stands for ‘long double’ which signify the argument and return type of the function.

Code example

#include <typeinfo>

/*tanhf */
cout<< “*tanhf\n”;
float f=2;
cout<< typeid( tanhf(2) ).name() << endl /*identify type of tanhf retunred value */
<< typeid( tanh(f) ).name() << endl; /*identify type of tanh(float) returned value */

cout<< “*tanhl\n”;
long double ld=2;
cout<< typeid( tanhl(2) ).name() << endl /*indentify type of tanhl returned value */
<< typeid( tanh(ld) ).name() << endl; /*identify type of tanh(long double) returned value*/


e (means ‘long double’)
e (means ‘long double’)