C++ cmath exp function


The C++ <cmath> exp,expf and expl function compute the base-e exponential of the value.The declaration of the functions are given below.

1 float exp(float x);
2 double exp(double x);
3 long double exp(long double x);
4 float expf(long double x);
5 long double expl(long double x);

Parameters:
x -A floating point value.

Return type
floating point– The exponential value of x.

Some points to note:

i) exp(x) is same as ex.

ii) If the argument is very large Range error occur.In MinGW 6.1 and VS 2015 if x=>710 ,range error seems to occur.

iii) If the argument x<= -746 ,the computed value is always 0.

iv) If NAN is passed ‘nan’ is returned,.If INFINITY is passed ‘inf'(infinity) is returned.

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

Code example

/*Passing floating point value and character*/
cout<< “exp(23)=” << exp(23) << endl
<< “exp(‘B’)=” << exp(‘B’) << endl ;

/*Passing x=>710 */
cout<< “exp(710)=” << exp(710) << endl ;

/*Passing x=<-746 */
cout<< “exp(-746)=” << exp(-746) << endl ;

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

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

Output

exp(23)= 9.7448e+009
exp(‘B’)= 4.60719e+028
exp(710)= inf
exp(-746)= 0
exp(NAN)= nan
exp(INFINITY)= inf


 


expf and expl

*Note

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

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

Code example

#include <typeinfo>

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

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

Output

*expf
f
f
*expl
e (means ‘long double’)
e (means ‘long double’)