C++ cmath ilogb ,ilogbf and ilogbl


The C++ <cmath> 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);

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

cout<< “ilogb(90)=” << ilogb(80)= << endl /*return the value express as the power in the expression “26 + 26″ */
<< “ilogb(80)=” << ilogb(80)= << endl ; /*return the value express as the power in the expression “26 + 16″ */

/*Passing INFINITY */
cout<< “ilogb(INFINITY)=” << ilogb(INFINITY)= << endl;

/*Passing NAN */
cout<< “ilogb(NAN)=” << ilogb(NAN)= << endl

Output in Code::blocks

ilogb(90)= 6
ilogb(80)= 6
ilogb(INFINITY)= 2147483647
ilogb(NAN)= -2147483648


 


ilogbf and ilogbl

i)The 4th version ilogbf is same as the 1st version int ilogb(float) .The last character ‘f‘ in ‘ilogbf’ stands for ‘float’ which signify the argument type and return type of the function.

ii)The 5th version ilogbl is same as the 3rd version int ilogb(long double).The last character ‘l‘ in ‘ilogbl’ stands for ‘long double’ which signify the argument type and return type of the function.

Code example

float f=23.4 ;
cout<< ilogbf( 23.4 ) << endl ///the first version is called
<< ilogb( f ) << endl ; //Same as above

long double ld=157.657 ;

cout<< ilogbl( 157.657 ) << endl ///the third version is called
<< ilogb( ld ) << endl ; // same as above

Output

4
4
7
7


*Side Note

A fact about ilogb:

If the correct result is outside the range of the return type,the numeric result is unspecified and the ‘invalid’ floating-point exception is raised.