function
<cmath> <ctgmath>

ilogb

int ilogb  (double x);
int ilogbf (float x);
int ilogbl (long double x);
int ilogb (double x);
int ilogb (float x);
int ilogb (long double x);
int ilogb (T x);           // additional overloads for integral types
Integer binary logarithm
Returns the integral part of the logarithm of |x|, using FLT_RADIX as base for the logarithm.

This is the exponent used internally by the machine to express the floating-point value x, when it uses a significand between 1.0 and FLT_RADIX, so that, for a positive x:

x = significand * FLT_RADIX exponent

Generally, FLT_RADIX is 2, and the value returned by this function is one less than the exponent obtained with frexp (because of the different significand normalization as [1.0,2.0) instead of [0.5,1.0)).

Header <tgmath.h> provides a type-generic macro version of this function.
Additional overloads are provided in this header (<cmath>) for the integral types: These overloads effectively cast x to a double before calculations (defined for T being any integral type).

Two specific macro constants may be returned by this function to indicate the following special cases:
macrodescription
FP_ILOGB0x is zero
FP_ILOGBNANx is NaN

These macro constants are defined in this same header (<cmath>).

Parameters

x
Value whose ilogb is returned.

Return Value

If x is normal, the base-FLT_RADIX logarithm of x.
If x is subnormal, the value returned is the one corresponding to the normalized representation (negative exponent).
If x is zero, it returns FP_LOGB0 (a special value, only returned by this function, defined in <cmath>).
If x is infinite, it returns INT_MAX.
If x is NaN, it returns FP_ILOGBNAN (a special value, only returned by this function, defined in <cmath>).
If the magnitude of the result is too large to be represented by a value of the return type, the function returns an unspecified value, and an overflow range error occurs.
A zero, infinite or NaN value of x may also cause either a domain error or an overflow range error.

If an domain error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to EDOM.
- And math_errhandling has MATH_ERREXCEPT set: FE_INVALID is raised.

If an overflow range error occurs:
- And math_errhandling has MATH_ERRNO set: the global variable errno is set to ERANGE.
- And math_errhandling has MATH_ERREXCEPT set: FE_OVERFLOW is raised.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* ilogb example */
#include <stdio.h>      /* printf */
#include <math.h>       /* ilogb */

int main ()
{
  double param;
  int result;

  param = 10.0;
  result = ilogb (param);
  printf ("ilogb(%f) = %d\n", param, result);
  return 0;
}


Output:

ilogb(10.000000) = 3

See also