|
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Defined in header <math.h> | ||
int ilogbf(float arg); | (1) | (since C99) |
int ilogb(double arg); | (2) | (since C99) |
int ilogbl(longdouble arg); | (3) | (since C99) |
Defined in header <tgmath.h> | ||
#define ilogb( arg ) | (4) | (since C99) |
Defined in header <math.h> | ||
#define FP_ILOGB0 /* implementation-defined */ | (5) | (since C99) |
#define FP_ILOGBNAN /* implementation-defined */ | (6) | (since C99) |
ilogbl is called. Otherwise, ifarg has integer type or the typedouble,ilogb is called. Otherwise,ilogbf is called.Formally, the unbiased exponent is the integral part oflogr|arg| as a signed integral value, for non-zeroarg, wherer isFLT_RADIX.
Contents |
| arg | - | floating-point value |
If no errors occur, the unbiased exponent ofarg is returned as a signed int value.
Ifarg is zero,FP_ILOGB0 is returned.
Ifarg is infinite,INT_MAX is returned.
Ifarg is a NaN,FP_ILOGBNAN is returned.
If the correct result is greater thanINT_MAX or smaller thanINT_MIN, the return value is unspecified and a domain error or range error may occur.
Errors are reported as specified inmath_errhandling.
A domain error or range error may occur ifarg is zero, infinite, or NaN.
If the correct result is greater thanINT_MAX or smaller thanINT_MIN, a domain error or a range error may occur
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
Ifarg is not zero, infinite, or NaN, the value returned is exactly equivalent to(int)logb(arg).
POSIX requires that a domain error occurs ifarg is zero, infinite, NaN, or if the correct result is outside of the range ofint.
POSIX also requires that, on XSI-conformant systems, the value returned when the correct result is greater thanINT_MAX isINT_MAX and the value returned when the correct result is less thanINT_MIN isINT_MIN.
The correct result can be represented asint on all known implementations. For overflow to occur,INT_MAX must be less thanLDBL_MAX_EXP* log2(FLT_RADIX) orINT_MIN must be greater thanLDBL_MIN_EXP-LDBL_MANT_DIG)* log2(FLT_RADIX).
The value of the exponent returned byilogb is always 1 less than the exponent retuned byfrexp because of the different normalization requirements: for the exponente returned byilogb,|arg*r-e
| is between 1 andr (typically between1 and2), but for the exponente returned byfrexp,|arg*2-e
| is between0.5 and1.
#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 handlingfeclearexcept(FE_ALL_EXCEPT);printf("ilogb(0) = %d\n", ilogb(0));if(fetestexcept(FE_INVALID))puts(" FE_INVALID raised");}
Possible output:
Given the number 123.45 or 0x1.edccccccccccdp+6 in hex,modf() makes 123 + 0.45frexp() makes 0.964453 * 2^7logb()/ilogb() make 1.92891 * 2^6ilogb(0) = -2147483648 FE_INVALID raised
(C99)(C99) | breaks a number into significand and a power of2 (function)[edit] |
(C99)(C99)(C99) | extracts exponent of the given number (function)[edit] |
(C99)(C99)(C99)(C99)(C99)(C99) | computes efficiently a number timesFLT_RADIX raised to a power (function)[edit] |
C++ documentation forilogb | |