C programming logb , logbf and logbl math.h


In C programming the <math.h> logb,logbf and logbl functions extract the exponent of the argument as a signed integer value in floating point format.

1 float logb(float x);
2 double logb(double x);
3 long double logb(long double x);
4 float logbf(float x);
5 long double logbl(long double x);

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

Return type
floating point -Returns the extracted exponent of x.

All the functions are the same,they extract the exponent 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 points to note:

i) The value returned by logb(x) is the value that is raise to the power when ‘x’ is expressed in power form.For instance,logb(90) the value 90 can be written as ‘26 + 16′,the value 6 is raise as the power so it is the returned value.(Note this interpretation of logb is only true for valid values not equal to 0 or INFINITY or NAN).

ii) A domain error or range error may occur if the argument is 0.

iii) If very large or very small value is passed ±∞ is returned.

iv) If ±∞ is passed,∞ is returned.

v) If NaN is passed NaN is returned.

vi) If integer is passed as the parameter the double version is called.

Code example

double d=90 , x1=-0.00 ;

float f=90 ;

long double ld=90 ;

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

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

Output in Visual Studio,

6.000000
6.000000
4.499810
-inf
-nan(ind)

If the argument passed is 0 or -0.0 the returned value is ‘-inf’,here it means range error has occurred.



logbf

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

Code example

float x=1000 ;

printf(“%f”, logb( x ) ); //Calls the 1st version
printf(“\n%f”, logbf( 1000 ) ); Calls the 4th version

Output in Visual Studio,

9.000000
9.000000


logbl

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

Code example

long double x=1000 ;

printf(“%Lf”, logb( x ) ); //Calls the 3rd version
printf(“\n%Lf”, logbl( 1000 ) ); Calls the 5th version

Output,

9.000000
9.000000