Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      ilogb, ilogbf, ilogbl

      From cppreference.com
      <c‎ |numeric‎ |math
       
       
       
      Common mathematical functions
      Functions
      Basic operations
      (C99)
      (C99)
      (C99)
      (C99)(C99)(C99)(C23)
      Maximum/minimum operations
      (C99)
      (C99)
      Exponential functions
      (C23)
      (C99)
      (C99)
      (C23)
      (C23)

      (C99)
      (C99)(C23)
      (C23)
      (C23)
      Power functions
      (C99)
      (C23)
      (C23)

      (C99)
      (C23)
      (C23)
      Trigonometric and hyperbolic functions
      (C23)
      (C23)
      (C23)
      (C23)
      (C99)
      (C99)
      (C99)
      Nearest integer floating-point
      (C99)(C99)(C99)
      (C23)(C23)(C23)(C23)
      Floating-point manipulation
      (C99)(C99)
      ilogbllogb
      (C99)(C23)
      (C99)
      Narrowing operations
      (C23)
      (C23)
      (C23)
      (C23)
      (C23)
      (C23)
      Quantum and quantum exponent
      Decimal re-encoding functions
      Total order and payload functions
      Classification
      Error and gamma functions
      (C99)
      (C99)
      (C99)
      (C99)
      Types
      Macro constants
      Special floating-point values
      (C99)(C23)
      Arguments and return values
      FP_ILOGB0FP_ILOGBNAN
      (C99)(C99)
      Error handling
      Fast operation indicators
       
      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)
      1-3) Extracts the value of the unbiased exponent from the floating-point argumentarg, and returns it as a signed integer value.
      4) Type-generic macros: Ifarg has typelongdouble,ilogbl is called. Otherwise, ifarg has integer type or the typedouble,ilogb is called. Otherwise,ilogbf is called.
      5) Expands to integer constant expression whose value is eitherINT_MIN or-INT_MAX.
      6) Expands to integer constant expression whose value is eitherINT_MIN or+INT_MAX.

      Formally, the unbiased exponent is the integral part oflogr|arg| as a signed integral value, for non-zeroarg, wherer isFLT_RADIX.

      Contents

      [edit]Parameters

      arg - floating-point value

      [edit]Return 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.

      [edit]Error handling

      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),

      [edit]Notes

      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.

      [edit]Example

      Run this code
      #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

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.12/8 Mathematics <math.h> (p: TBD)
      • 7.12.6.5 The ilogb functions (p: TBD)
      • 7.25 Type-generic math <tgmath.h> (p: TBD)
      • F.10.3.5 The ilogb functions (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.12/8 Mathematics <math.h> (p: TBD)
      • 7.12.6.5 The ilogb functions (p: TBD)
      • 7.25 Type-generic math <tgmath.h> (p: TBD)
      • F.10.3.5 The ilogb functions (p: TBD)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.12/8 Mathematics <math.h> (p: 232)
      • 7.12.6.5 The ilogb functions (p: 244)
      • 7.25 Type-generic math <tgmath.h> (p: 373-375)
      • F.10.3.5 The ilogb functions (p: 521)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.12/8 Mathematics <math.h> (p: 213)
      • 7.12.6.5 The ilogb functions (p: 224-225)
      • 7.22 Type-generic math <tgmath.h> (p: 335-337)
      • F.9.3.5 The ilogb functions (p: 458)

      [edit]See also

      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]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/numeric/math/ilogb&oldid=172026"

      [8]ページ先頭

      ©2009-2025 Movatter.jp