Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl

      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
      roundlroundllround
      (C99)(C99)(C99)
      (C99)

      (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       roundf(float arg);
      (1)(since C99)
      double      round(double arg);
      (2)(since C99)
      longdouble roundl(longdouble arg);
      (3)(since C99)
      Defined in header<tgmath.h>
      #define round( arg )
      (4)(since C99)
      Defined in header<math.h>
      long      lroundf(float arg);
      (5)(since C99)
      long      lround(double arg);
      (6)(since C99)
      long      lroundl(longdouble arg);
      (7)(since C99)
      Defined in header<tgmath.h>
      #define lround( arg )
      (8)(since C99)
      Defined in header<math.h>
      longlong llroundf(float arg);
      (9)(since C99)
      longlong llround(double arg);
      (10)(since C99)
      longlong llroundl(longdouble arg);
      (11)(since C99)
      Defined in header<tgmath.h>
      #define llround( arg )
      (12)(since C99)
      1-3) Computes the nearest integer value toarg (in floating-point format), rounding halfway cases away from zero, regardless of the current rounding mode.
      5-7, 9-11) Computes the nearest integer value toarg (in integer format), rounding halfway cases away from zero, regardless of the current rounding mode.
      4,8,12) Type-generic macros: Ifarg has typelongdouble,roundl,lroundl,llroundl is called. Otherwise, ifarg has integer type or the typedouble,round,lround,llround is called. Otherwise,roundf,lroundf,llroundf is called, respectively.

      Contents

      [edit]Parameters

      arg - floating-point value

      [edit]Return value

      If no errors occur, the nearest integer value toarg, rounding halfway cases away from zero, is returned.

      Return value
      math-round away zero.svg
      Argument

      If a domain error occurs, an implementation-defined value is returned.

      [edit]Error handling

      Errors are reported as specified inmath_errhandling.

      If the result oflround orllround is outside the range representable by the return type, a domain error or a range error may occur.

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

      For theround,roundf, androundl function:
      • The currentrounding mode has no effect.
      • Ifarg is ±∞, it is returned, unmodified.
      • Ifarg is ±0, it is returned, unmodified.
      • Ifarg is NaN, NaN is returned.
      Forlround andllround families of functions:
      • FE_INEXACT is never raised.
      • The currentrounding mode has no effect.
      • Ifarg is ±∞,FE_INVALID is raised and an implementation-defined value is returned
      • If the result of the rounding is outside the range of the return type,FE_INVALID is raised and an implementation-defined value is returned.
      • Ifarg is NaN,FE_INVALID is raised and an implementation-defined value is returned.

      [edit]Notes

      FE_INEXACT may be (but isn't required to be) raised byround when rounding a non-integer finite value.

      The largest representable floating-point values are exact integers in all standard floating-point formats, soround never overflows on its own; however the result may overflow any integer type (includingintmax_t), when stored in an integer variable.

      POSIX specifies that all cases wherelround orllround raiseFE_INVALID are domain errors.

      Thedouble version ofround behaves as if implemented as follows:

      #include <math.h>#pragma STDC FENV_ACCESS ON double round(double x){returnsignbit(x)?ceil(x-0.5):floor(x+0.5);}

      [edit]Example

      Run this code
      #include <assert.h>#include <fenv.h>#include <float.h>#include <limits.h>#include <math.h>#include <stdio.h>// #pragma STDC FENV_ACCESS ON double custom_round(double x){returnsignbit(x)?ceil(x-0.5):floor(x+0.5);} void test_custom_round(){constdouble sample[]={0.0,2.3,2.5-DBL_EPSILON,2.5,2.5+DBL_EPSILON,2.7, INFINITY};for(size_t t=0; t<sizeof sample/sizeof(double);++t)assert(round(+sample[t])== custom_round(+sample[t])&&               round(-sample[t])== custom_round(-sample[t]));} int main(void){// roundprintf("round(+2.3) = %+.1f  ", round(2.3));printf("round(+2.5) = %+.1f  ", round(2.5));printf("round(+2.7) = %+.1f\n", round(2.7));printf("round(-2.3) = %+.1f  ", round(-2.3));printf("round(-2.5) = %+.1f  ", round(-2.5));printf("round(-2.7) = %+.1f\n", round(-2.7)); printf("round(-0.0) = %+.1f\n", round(-0.0));printf("round(-Inf) = %+f\n",   round(-INFINITY));     test_custom_round(); // lroundprintf("lround(+2.3) = %+ld  ", lround(2.3));printf("lround(+2.5) = %+ld  ", lround(2.5));printf("lround(+2.7) = %+ld\n", lround(2.7));printf("lround(-2.3) = %+ld  ", lround(-2.3));printf("lround(-2.5) = %+ld  ", lround(-2.5));printf("lround(-2.7) = %+ld\n", lround(-2.7)); printf("lround(-0.0) = %+ld\n", lround(-0.0));printf("lround(-Inf) = %+ld\n", lround(-INFINITY));// FE_INVALID raised // error handlingfeclearexcept(FE_ALL_EXCEPT);printf("lround(LONG_MAX+1.5) = %ld\n", lround(LONG_MAX+1.5));if(fetestexcept(FE_INVALID))puts("    FE_INVALID was raised");}

      Possible output:

      round(+2.3) = +2.0  round(+2.5) = +3.0  round(+2.7) = +3.0round(-2.3) = -2.0  round(-2.5) = -3.0  round(-2.7) = -3.0round(-0.0) = -0.0round(-Inf) = -inflround(+2.3) = +2  lround(+2.5) = +3  lround(+2.7) = +3lround(-2.3) = -2  lround(-2.5) = -3  lround(-2.7) = -3lround(-0.0) = +0lround(-Inf) = -9223372036854775808lround(LONG_MAX+1.5) = -9223372036854775808    FE_INVALID was raised

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.12.9.6 The round functions (p: TBD)
      • 7.12.9.7 The lround and llround functions (p: TBD)
      • 7.25 Type-generic math <tgmath.h> (p: TBD)
      • F.10.6.6 The round functions (p: TBD)
      • F.10.6.7 The lround and llround functions (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.12.9.6 The round functions (p: 184)
      • 7.12.9.7 The lround and llround functions (p: 184-185)
      • 7.25 Type-generic math <tgmath.h> (p: 272-273)
      • F.10.6.6 The round functions (p: 384)
      • F.10.6.7 The lround and llround functions (p: 385)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.12.9.6 The round functions (p: 253)
      • 7.12.9.7 The lround and llround functions (p: 253)
      • 7.25 Type-generic math <tgmath.h> (p: 373-375)
      • F.10.6.6 The round functions (p: 527)
      • F.10.6.7 The lround and llround functions (p: 528)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.12.9.6 The round functions (p: 233)
      • 7.12.9.7 The lround and llround functions (p: 234)
      • 7.22 Type-generic math <tgmath.h> (p: 335-337)
      • F.9.6.6 The round functions (p: 464)
      • F.9.6.7 The lround and llround functions (p: 464)

      [edit]See also

      computes largest integer not greater than the given value
      (function)[edit]
      (C99)(C99)
      computes smallest integer not less than the given value
      (function)[edit]
      (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/round&oldid=172059"

      [8]ページ先頭

      ©2009-2025 Movatter.jp