Difference between revisions of "cpp/numeric/math/round"
m (Text replace - "{{params}}" to "===Parameters===") |
m (→Possible implementation: fixed: `rint` uses "current rounding mode", e.g. "toward-zero", which issued wrong result in half-way cases by rounding to smaller (by absolute value) integer; e.g., old round(2.5) gave 2.0 instead of 3.0.) |
||
(27 intermediate revisions by 13 users not shown) | |||
Line 1: | Line 1: | ||
− | {{cpp/title|round | lround | llround}} | + | {{cpp/title|round|roundf|roundl|lround|lroundf|lroundl|llround|llroundf|llroundl}} |
− | {{cpp/numeric/math/ | + | {{cpp/numeric/math/navbar}} |
− | {{ | + | {{dcl begin}} |
− | {{ | + | {{dcl header|cmath}} |
− | {{ | + | {{dcl h|Rounding to floating-point types}} |
− | + | {{dcl rev multi|num=1|since1=c++11|dcl1= | |
+ | float round ( float num ); | ||
+ | double round ( double num ); | ||
+ | long double round ( long double num ); | ||
+ | |since2=c++23|dcl2= | ||
+ | constexpr /* floating-point-type */ | ||
+ | round ( /* floating-point-type */ num ); | ||
}} | }} | ||
− | {{ | + | {{dcl|num=2|since=c++11|notes={{mark|constexpr since C++23}}| |
− | + | float roundf( float num ); | |
}} | }} | ||
− | {{ | + | {{dcl|num=3|since=c++11|notes={{mark|constexpr since C++23}}| |
− | + | long double roundl( long double num ); | |
}} | }} | ||
− | {{ | + | {{dcl h|Rounding to {{c/core|long}}}} |
− | long lround( float | + | {{dcl rev multi|num=4|since1=c++11|dcl1= |
+ | long lround ( float num ); | ||
+ | long lround ( double num ); | ||
+ | long lround ( long double num ); | ||
+ | |since2=c++23|dcl2= | ||
+ | constexpr long lround( /* floating-point-type */ num ); | ||
}} | }} | ||
− | {{ | + | {{dcl|num=5|since=c++11|notes={{mark|constexpr since C++23}}| |
− | long | + | long lroundf( float num ); |
}} | }} | ||
− | {{ | + | {{dcl|num=6|since=c++11|notes={{mark|constexpr since C++23}}| |
− | long | + | long lroundl( long double num ); |
}} | }} | ||
− | {{ | + | {{dcl h|Rounding to {{c/core|long long}}}} |
− | long long llround( float | + | {{dcl rev multi|num=7|since1=c++11|dcl1= |
+ | long long llround ( float num ); | ||
+ | long long llround ( double num ); | ||
+ | long long llround ( long double num ); | ||
+ | |since2=c++23|dcl2= | ||
+ | constexpr long long llround( /* floating-point-type */ num ); | ||
}} | }} | ||
− | {{ | + | {{dcl|num=8|since=c++11|notes={{mark|constexpr since C++23}}| |
− | long long | + | long long llroundf( float num ); |
}} | }} | ||
− | {{ | + | {{dcl|num=9|since=c++11|notes={{mark|constexpr since C++23}}| |
− | long long | + | long long llroundl( long double num ); |
}} | }} | ||
− | {{ | + | {{dcl h|[[#Notes|Additional overloads]]}} |
+ | {{dcl header|cmath}} | ||
+ | {{dcl|num=A|since=c++11|notes={{mark|constexpr since C++23}}| | ||
+ | template< class Integer > | ||
+ | double round( Integer num ); | ||
+ | }} | ||
+ | {{dcl|num=B|since=c++11|notes={{mark|constexpr since C++23}}| | ||
+ | template< class Integer > | ||
+ | long lround( Integer num ); | ||
+ | }} | ||
+ | {{dcl|num=C|since=c++11|notes={{mark|constexpr since C++23}}| | ||
+ | template< class Integer > | ||
+ | long long llround( Integer num ); | ||
+ | }} | ||
+ | {{dcl end}} | ||
− | Computes nearest integer to {{ | + | @1-3@ Computes the nearest integer value to {{c|num}} (in floating-point format), rounding halfway cases away from zero, regardless of the current rounding mode.{{rev inl|since=c++23| The library provides overloads of {{tt|std::round}} for all cv-unqualified floating-point types as the type of the parameter {{c|num}}.}} |
+ | @4-9@ Computes the nearest integer value to {{c|num}} (in integer format), rounding halfway cases away from zero, regardless of the current rounding mode.{{rev inl|since=c++23| The library provides overloads of {{tt|std::lround}} and {{tt|std::llround}} for all cv-unqualified floating-point types as the type of the parameter {{c|num}}.}} | ||
+ | @A-C@ Additional overloads are provided for all integer types, which are treated as {{c/core|double}}. | ||
− | |||
===Parameters=== | ===Parameters=== | ||
− | {{ | + | {{par begin}} |
− | {{ | + | {{par|num|floating-point or integer value}} |
− | {{ | + | {{par end}} |
− | + | ===Return value=== | |
− | {{ | + | If no errors occur, the nearest integer value to {{c|num}}, rounding halfway cases away from zero, is returned. |
− | + | {{plot|left=Return value|bottom={{c|num}}|math-round_away_zero.svg}} | |
+ | |||
+ | If a domain error occurs, an implementation-defined value is returned. | ||
+ | |||
+ | ===Error handling=== | ||
+ | Errors are reported as specified in {{lc|math_errhandling}}. | ||
+ | |||
+ | If the result of {{tt|std::lround}} or {{tt|std::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 the {{tt|std::round}} function: | ||
+ | :* The current [[cpp/numeric/fenv/FE_round|rounding mode]] has no effect. | ||
+ | :* If {{c|num}} is ±∞, it is returned, unmodified. | ||
+ | :* If {{c|num}} is ±0, it is returned, unmodified. | ||
+ | :* If {{c|num}} is NaN, NaN is returned. | ||
+ | : For {{tt|std::lround}} and {{tt|std::llround}} functions: | ||
+ | :* {{lc|FE_INEXACT}} is never raised. | ||
+ | :* The current [[cpp/numeric/fenv/FE_round|rounding mode]] has no effect. | ||
+ | :* If {{c|num}} is ±∞, {{lc|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, {{lc|FE_INVALID}} is raised and an implementation-defined value is returned. | ||
+ | :* If {{c|num}} is NaN, {{lc|FE_INVALID}} is raised and an implementation-defined value is returned. | ||
+ | |||
+ | ===Notes=== | ||
+ | {{lc|FE_INEXACT}} may be (but is not required to be) raised by {{tt|std::round}} when rounding a non-integer finite value. | ||
+ | |||
+ | The largest representable floating-point values are exact integers in all standard floating-point formats, so {{tt|std::round}} never overflows on its own; however the result may overflow any integer type (including {{lc|std::intmax_t}}), when stored in an integer variable. | ||
+ | |||
+ | [https://pubs.opengroup.org/onlinepubs/9699919799/functions/lround.html POSIX specifies] that all cases where {{tt|std::lround}} or {{tt|std::llround}} raise {{lc|FE_INEXACT}} are domain errors. | ||
+ | |||
+ | The {{c/core|double}} version of {{tt|std::round}} behaves as if implemented as follows: | ||
+ | {{source|1= | ||
+ | #include <cfenv> | ||
+ | #include <cmath> | ||
+ | |||
+ | #pragma STDC FENV_ACCESS ON | ||
+ | |||
+ | double round(double x) | ||
+ | { | ||
+ | const int save_round = std::fegetround(); | ||
+ | std::fesetround(FE_TOWARDZERO); | ||
+ | const double result = std::rint(std::copysign(0.5 + std::fabs(x), x)); | ||
+ | std::fesetround(save_round); | ||
+ | return result; | ||
+ | } | ||
+ | }} | ||
+ | |||
+ | {{cpp/numeric/math/additional integer overload note|round}} | ||
+ | |||
+ | ===Example=== | ||
+ | {{example | ||
+ | |code= | ||
+ | #include <cassert> | ||
+ | #include <cfenv> | ||
+ | #include <cfloat> | ||
+ | #include <climits> | ||
+ | #include <cmath> | ||
+ | #include <iostream> | ||
+ | |||
+ | // #pragma STDC FENV_ACCESS ON | ||
+ | |||
+ | double custom_round(double x) | ||
+ | { | ||
+ | const int save_round = std::fegetround(); | ||
+ | std::fesetround(FE_TOWARDZERO); | ||
+ | const double result = std::rint(std::copysign(0.5 + std::fabs(x), x)); | ||
+ | std::fesetround(save_round); | ||
+ | return result; | ||
+ | } | ||
+ | |||
+ | void test_custom_round() | ||
+ | { | ||
+ | for (const double 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; | ||
+ | |||
+ | // round | ||
+ | std::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'; | ||
+ | |||
+ | // lround | ||
+ | std::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 handling | ||
+ | std::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"; | ||
+ | } | ||
+ | |p=true | ||
+ | |output= | ||
+ | round(+2.3) = +2 round(+2.5) = +3 round(+2.7) = +3 | ||
+ | round(-2.3) = -2 round(-2.5) = -3 round(-2.7) = -3 | ||
+ | round(-0.0) = -0 | ||
+ | round(-Inf) = -inf | ||
+ | lround(+2.3) = +2 lround(+2.5) = +3 lround(+2.7) = +3 | ||
+ | lround(-2.3) = -2 lround(-2.5) = -3 lround(-2.7) = -3 | ||
+ | lround(-0.0) = +0 | ||
+ | lround(-Inf) = -9223372036854775808 | ||
+ | std::lround(LONG_MAX+1.5) = -9223372036854775808 | ||
+ | FE_INVALID was raised | ||
+ | }} | ||
− | + | ===See also=== | |
− | {{see | + | {{dsc begin}} |
+ | {{dsc inc|cpp/numeric/math/dsc floor}} | ||
+ | {{dsc inc|cpp/numeric/math/dsc ceil}} | ||
+ | {{dsc inc|cpp/numeric/math/dsc trunc}} | ||
+ | {{dsc see c|c/numeric/math/round}} | ||
+ | {{dsc end}} | ||
− | {{ | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
− | + | ||
− | + | ||
− | + | ||
− | + |
Latest revision as of 13:13, 11 February 2024
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) |
long double roundl( long double num ); |
(3) | (since C++11) (constexpr since C++23) |
Rounding to long |
||
(4) | ||
long lround ( float num ); long lround ( double num ); |
(since C++11) (until C++23) |
|
constexpr long lround( /* floating-point-type */ num ); |
(since C++23) | |
long lroundf( float num ); |
(5) | (since C++11) (constexpr since C++23) |
long lroundl( long double num ); |
(6) | (since C++11) (constexpr since C++23) |
Rounding to long long |
||
(7) | ||
long long llround ( float num ); long long llround ( double num ); |
(since C++11) (until C++23) |
|
constexpr long long llround( /* floating-point-type */ num ); |
(since C++23) | |
long long llroundf( float num ); |
(8) | (since C++11) (constexpr since C++23) |
long long llroundl( long double 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 > long long 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 parameter num.(since C++23)std::lround
and std::llround
for all cv-unqualified floating-point types as the type of the parameter num.(since C++23)Contents |
[edit] Parameters
num | - | floating-point or integer value |
[edit] Return value
If no errors occur, the nearest integer value to num, rounding halfway cases away from zero, is returned.
If a domain error occurs, an implementation-defined value is returned.
[edit] Error handling
Errors are reported as specified in math_errhandling.
If the result of std::lround
or std::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 the
std::round
function:- The current rounding mode has no effect.
- If num is ±∞, it is returned, unmodified.
- If num is ±0, it is returned, unmodified.
- If num is NaN, NaN is returned.
- For
std::lround
andstd::llround
functions:- FE_INEXACT is never raised.
- The current rounding mode has no effect.
- If num 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.
- If num 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 by std::round
when rounding a non-integer finite value.
The largest representable floating-point values are exact integers in all standard floating-point formats, so std::round
never overflows on its own; however the result may overflow any integer type (including std::intmax_t), when stored in an integer variable.
POSIX specifies that all cases where std::lround
or std::llround
raise FE_INEXACT are domain errors.
The double version of std::round
behaves as if implemented as follows:
#include <cfenv> #include <cmath> #pragma STDC FENV_ACCESS ON double round(double x) { const int save_round = std::fegetround(); std::fesetround(FE_TOWARDZERO); const double 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 argument num of integer type:
- std::round(num) has the same effect as std::round(static_cast<double>(num)).
- std::lround(num) has the same effect as std::lround(static_cast<double>(num)).
- std::llround(num) has the same effect as std::llround(static_cast<double>(num)).
[edit] Example
#include <cassert> #include <cfenv> #include <cfloat> #include <climits> #include <cmath> #include <iostream> // #pragma STDC FENV_ACCESS ON double custom_round(double x) { const int save_round = std::fegetround(); std::fesetround(FE_TOWARDZERO); const double result = std::rint(std::copysign(0.5 + std::fabs(x), x)); std::fesetround(save_round); return result; } void test_custom_round() { for (const double 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; // round std::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'; // lround std::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 handling std::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) = +3 round(-2.3) = -2 round(-2.5) = -3 round(-2.7) = -3 round(-0.0) = -0 round(-Inf) = -inf lround(+2.3) = +2 lround(+2.5) = +3 lround(+2.7) = +3 lround(-2.3) = -2 lround(-2.5) = -3 lround(-2.7) = -3 lround(-0.0) = +0 lround(-Inf) = -9223372036854775808 std::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) |
(C++11)(C++11) |
nearest integer not less than the given value (function) |
(C++11)(C++11)(C++11) |
nearest integer not greater in magnitude than the given value (function) |
C documentation for round
|