C programming log2,log2f and log2l math.h


In C programming the <math.h> log2,log2f and log2l function compute the the base-2 logarithm of the parameter.The declaration of the functions are given below.

1 float log2(float x);
2 double log2(double x);
3 long double log2(long double x);
4 float log2f(float x);
5 long double log2l(long double x);

Parameters:
x -The value whose base-2 logarithm is to be computed.

Return type
floating point -Returns the base-2 logarithm of x.

All the functions are the same,they compute the base-2 logarithm of the argument value.The 1st,2nd and the 3rd function have the same name i.e.’log2′.The 4th and 5th function have different names.What does having a different names mean is explained in detail below.

Some point to note:

i) log2(x) is same log2(x).

ii) Domain error occur if argument is less than 0.

iii) Range error may occur if the argument is 0.

iv) If NAN is passed ‘nan’ is returned, if INFINITY is passed ‘inf’ is returned.

Code example

double d=9 , x1=-0.0 ;

float f=9 ;

long double ld=9 ;

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

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

Output in Visual Studio,

3.169925
3.169925
3.169925
-inf
-nan(ind)

The value of x1 is -0.0,here range error occur so the returned value is ‘-inf'(-infinity).


 


log2f

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

Code example

float x=100 ;

printf(“%f”, log2( x ) ); //Calls the 1st version
printf(“\n%f”, log2f( 100 ) ); Calls the 4th version

Output in Visual Studio,

6.643856
6.643856


log2l

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

Code example

long double x=100 ;

printf(“%Lf”, log2( x ) ); //Calls the 3rd version
printf(“\n%Lf”, log2l( 100 ) ); Calls the 5th version

Output,

6.643856
6.643856