ilogb, ilogbf, ilogbl
Header: <math.h>
1-3) Extracts the value of the unbiased exponent from the floating-point argument arg, and returns it as a signed integer value.
# Declarations
int ilogbf( float arg );
(since C99)
int ilogb( double arg );
(since C99)
int ilogbl( long double arg );
(since C99)
#define ilogb( arg )
(since C99)
#define FP_ILOGB0 /* implementation-defined */
(since C99)
#define FP_ILOGBNAN /* implementation-defined */
(since C99)
# Parameters
arg: floating-point value
# Return value
If no errors occur, the unbiased exponent of arg is returned as a signed int value.
# Notes
If arg is not zero, infinite, or NaN, the value returned is exactly equivalent to (int)logb(arg).
POSIX requires that a domain error occurs if arg is zero, infinite, NaN, or if the correct result is outside of the range of int.
POSIX also requires that, on XSI-conformant systems, the value returned when the correct result is greater than INT_MAX is INT_MAX and the value returned when the correct result is less than INT_MIN is INT_MIN.
The correct result can be represented as int on all known implementations. For overflow to occur, INT_MAX must be less than LDBL_MAX_EXP * log2(FLT_RADIX) or INT_MIN must be greater than LDBL_MIN_EXP - LDBL_MANT_DIG) * log2(FLT_RADIX).
The value of the exponent returned by ilogb is always 1 less than the exponent retuned by frexp because of the different normalization requirements: for the exponent e returned by ilogb, |argr-e| is between 1 and r (typically between 1 and 2), but for the exponent e returned by frexp, |arg2-e| is between 0.5 and 1.
# Example
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
int main(void)
{
double f = 123.45;
printf("Given the number %.2f or %a in hex,\n", f, f);
double f3;
double f2 = modf(f, &f3);
printf("modf() makes %.0f + %.2f\n", f3, f2);
int i;
f2 = frexp(f, &i);
printf("frexp() makes %f * 2^%d\n", f2, i);
i = ilogb(f);
printf("logb()/ilogb() make %f * %d^%d\n", f/scalbn(1.0, i), FLT_RADIX, i);
// error handling
feclearexcept(FE_ALL_EXCEPT);
printf("ilogb(0) = %d\n", ilogb(0));
if (fetestexcept(FE_INVALID))
puts(" FE_INVALID raised");
}