Common mathematical functions | |||||||||||||||||||||||||||||||
Mathematical special functions(C++17) | |||||||||||||||||||||||||||||||
Mathematical constants(C++20) | |||||||||||||||||||||||||||||||
Basic linear algebra algorithms(C++26) | |||||||||||||||||||||||||||||||
Data-parallel types (SIMD)(C++26) | |||||||||||||||||||||||||||||||
Floating-point environment(C++11) | |||||||||||||||||||||||||||||||
Complex numbers | |||||||||||||||||||||||||||||||
Numeric array (valarray ) | |||||||||||||||||||||||||||||||
Pseudo-random number generation | |||||||||||||||||||||||||||||||
Bit manipulation(C++20) | |||||||||||||||||||||||||||||||
Saturation arithmetic(C++26) | |||||||||||||||||||||||||||||||
Factor operations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Interpolations | |||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||
Generic numeric operations | |||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||
C-style checked integer arithmetic | |||||||||||||||||||||||||||||||
|
Nearest integer floating point operations | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
Floating point manipulation functions | |||||||||||||||||||||||||||||||||||||||||
|
| ||||||||||||||||||||||||||||||||||||||||
Classification and comparison | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
Types | |||||||||||||||||||||||||||||||||||||||||
Macro constants | |||||||||||||||||||||||||||||||||||||||||
|
|
Defined in header <cmath> | ||
Rounding to floating-point types | ||
(1) | ||
float round(float num); double round(double 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); | (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); | (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) |
std::round
for all cv-unqualified floating-point types as the type of the parameternum.(since C++23)std::lround
andstd::llround
for all cv-unqualified floating-point types as the type of the parameternum.(since C++23)Contents |
num | - | floating-point or integer value |
If no errors occur, the nearest integer value tonum, rounding halfway cases away from zero, is returned.
If a domain error occurs, an implementation-defined value is returned.
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),
std::round
function:std::lround
andstd::llround
functions: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:
#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
(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] |
C documentation forround |