std::rint, std::rintf, std::rintl, std::lrint, std::lrintf, std::lrintl, std::llrint, std::llrintf
Header: <cmath>
1-3) Rounds the floating-point argument num to an integer value (in floating-point format), using the current rounding mode.The library provides overloads of std::rint for all cv-unqualified floating-point types as the type of the parameter num.(since C++23)
# Declarations
Rounding to floating-point types
float rint ( float num );
double rint ( double num );
long double rint ( long double num );
(since C++11) (until C++23)
/* floating-point-type */ rint( /* floating-point-type */ num );
(since C++23)
float rintf( float num );
(since C++11)
long double rintl( long double num );
(since C++11)
Rounding to long
long lrint ( float num );
long lrint ( double num );
long lrint ( long double num );
(since C++11) (until C++23)
long lrint ( /* floating-point-type */ num );
(since C++23)
long lrintf( float num );
(since C++11)
long lrintl( long double num );
(since C++11)
Rounding to long long
long long llrint ( float num );
long long llrint ( double num );
long long llrint ( long double num );
(since C++11) (until C++23)
long long llrint ( /* floating-point-type */ num );
(since C++23)
long long llrintf( float num );
(since C++11)
long long llrintl( long double num );
(since C++11)
Additional overloads
template< class Integer >
double rint( Integer num );
(since C++11)
template< class Integer >
long lrint( Integer num );
(since C++11)
template< class Integer >
long long llrint( Integer num );
(since C++11)
# Parameters
num: floating-point or integer value
# Return value
If no errors occur, the nearest integer value to num, according to the current rounding mode, is returned.
# Notes
POSIX specifies that all cases where std::lrint or std::llrint raise FE_INEXACT are domain errors.
As specified in math_errhandling, FE_INEXACT may be (but isn’t required to be on non-IEEE floating-point platforms) raised by std::rint when rounding a non-integer finite value.
The only difference between std::rint and std::nearbyint is that std::nearbyint never raises FE_INEXACT.
The largest representable floating-point values are exact integers in all standard floating-point formats, so std::rint never overflows on its own; however the result may overflow any integer type (including std::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 argument num of integer type:
# Example
#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 handling
std::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";
}