C programming log,logf and logl math.h


In C programming the <math.h> log ,logf and logl compute the base-e logarithm of the parameter.The declaration of the functions are given below.

1 float log(float x);
2 double log(double x);
3 long double log(long double x);
4 float logf(float x);
5 long double logl(long double x);

Parameters:
x -The floating point value for which the logarithm is to be computed.

Return type
floating point -The base-e logarithm of ‘x’.

All the functions are the same,they compute the base-e logarithm of the parameter.The 1st,2nd and the 3rd function have the same name i.e.’log’.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) log(x) is same as loge( x ).

ii) A domain error occur if the argument is negative.MinGW returns ‘nan’ ,VS returns ‘-nan’.

iii) A range error may occur if the argument is 0.MinGW returns ‘-inf’ ,VS returns ‘-inf’.

iii) A NAN is passed ‘nan’ is returned,if INFINITY is passed ‘inf’ is returned.

Code example

double d=12 , x1=-2 ;

float f=12 ;

long double ld=12 ;

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

printf(“\n%lf”, log( x1 ) ) ;
printf(“\n%lf”, log( INFINITY ) ) ;

Output in Visual Studio,

2.484907
2.484907
2.484907
-nan(ind)
inf

When the parameter is -2 the returned value is ‘-nan(ind)’ which means the returned value is not representable.And when INFINITY(a macro for infinity value) is passed ‘inf’ is returned.






logf

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

Code example

float x=22 ;

printf(“%f”, log( x ) ); //Calls the 1st version
printf(“\n%f”, logf( 22 ) ); Calls the 4th version

Output in Visual Studio,

3.091042
3.091042


logl

The ‘logl‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘l‘ character appended to ‘logl‘ stands for float and it signify the parameter and return type of the function.

Code example

long double x=22 ;

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

Output,

3.091042
3.091042