Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      rint, rintf, rintl, lrint, lrintf, lrintl, llrint, llrintf, llrintl

      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
      rintlrintllrint
      (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 rintf(float arg);
      (1)(since C99)
      double rint(double arg);
      (2)(since C99)
      longdouble rintl(longdouble arg);
      (3)(since C99)
      Defined in header<tgmath.h>
      #define rint( arg )
      (4)(since C99)
      Defined in header<math.h>
      long lrintf(float arg);
      (5)(since C99)
      long lrint(double arg);
      (6)(since C99)
      long lrintl(longdouble arg);
      (7)(since C99)
      Defined in header<tgmath.h>
      #define lrint( arg )
      (8)(since C99)
      Defined in header<math.h>
      longlong llrintf(float arg);
      (9)(since C99)
      longlong llrint(double arg);
      (10)(since C99)
      longlong llrintl(longdouble arg);
      (11)(since C99)
      Defined in header<tgmath.h>
      #define llrint( arg )
      (12)(since C99)
      1-3) Rounds the floating-point argumentarg to an integer value in floating-point format, using the current rounding mode.
      5-7, 9-11) Rounds the floating-point argumentarg to an integer value in integer format, using the current rounding mode.
      4,8,12) Type-generic macros: Ifarg has typelongdouble,rintl,lrintl,llrintl is called. Otherwise, ifarg has integer type or the typedouble,rint,lrint,llrint is called. Otherwise,rintf,lrintf,llrintf is called, respectively.

      Contents

      [edit]Parameters

      arg - floating-point value

      [edit]Return value

      If no errors occur, the nearest integer value toarg, according to thecurrent rounding mode, is returned.

      [edit]Error handling

      Errors are reported as specified inmath_errhandling.

      If the result oflrint orllrint 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 therint function:
      • Ifarg is ±∞, it is returned, unmodified.
      • Ifarg is ±0, it is returned, unmodified.
      • Ifarg is NaN, NaN is returned.
      Forlrint andllrint functions:
      • 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

      POSIX specifies that all cases wherelrint orllrint raiseFE_INEXACT are domain errors.

      As specified inmath_errhandling,FE_INEXACT may be (but isn't required to be on non-IEEE floating-point platforms) raised byrint when rounding a non-integer finite value.

      The only difference betweenrint andnearbyint is thatnearbyint never raisesFE_INEXACT.

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

      If the current rounding mode is...

      [edit]Example

      Run this code
      #include <fenv.h>#include <limits.h>#include <math.h>#include <stdio.h> int main(void){#pragma STDC FENV_ACCESS ONfesetround(FE_TONEAREST);printf("rounding to nearest (halfway cases to even):\n""rint(+2.3) = %+.1f  ", rint(2.3));printf("rint(+2.5) = %+.1f  ", rint(2.5));printf("rint(+3.5) = %+.1f\n", rint(3.5));printf("rint(-2.3) = %+.1f  ", rint(-2.3));printf("rint(-2.5) = %+.1f  ", rint(-2.5));printf("rint(-3.5) = %+.1f\n", rint(-3.5)); fesetround(FE_DOWNWARD);printf("rounding down:\nrint(+2.3) = %+.1f  ", rint(2.3));printf("rint(+2.5) = %+.1f  ", rint(2.5));printf("rint(+3.5) = %+.1f\n", rint(3.5));printf("rint(-2.3) = %+.1f  ", rint(-2.3));printf("rint(-2.5) = %+.1f  ", rint(-2.5));printf("rint(-3.5) = %+.1f\n", rint(-3.5));printf("rounding down with lrint:\nlrint(+2.3) = %ld  ", lrint(2.3));printf("lrint(+2.5) = %ld  ", lrint(2.5));printf("lrint(+3.5) = %ld\n", lrint(3.5));printf("lrint(-2.3) = %ld  ", lrint(-2.3));printf("lrint(-2.5) = %ld  ", lrint(-2.5));printf("lrint(-3.5) = %ld\n", lrint(-3.5)); printf("lrint(-0.0) = %ld\n", lrint(-0.0));printf("lrint(-Inf) = %ld\n", lrint(-INFINITY));// FE_INVALID raised // error handlingfeclearexcept(FE_ALL_EXCEPT);printf("rint(1.1) = %.1f\n", rint(1.1));if(fetestexcept(FE_INEXACT))puts("    FE_INEXACT was raised"); feclearexcept(FE_ALL_EXCEPT);printf("lrint(LONG_MIN-2048.0) = %ld\n", lrint(LONG_MIN-2048.0));if(fetestexcept(FE_INVALID))puts("    FE_INVALID was raised");}

      Possible output:

      rounding to nearest (halfway cases to even):rint(+2.3) = +2.0  rint(+2.5) = +2.0  rint(+3.5) = +4.0rint(-2.3) = -2.0  rint(-2.5) = -2.0  rint(-3.5) = -4.0rounding down:rint(+2.3) = +2.0  rint(+2.5) = +2.0  rint(+3.5) = +3.0rint(-2.3) = -3.0  rint(-2.5) = -3.0  rint(-3.5) = -4.0rounding down with lrint:lrint(+2.3) = 2  lrint(+2.5) = 2  lrint(+3.5) = 3lrint(-2.3) = -3  lrint(-2.5) = -3  lrint(-3.5) = -4lrint(-0.0) = 0lrint(-Inf) = -9223372036854775808rint(1.1) = 1.0    FE_INEXACT was raisedlrint(LONG_MIN-2048.0) = -9223372036854775808    FE_INVALID was raised

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 7.12.9.4 The rint functions (p: TBD)
      • 7.12.9.5 The lrint and llrint functions (p: TBD)
      • 7.25 Type-generic math <tgmath.h> (p: TBD)
      • F.10.6.4 The rint functions (p: TBD)
      • F.10.6.5 The lrint and llrint functions (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 7.12.9.4 The rint functions (p: 184)
      • 7.12.9.5 The lrint and llrint functions (p: 184)
      • 7.25 Type-generic math <tgmath.h> (p: 272-273)
      • F.10.6.4 The rint functions (p: 384)
      • F.10.6.5 The lrint and llrint functions (p: 384)
      • C11 standard (ISO/IEC 9899:2011):
      • 7.12.9.4 The rint functions (p: 252)
      • 7.12.9.5 The lrint and llrint functions (p: 252)
      • 7.25 Type-generic math <tgmath.h> (p: 373-375)
      • F.10.6.4 The rint functions (p: 527)
      • F.10.6.5 The lrint and llrint functions (p: 527)
      • C99 standard (ISO/IEC 9899:1999):
      • 7.12.9.4 The rint functions (p: 232-233)
      • 7.12.9.5 The lrint and llrint functions (p: 233)
      • 7.22 Type-generic math <tgmath.h> (p: 335-337)
      • F.9.6.4 The rint functions (p: 463)
      • F.9.6.5 The lrint and llrint functions (p: 463)

      [edit]See also

      (C99)(C99)(C99)
      rounds to nearest integer not greater in magnitude than the given value
      (function)[edit]
      rounds to an integer using current rounding mode
      (function)[edit]
      gets or sets rounding direction
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/numeric/math/rint&oldid=172058"

      [8]ページ先頭

      ©2009-2025 Movatter.jp