C Standard Library

C <math.h> - ilogb() Function



The C <math.h> ilogb() function returns the integer part of the logarithm of |x| (mod of argument), using FLT_RADIX as base for the logarithm. On most platforms, FLT_RADIX is 2.

In special cases, the following two specific macros are returned by the function:

MacrosDescription
FP_ILOGB0 Returned when x is zero.
FP_ILOGBNAN Returned when x is NaN.

Syntax

int ilogb  (double x);
int ilogbf (float x);
int ilogbl (long double x);                  

Parameters

x Specify the value to calculate ilogb.

Return Value

Returns the integer part of the logarithm of |x|, using FLT_RADIX as base for the logarithm. In special cases, it returns the following:

  • FP_ILOGB0, if i is zero.
  • INT_MAX, if x is infinity.
  • FP_ILOGBNAN, if x is a NaN.
  • If the result is too large to be represented by a value of return type, the function returns an unspecified value, and an overflow range error occurs.

Example:

The example below shows the usage of ilogb() function.

#include <stdio.h>
#include <math.h>
 
int main (){
  printf("ilogb(10) = %d\n", ilogb(10));
  printf("ilogb(-10) = %d\n", ilogb(-10));
  return 0;
}

The output of the above code will be:

ilogb(10) = 3
ilogb(-10) = 3

❮ C <math.h> Library