Defined in header
<cmath> | ||
---|---|---|
float nearbyint( float arg ); | (1) | (since C++11) |
double nearbyint( double arg ); | (2) | (since C++11) |
long double nearbyint( long double arg ); | (3) | (since C++11) |
double nearbyint( Integral arg ); | (4) | (since C++11) |
arg
to an integer value in floating-point format, using the current rounding mode.double
).arg | - | floating point value |
The nearest integer value to arg
, according to the current rounding mode, is returned.
This function is not subject to any of the errors specified in math_errhandling.
If the implementation supports IEEE floating-point arithmetic (IEC 60559),
FE_INEXACT
is never raised arg
is ±∞, it is returned, unmodified arg
is ±0, it is returned, unmodified arg
is NaN, NaN is returned The only difference between std::nearbyint
and std::rint
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::nearbyint
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 FE_TONEAREST
, this function rounds to even in halfway cases (like rint
, but unlike round
).
#include <iostream> #include <cmath> #include <cfenv> int main() { #pragma STDC FENV_ACCESS ON 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
(C++11)(C++11)(C++11)
| nearest integer using current rounding mode with exception if the result differs (function) |
(C++11)(C++11)(C++11)
| nearest integer, rounding away from zero in halfway cases (function) |
(C++11)(C++11)
| gets or sets rounding direction (function) |
C documentation for nearbyint |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/numeric/math/nearbyint