C programming ilogb ,ilogbf and ilogbl math.h


In C programming the <math.h> ilogb,ilogbf and ilogbl extract the exponent of the parameter as signed int value.The declaration of the functions are given below.

1 int ilogb(float x);
2 int ilogb(double x);
3 int ilogb(long double x);
4 int ilogbf(float x);
5 int ilogbl(long double x);

Parameters:
x – A floating point value.

Return type
int -The exponent value of the argument ‘x’.

All the functions are the same,they extract the exponent of ‘x’ as signed int value.The 1st,2nd and the 3rd function have the same name i.e.’ilogb’.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 return by ‘ilogb(x)’ can be said as the value raised to the power with base as 2 when the parameter is expressed in the power form.For instance,if the argument passed is 19,the value 19 can be expressed as ‘24+3′.The ilogb(19) will return the integer ‘4’,which is the power raised to 2.

ii) If ‘x’ is zero the function compute the value FP_ILOGB0.

iii) if ‘x’ is infinite the function compute the value INT_MAX.

iv) if ‘x’ is a NaN the function compute the value FP_ILOGBNAN.

v) For any value ‘x’ if the returned value should be larger than the return type can represent the result is unspecified.

vi) If the argument passed is character or int type the double version version is called-the second function.

Code example

double d=19 , x1=2e300 ;

float f=19 ;

long double ld=19 ;

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

printf(“\n%i”, ilogb( x1) ) ;
printf(“\n%”, ilogb( INFINITY ) ) ;

Output in Visual Studio,

4
4
4
997
2147483647

When ‘INFINITY’ is passed as the parameter the returned value is the value given by INT_MAX macro.






ilogbf

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

Code example

float x=400 ;

printf(“%i”, ilogb( x ) ); //Calls the 1st version
printf(“\n%i”, ilogbf( 400 ) ); Calls the 4th version

Output,

8
8


ilogbl

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

Code example

long double ld=400 ;

printf(“%i”, ilogb( ld ) ); //Calls the 3rd version
printf(“\n%i”, ilogbl( 400 ) ); Calls the 5th version

Output,

8
8