C++ cmath expm1, expm1f expm1l function

The C++ cmath expm1, expm1f and expm1l function compute the exponential of the argument minus 1.The declaration of the functions are given below.

1 float expm1(float x);
2 double expm1(double x);
3 long double expm1(long double x);
4 float expm1f(float x);
5 long double expm1l(long double x);

Parameters:
x – A floating point value.

Return type
Floating point -The base-e exponential of x minus 1.

The first function is called if the argument is float type.

The second function 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 arfe discussed below.

Link : C++ cmath exp2, exp2f, exp2l


Some points to note:

i) expm1(x) is same as ex-1.

ii)If x is very large Range error occurs. In case of ‘long double expm1(long double x)’ and using the MinGW/Gcc 8.1 and VS 2015, if x=> 710 the range error seem to occur. For expm1f and expm1l the value where Range error occurs vary. Their respective values are given later.

iii)If x is small , x=<-15 the function returns -1.

iv) If NAN is passed ‘nan’ is returned.If INFINITY is passed ‘infinity’ is returned.

v)If integer or character is passed the second version is called-the double version.

/*Passing floating point value*/
cout<< “expm1(3)=” << expm1(3) << endl ;

/*Passing x=710 value*/
cout<< “expm1(710)=” << expm1(710) << endl ;

/*Passing x=-15 value*/
cout<< “expm1(-15)=” << expm1(-15) << endl ;

/*Passing NAN value*/
cout<< “expm1(NAN)=” << expm1(NAN) << endl ;

/*Passing INFINITY value*/
cout<< “expm1(INFINITY)=” << expm1(INFINITY) << endl ;

Output

expm1(3)= 19.0855
expm1(710)= inf
expm1(-15)= -1
expm1(NAN)= nan
expm1(INFINITY)= infinity



C++ expm1f

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

The expm1f accept only float type argument. If the argument is not a float type then it is converted to float type.

If any argument larger than 88 is passed the Range error occurs.

cout<< expm1f( (double)17 ) << endl ; //calls expm1f

cout<< expm1f( 17 ) 
 << "\n" << expm1f( 89 ) ;

Output,

2.4155e+07
1.65164e+38
inf


C++ expm1l

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

The expm1l accept only long double type argument. If the argument is not long double type then it is converted to long double.

If the argument is greater than 11356 the Range error occurs.

cout<< expm1l(  (float)23 ) << endl;

cout<< expm1l( 11356 )
 << "\n" << expm1l( 11357 ) ;

Output

9.7448e+09
7.04915e+4931
inf


Side note

If BOOST library is used and the precision is set to 50 ,the smallest value expm1() can compiled is -37.For smaller values the returned value is always -1.




Leave a Reply

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