From cppreference.com
Defined in header
floatnearbyint(floatnum);doublenearbyint(doublenum);longdoublenearbyint(longdoublenum); (1)(until C++23)/*floating-point-type*/nearbyint(/*floating-point-type*/num);(since C++23)floatnearbyintf(floatnum); (2)(since C++11)longdoublenearbyintl(longdoublenum); (3)(since C++11)
(since C++26)
Defined in header
template</*math-floating-point*/V>constexpr/*deduced-simd-t*/<V>nearbyint(constV&v_num); (S) (since C++26)
(since C++11)
Defined in header
template<classInteger>doublenearbyint(Integernum); (A)1-3) Rounds the floating-point argument num to an integer value in floating-point format, using the
. The library provides overloads of std::nearbyint for all cv-unqualified floating-point types as the type of the parameter.(since C++23)
A) Additional overloads are provided for all integer types, which are treated as double.
(since C++11)Parameters
num - floating-point or integer value Return value
The nearest integer value to num, according to the
, is returned.
Error handling
This function is not subject to any of the errors specified in
.
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
is never raised.
If num is ±∞, it is returned, unmodified.
If num is ±0, it is returned, unmodified.
If num is NaN, NaN is returned.
Notes
The only difference between std::nearbyint and
is that std::nearbyint never raises
.
The largest representable floating-point values are exact integers in all standard floating-point formats, so std::nearbyint never overflows on its own; however the result may overflow any integer type (including
), when stored in an integer variable.
If the current rounding mode is
, this function rounds to even in halfway cases (like
, but unlike
).
The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their argument num of integer type, std::nearbyint(num) has the same effect as std::nearbyint(static_cast<double>(num)).
Example
Run this code
#include<cfenv>#include<cmath>#include<iostream>#pragma STDC FENV_ACCESS ONintmain(){std::fesetround(FE_TONEAREST);std::cout<<"rounding to nearest: \n"<<"nearbyint(+2.3) = "<<std::nearbyint(2.3)<<" nearbyint(+2.5) = "<<std::nearbyint(2.5)<<" nearbyint(+3.5) = "<<std::nearbyint(3.5)<<'\n'<<"nearbyint(-2.3) = "<<std::nearbyint(-2.3)<<" nearbyint(-2.5) = "<<std::nearbyint(-2.5)<<" nearbyint(-3.5) = "<<std::nearbyint(-3.5)<<'\n';std::fesetround(FE_DOWNWARD);std::cout<<"rounding down:\n"<<"nearbyint(+2.3) = "<<std::nearbyint(2.3)<<" nearbyint(+2.5) = "<<std::nearbyint(2.5)<<" nearbyint(+3.5) = "<<std::nearbyint(3.5)<<'\n'<<"nearbyint(-2.3) = "<<std::nearbyint(-2.3)<<" nearbyint(-2.5) = "<<std::nearbyint(-2.5)<<" nearbyint(-3.5) = "<<std::nearbyint(-3.5)<<'\n';std::cout<<"nearbyint(-0.0) = "<<std::nearbyint(-0.0)<<'\n'<<"nearbyint(-Inf) = "<<std::nearbyint(-INFINITY)<<'\n';}Output:
rounding to nearest: nearbyint(+2.3) = 2 nearbyint(+2.5) = 2 nearbyint(+3.5) = 4 nearbyint(-2.3) = -2 nearbyint(-2.5) = -2 nearbyint(-3.5) = -4 rounding down: nearbyint(+2.3) = 2 nearbyint(+2.5) = 2 nearbyint(+3.5) = 3 nearbyint(-2.3) = -3 nearbyint(-2.5) = -3 nearbyint(-3.5) = -4 nearbyint(-0.0) = -0 nearbyint(-Inf) = -inf See also