Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::round,std::roundf,std::roundl,std::lround,std::lroundf,std::lroundl,std::llround,std::llroundf

      From cppreference.com
      <cpp‎ |numeric‎ |math
       
       
       
      Common mathematical functions
      Nearest integer floating point operations
      roundlroundllround
      (C++11)(C++11)(C++11)
      (C++11)
      (C++11)
      (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       round(float num);

      double      round(double num);

      longdouble round(longdouble num);
      (since C++11)
      (until C++23)
      constexpr/* floating-point-type */
                  round(/* floating-point-type */ num);
      (since C++23)
      float       roundf(float num);
      (2)(since C++11)
      (constexpr since C++23)
      longdouble roundl(longdouble num);
      (3)(since C++11)
      (constexpr since C++23)
      Rounding tolong
      (4)
      long lround(float num);

      long lround(double num);

      long lround(longdouble num);
      (since C++11)
      (until C++23)
      constexprlong lround(/* floating-point-type */ num);
      (since C++23)
      long lroundf(float num);
      (5)(since C++11)
      (constexpr since C++23)
      long lroundl(longdouble num);
      (6)(since C++11)
      (constexpr since C++23)
      Rounding tolonglong
      (7)
      longlong llround(float num);

      longlong llround(double num);

      longlong llround(longdouble num);
      (since C++11)
      (until C++23)
      constexprlonglong llround(/* floating-point-type */ num);
      (since C++23)
      longlong llroundf(float num);
      (8)(since C++11)
      (constexpr since C++23)
      longlong llroundl(longdouble num);
      (9)(since C++11)
      (constexpr since C++23)
      Defined in header<cmath>
      template<class Integer>
      double round( Integer num);
      (A)(since C++11)
      (constexpr since C++23)
      template<class Integer>
      long lround( Integer num);
      (B)(since C++11)
      (constexpr since C++23)
      template<class Integer>
      longlong llround( Integer num);
      (C)(since C++11)
      (constexpr since C++23)
      1-3) Computes the nearest integer value tonum (in floating-point format), rounding halfway cases away from zero, regardless of the current rounding mode. The library provides overloads ofstd::round for all cv-unqualified floating-point types as the type of the parameternum.(since C++23)
      4-9) Computes the nearest integer value tonum (in integer format), rounding halfway cases away from zero, regardless of the current rounding mode. The library provides overloads ofstd::lround andstd::llround 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, rounding halfway cases away from zero, is returned.

      Return value
      math-round away zero.svg
      num

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

      [edit]Error handling

      Errors are reported as specified inmath_errhandling.

      If the result ofstd::lround orstd::llround 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::round function:
      • The currentrounding mode has no effect.
      • Ifnum is ±∞, it is returned, unmodified.
      • Ifnum is ±0, it is returned, unmodified.
      • Ifnum is NaN, NaN is returned.
      Forstd::lround andstd::llround functions:
      • FE_INEXACT is never raised.
      • The currentrounding mode has no effect.
      • 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

      FE_INEXACT may be (but is not required to be) raised bystd::round when rounding a non-integer finite value.

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

      POSIX specifies that all cases wherestd::lround orstd::llround raiseFE_INEXACT are domain errors.

      Thedouble version ofstd::round behaves as if implemented as follows:

      #include <cfenv>#include <cmath> #pragma STDC FENV_ACCESS ON double round(double x){constint save_round=std::fegetround();std::fesetround(FE_TOWARDZERO);constdouble result=std::rint(std::copysign(0.5+std::fabs(x), x));std::fesetround(save_round);return result;}

      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::round(num) has the same effect asstd::round(static_cast<double>(num)).
      • std::lround(num) has the same effect asstd::lround(static_cast<double>(num)).
      • std::llround(num) has the same effect asstd::llround(static_cast<double>(num)).

      [edit]Example

      Run this code
      #include <cassert>#include <cfenv>#include <cfloat>#include <climits>#include <cmath>#include <iostream> // #pragma STDC FENV_ACCESS ON double custom_round(double x){constint save_round=std::fegetround();std::fesetround(FE_TOWARDZERO);constdouble result=std::rint(std::copysign(0.5+std::fabs(x), x));std::fesetround(save_round);return result;} void test_custom_round(){for(constdouble x:{0.0,0.3,0.5-DBL_EPSILON/2,0.5,0.5+DBL_EPSILON/2,0.7,1.0,2.3,2.5,2.7,3.0,static_cast<double>(INFINITY)})assert(round(+x)== custom_round(+x)&& round(-x)== custom_round(-x));} int main(){    test_custom_round(); std::cout<<std::showpos; // roundstd::cout<<"round(+2.3) = "<< std::round(2.3)<<"  round(+2.5) = "<< std::round(2.5)<<"  round(+2.7) = "<< std::round(2.7)<<'\n'<<"round(-2.3) = "<< std::round(-2.3)<<"  round(-2.5) = "<< std::round(-2.5)<<"  round(-2.7) = "<< std::round(-2.7)<<'\n'; std::cout<<"round(-0.0) = "<< std::round(-0.0)<<'\n'<<"round(-Inf) = "<< std::round(-INFINITY)<<'\n'; // lroundstd::cout<<"lround(+2.3) = "<< std::lround(2.3)<<"  lround(+2.5) = "<< std::lround(2.5)<<"  lround(+2.7) = "<< std::lround(2.7)<<'\n'<<"lround(-2.3) = "<< std::lround(-2.3)<<"  lround(-2.5) = "<< std::lround(-2.5)<<"  lround(-2.7) = "<< std::lround(-2.7)<<'\n'; std::cout<<"lround(-0.0) = "<< std::lround(-0.0)<<'\n'<<"lround(-Inf) = "<< std::lround(-INFINITY)<<'\n'; // error handlingstd::feclearexcept(FE_ALL_EXCEPT); std::cout<<"std::lround(LONG_MAX+1.5) = "<< std::lround(LONG_MAX+1.5)<<'\n';if(std::fetestexcept(FE_INVALID))std::cout<<"    FE_INVALID was raised\n";}

      Possible output:

      round(+2.3) = +2  round(+2.5) = +3  round(+2.7) = +3round(-2.3) = -2  round(-2.5) = -3  round(-2.7) = -3round(-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) = -9223372036854775808std::lround(LONG_MAX+1.5) = -9223372036854775808    FE_INVALID was raised

      [edit]See also

      (C++11)(C++11)
      nearest integer not greater than the given value
      (function)[edit]
      (C++11)(C++11)
      nearest integer not less than the given value
      (function)[edit]
      (C++11)(C++11)(C++11)
      nearest integer not greater in magnitude than the given value
      (function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/math/round&oldid=181908"

      [8]ページ先頭

      ©2009-2025 Movatter.jp