ilogb Function

Calculates the signed integer exponent of a double precision floating-point value.

Include

<math.h>

Prototype

int ilogb(double x);

Argument

x
any positive value for which to return the exponent

Return Value

Returns the exponent of x as a signed integer value. If x is 0, it returns the value FP_ILOGB0; if x is infinite, it returns the value INT_MAX; if x is a NaN it returns the value FP_ILOGBNAN; otherwise, this will yield the same value as the corresponding logb function cast to type int. A range error may occur if x is 0.

Remarks

A range error might occur if x is 0.

Example

#include <math.h>
#include <stdio.h>
#include <errno.h>

int main(void)
{
  double x;
  int y;

  errno = 0;
  x = 13.45;
  y = ilogb(x);
  if (errno)
    perror("Error");
  printf("The exponent of %f is %d\n", x, y);

  errno = 0;
  x = 0.0;
  y = ilogb(x);
  if (errno)
    perror("Error");
  printf("The exponent of %f is %d\n", x, y);

  errno = 0;
  x = -2.0;
  y = ilogb(x);
  if (errno)
    perror("Error");
  printf("The exponent of %f is %d\n", x, y);
}

Example Output

The exponent of 13.450000 is 3
The exponent of 0.000000 is -2147483648
The exponent of -2.000000 is 1