std::round,...td::lroundl,(3)
π
<cmath> round girlies β bestie edition π
okay so this is the <cmath> rounding page from the C++ Standard Libary manual, aka std::round,...td::lroundl,(3). weβre talking about the whole squad: std::round, std::roundf, std::roundl, std::lround, std::lroundf, std::lroundl β and their llround cousins too. they all snap numbers to the nearest integer, but the return types and edge cases are where the plot twists live. no cap. π«Ά
πΈ NAME πΈ
std::round, std::roundf, std::roundl, std::lround, std::lroundf, std::lroundl β and yep, the NAME line repeats the whole set exactly like this: std::round,std::roundf,std::roundl,std::lround,std::lroundf,std::lroundl, π
β¨ Synopsis β¨
Defined in header <cmath>
Rounding to floating-point types
float round ( float num ); (1)
double round ( double num ); (since C++11) (until C++23)
long double round ( long double num );
constexpr /* floating-point-type */ round ( /* floating-point-type */ num ); (since C++23)
float roundf( float num ); (since C++11) (2) (constexpr since C++23)
long double roundl( long double num ); (since C++11) (3) (constexpr since C++23)
Rounding to long
long lround ( float num ); (4)
long lround ( double num ); (since C++11) (until C++23)
long lround ( long double num );
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
long long llround ( float num ); (7)
long long llround ( double num ); (since C++11) (until C++23)
long long llround ( long double num );
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)
Additional overloads
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)
okay so what do the labels mean? (1)β(3) are the floating-point-returning squad: round, roundf, roundl. (4)β(9) are the integer-returning squad: lround, lroundf, lroundl, llround, llroundf, llroundl. and (A)β(C) are the extra integer overloads. slay. π
1-3) Computes the nearest integer value to num (in floating-point format), rounding halfway cases away from zero, regardless of the current rounding mode.
The library provides overloads of std::round for all cv-unqualified floating-point types as the type of the parameter num. (since C++23)
4-9) Computes the nearest integer value to num (in integer format), rounding halfway cases away from zero, regardless of the current rounding mode.
The library provides overloads of std::lround and std::llround for all cv-unqualified floating-point types as the type of the parameter num. (since C++23)
A-C) Additional overloads are provided for all integer types, which are treated as double.
π Parameters π
numβ floating-point or integer value π
π Return value π
If no errors occur, the nearest integer value to num, rounding halfway cases away from zero, is returned. periodt. β¨
π Return value (the sequel, because the manpage said so) π
the original drops a lil diagram reference here: math-round away zero.svg over num. π¦
If a domain error occurs, an implementation-defined value is returned.
π¨ 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), then listen up bestie:
For the std::round function:
- The current rounding mode has no effect.
- If
numis Β±β, it is returned, unmodified. - If
numis Β±0, it is returned, unmodified. - If
numis NaN, NaN is returned.
For std::lround and std::llround functions:
FE_INEXACTis never raised.- The current rounding mode has no effect.
- If
numis Β±β,FE_INVALIDis raised and an implementation-defined value is returned. - If the result of the rounding is outside the range of the return type,
FE_INVALIDis raised and an implementation-defined value is returned. - If
numis NaN,FE_INVALIDis raised and an implementation-defined value is returned.
π 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 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)).
π» 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)
{
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
π See also π
floorβ nearest integer not greater than the given value (function)floorfβ (function) (C++11)floorlβ (function) (C++11)ceilβ nearest integer not less than the given value (function)ceilfβ (function) (C++11)ceillβ (function) (C++11)truncβ nearest integer not greater in magnitude than the given value (C++11) (function)truncfβ (C++11) (function)trunclβ (C++11) (function)- C documentation for
round
and thatβs the whole page, bestie π«Ά source: http://cppreference.com Β· version/date: 2024.06.10 Β· manual page: std::round,...td::lroundl,(3). now go round some numbers, but keep an eye on your ranges π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms