Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      modf, modff, modfl

      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)
      (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
      Error handling
      Fast operation indicators
       
      Defined in header<math.h>
      float       modff(float arg,float* iptr);
      (1)(since C99)
      double      modf(double arg,double* iptr);
      (2)
      longdouble modfl(longdouble arg,longdouble* iptr);
      (3)(since C99)
      1-3) Decomposes given floating-point valuearg into integral and fractional parts, each having the same type and sign asarg. The integral part (in floating-point format) is stored in the object pointed to byiptr.

      Contents

      [edit]Parameters

      arg - floating-point value
      iptr - pointer to floating-point value to store the integral part to

      [edit]Return value

      If no errors occur, returns the fractional part ofarg with the same sign asarg. The integral part is put into the value pointed to byiptr.

      The sum of the returned value and the value stored in*iptr givesarg (allowing for rounding).

      [edit]Error handling

      This function is not subject to any errors specified inmath_errhandling.

      If the implementation supports IEEE floating-point arithmetic (IEC 60559),

      • Ifarg is ±0, ±0 is returned, and ±0 is stored in*iptr.
      • Ifarg is ±∞, ±0 is returned, and ±∞ is stored in*iptr.
      • Ifarg is NaN, NaN is returned, and NaN is stored in*iptr.
      • The returned value is exact,the current rounding mode is ignored.

      [edit]Notes

      This function behaves as if implemented as follows:

      double modf(double value,double*iptr){#pragma STDC FENV_ACCESS ONint save_round=fegetround();fesetround(FE_TOWARDZERO);*iptr= std::nearbyint(value);fesetround(save_round);returncopysign(isinf(value)?0.0: value-(*iptr), value);}

      [edit]Example

      Run this code
      #include <float.h>#include <math.h>#include <stdio.h> 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 %.2f + %.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); // special values    f2= modf(-0.0,&f3);printf("modf(-0) makes %.2f + %.2f\n", f3, f2);    f2= modf(-INFINITY,&f3);printf("modf(-Inf) makes %.2f + %.2f\n", f3, f2);}

      Possible output:

      Given the number 123.45 or 0x1.edccccccccccdp+6 in hex,modf() makes 123.00 + 0.45frexp() makes 0.964453 * 2^7logb()/ilogb() make 1.92891 * 2^6modf(-0) makes -0.00 + -0.00modf(-Inf) makes -INF + -0.00

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.12.6.12 The modf functions (p: TBD)
      • F.10.3.12 The modf functions (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.12.6.12 The modf functions (p: TBD)
      • F.10.3.12 The modf functions (p: TBD)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.12.6.12 The modf functions (p: 246-247)
      • F.10.3.12 The modf functions (p: 523)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.12.6.12 The modf functions (p: 227)
      • F.9.3.12 The modf functions (p: 460)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 4.5.4.6 The modf function

      [edit]See also

      (C99)(C99)(C99)
      rounds to nearest integer not greater in magnitude than the given value
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/numeric/math/modf&oldid=172052"

      [8]ページ先頭

      ©2009-2025 Movatter.jp