Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::rint,std::rintf,std::rintl,std::lrint,std::lrintf,std::lrintl,std::llrint,std::llrintf

      From cppreference.com
      <cpp‎ |numeric‎ |math
       
       
       
      Common mathematical functions
      Nearest integer floating point operations
      (C++11)(C++11)(C++11)
      (C++11)
      (C++11)
      rintlrintllrint
      (C++11)(C++11)(C++11)
      Floating point manipulation functions
      (C++11)(C++11)
      (C++11)
      (C++11)
      Classification and comparison
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      Types
      (C++11)
      (C++11)
      (C++11)
      Macro constants
       
      Defined in header<cmath>
      Rounding to floating-point types
      (1)
      float       rint(float num);

      double      rint(double num);

      longdouble rint(longdouble num);
      (since C++11)
      (until C++23)
      /* floating-point-type */ rint(/* floating-point-type */ num);
      (since C++23)
      float       rintf(float num);
      (2)(since C++11)
      longdouble rintl(longdouble num);
      (3)(since C++11)
      Rounding tolong
      (4)
      long lrint(float num);

      long lrint(double num);

      long lrint(longdouble num);
      (since C++11)
      (until C++23)
      long lrint(/* floating-point-type */ num);
      (since C++23)
      long lrintf(float num);
      (5)(since C++11)
      long lrintl(longdouble num);
      (6)(since C++11)
      Rounding tolonglong
      (7)
      longlong llrint(float num);

      longlong llrint(double num);

      longlong llrint(longdouble num);
      (since C++11)
      (until C++23)
      longlong llrint(/* floating-point-type */ num);
      (since C++23)
      longlong llrintf(float num);
      (8)(since C++11)
      longlong llrintl(longdouble num);
      (9)(since C++11)
      Defined in header<cmath>
      template<class Integer>
      double rint( Integer num);
      (A)(since C++11)
      template<class Integer>
      long lrint( Integer num);
      (B)(since C++11)
      template<class Integer>
      longlong llrint( Integer num);
      (C)(since C++11)
      1-3) Rounds the floating-point argumentnum to an integer value (in floating-point format), using thecurrent rounding mode. The library provides overloads ofstd::rint for all cv-unqualified floating-point types as the type of the parameternum.(since C++23)
      4-9) Rounds the floating-point argumentnum to an integer value, using thecurrent rounding mode. The library provides overloads ofstd::lrint andstd::llrint for all cv-unqualified floating-point types as the type of the parameternum.(since C++23)
      A-C) Additional overloads are provided for all integer types, which are treated asdouble.

      Contents

      [edit]Parameters

      num - floating-point or integer value

      [edit]Return value

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

      [edit]Error handling

      Errors are reported as specified inmath_errhandling.

      If the result ofstd::lrint orstd::llrint 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 thestd::rint function:
      • Ifnum is ±∞, it is returned, unmodified.
      • Ifnum is ±0, it is returned, unmodified.
      • Ifnum is NaN, NaN is returned.
      Forstd::lrint andstd::llrint functions:
      • Ifnum 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.
      • Ifnum is NaN,FE_INVALID is raised and an implementation-defined value is returned.

      [edit]Notes

      POSIX specifies that all cases wherestd::lrint orstd::llrint 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 bystd::rint when rounding a non-integer finite value.

      The only difference betweenstd::rint andstd::nearbyint is thatstd::nearbyint never raisesFE_INEXACT.

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

      If the current rounding mode is:

      The additional overloads are not required to be provided exactly as(A-C). They only need to be sufficient to ensure that for their argumentnum of integer type:

      • std::rint(num) has the same effect asstd::rint(static_cast<double>(num)).
      • std::lrint(num) has the same effect asstd::lrint(static_cast<double>(num)).
      • std::llrint(num) has the same effect asstd::llrint(static_cast<double>(num)).

      [edit]Example

      Run this code
      #include <cfenv>#include <climits>#include <cmath>#include <iostream>// #pragma STDC FENV_ACCESS ON int main(){std::fesetround(FE_TONEAREST);std::cout<<"Rounding to nearest (halfway cases to even):\n"<<"  rint(+2.3) = "<< std::rint(2.3)<<'\n'<<"  rint(+2.5) = "<< std::rint(2.5)<<'\n'<<"  rint(+3.5) = "<< std::rint(3.5)<<'\n'<<"  rint(-2.3) = "<< std::rint(-2.3)<<'\n'<<"  rint(-2.5) = "<< std::rint(-2.5)<<'\n'<<"  rint(-3.5) = "<< std::rint(-3.5)<<'\n'; std::fesetround(FE_DOWNWARD);std::cout<<"Rounding down:\n"<<"  rint(+2.3) = "<< std::rint(2.3)<<'\n'<<"  rint(+2.5) = "<< std::rint(2.5)<<'\n'<<"  rint(+3.5) = "<< std::rint(3.5)<<'\n'<<"  rint(-2.3) = "<< std::rint(-2.3)<<'\n'<<"  rint(-2.5) = "<< std::rint(-2.5)<<'\n'<<"  rint(-3.5) = "<< std::rint(-3.5)<<'\n'<<"Rounding down with lrint:\n"<<"  lrint(+2.3) = "<< std::lrint(2.3)<<'\n'<<"  lrint(+2.5) = "<< std::lrint(2.5)<<'\n'<<"  lrint(+3.5) = "<< std::lrint(3.5)<<'\n'<<"  lrint(-2.3) = "<< std::lrint(-2.3)<<'\n'<<"  lrint(-2.5) = "<< std::lrint(-2.5)<<'\n'<<"  lrint(-3.5) = "<< std::lrint(-3.5)<<'\n'<<"Special values:\n"<<"  lrint(-0.0) = "<< std::lrint(-0.0)<<'\n'<<std::hex<<std::showbase<<"  lrint(-Inf) = "<< std::lrint(-INFINITY)<<'\n'; // error handlingstd::feclearexcept(FE_ALL_EXCEPT); std::cout<<"std::rint(0.1) = "<< std::rint(.1)<<'\n';if(std::fetestexcept(FE_INEXACT))std::cout<<"  FE_INEXACT was raised\n"; std::feclearexcept(FE_ALL_EXCEPT); std::cout<<"std::lrint(LONG_MIN-2048.0) = "<< std::lrint(LONG_MIN-2048.0)<<'\n';if(std::fetestexcept(FE_INVALID))std::cout<<"  FE_INVALID was raised\n";}

      Possible output:

      Rounding to nearest (halfway cases to even):  rint(+2.3) = 2  rint(+2.5) = 2  rint(+3.5) = 4  rint(-2.3) = -2  rint(-2.5) = -2  rint(-3.5) = -4Rounding down:  rint(+2.3) = 2  rint(+2.5) = 2  rint(+3.5) = 4  rint(-2.3) = -2  rint(-2.5) = -2  rint(-3.5) = -4Rounding down with lrint:  lrint(+2.3) = 2  lrint(+2.5) = 2  lrint(+3.5) = 3  lrint(-2.3) = -3  lrint(-2.5) = -3  lrint(-3.5) = -4Special values:  lrint(-0.0) = 0  lrint(-Inf) = 0x8000000000000000std::rint(0.1) = 0std::lrint(LONG_MIN-2048.0) = 0x8000000000000000  FE_INVALID was raised

      [edit]See also

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

      [8]ページ先頭

      ©2009-2025 Movatter.jp