What is ilogb() in C?
The ilogb function is a C library function that returns the exponent of a floating point argument as an integer.
The floating point is an alternative way to represent binary numbers with a fractional part.
Every floating point number has three parts:
-
Mantissa, which is the actual number.
-
Sign, which is the sign (+/-) of the mantissa.
-
Exponent, which sets the position of the binary point.
The following illustration shows the parts of a floating point number:
To use the ilogb function, we need to include the math.h header file, as shown below:
#include <math.h>
Syntax
The ilogb function takes a single argument and returns a single value. We declare it as follows:
Parameter
The ilogb function takes a single argument of the double type.
Return value
The ilogb function returns the exponent of the argument as a signed integer.
-
If the argument is zero, the return value is the
FP_ILOGB0macro. The value ofFP_ILOGB0is eitherINT_MINor- INT_MAX. -
If the argument is
NaN(not-a-number), the return value isFP_ILOGBNANmacro. The value ofFP_ILOGBNANis eitherINT_MINorINT_MAX. -
If the argument is infinite, then
INT_MAXis returned.
The value of
INT_MAXis +2147483647. The value ofINT_MINis -2147483648.
Syntax
The code below shows the use of the ilogb function in C:
#include <stdio.h>#include <math.h>int main() {//Declare valuesint exp = 0;double x = -1.67812345e300;//Call the functionexp = ilogb (x);//Display the resultprintf ("The exponent is: %d\n", exp);return 0;}
Free Resources