C programming exp,expf and expl


In C programming the <math.h> 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.

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

double d=555 , x1=715 ;

float f=555 ;

long double ld=555 ;

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

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

Output in Visual Studio ,

10800340716202017915566004731865563992345562575933493530346357549803051586960346307809636638422389085092263419770586768501681045280689796224890097204460645164791208011365308677265591258635396471887027149617435275932999484415559942712202362880.000000
10800340716202017915566004731865563992345562575933493530346357549803051586960346307809636638422389085092263419770586768501681045280689796224890097204460645164791208011365308677265591258635396471887027149617435275932999484415559942712202362880.000000
10800340716202017915566004731865563992345562575933493530346357549803051586960346307809636638422389085092263419770586768501681045280689796224890097204460645164791208011365308677265591258635396471887027149617435275932999484415559942712202362880.000000
inf
inf

When the argument is 715 ‘inf'(infinty) is returned meaning range error has occurred and also passing ‘NAN’-a macro for ‘NaN’ value,returned ‘inf’.

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



expf

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

Code example

float x=7 ;

printf(“%f”, exp( x ) ); //Calls the 1st version
printf(“\n%f”, expf( 7 ) ); Calls the 4th version

Output,

1096.633179
1096.633179


expl

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

Code example

long double x= 7 ;

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

Output,

1096.633158
1096.633158

If you look at the output of expl,exp(long oduble) and the output of expf ,exp( float),the accuracy of the values are different,this is the proof that they return different data type.