In C++, std::nearbyint(x) rounds to a whole number following the current rounding mode of the program. In the default mode it rounds to the nearest number, and when a value is exactly halfway it goes to the even neighbour. So 2.5 becomes 2 and 3.5 becomes 4.
This way of splitting halves is called "banker's rounding". It stops results from drifting up in long sums. You can change the mode with std::fesetround() from <cfenv>. It is the same as rint(), except that it never raises the "inexact" floating-point flag.
std::nearbyint() is declared in <cmath> and is part of the group Rounding. Rounding functions turn a fractional number into a whole number. They differ in which way they go for negative numbers and for ties such as 2.5.
Syntax
Include <cmath> and call std::nearbyint. Start with these lines.
C++
double a = std::nearbyint(2.5); // double version
float b = std::nearbyint(2.5f); // float version
Call
What it does
std::nearbyint(2.5)
Passes a double and returns a double. Here the result is 2.
std::nearbyint(2.5f)
Passes a float, so the float overload runs and returns a float.
std::nearbyint(3)
An integer is converted to double. Returns 3 as a double.
Write std::nearbyint so the C++ overloads for float, double, and long double are used.
Library declarations
C++
float nearbyint(float x);
double nearbyint(double x);
long double nearbyint(long double x);
double nearbyint(Integer x); // for integer types
Parameters
Parameter
Description
x
Required. The number to round. It can be float, double, long double, or an integer type (converted to double).
Return Value
Return Value
Description
Rounded value
A whole number as a floating-point type, chosen by the current rounding mode.
x
If x is already whole, inf, or NaN, it is returned as it is.
No exception
Unlike rint(), it does not raise FE_INEXACT.
C++ Examples
Example 1 — Round Several Numbers
Round some numbers. The halves go to the even neighbour in the default mode.
C++
#include <cmath>
#include <iostream>
int main() {
double values[] = {2.4, 2.5, 3.5, -2.5};
for (double v : values) {
std::cout << "nearbyint(" << v << ") = " << std::nearbyint(v) << '\n';
}
return 0;
}
Each Mode holds a name and a constant such as FE_UPWARD from <cfenv>. fesetround() switches the program to that mode. The volatile variables stop the compiler from working out the answer early, so the mode is really used.
"To nearest" gives 2 and -2, the even neighbours. "Upward" gives 3 and -2, both the bigger number. "Downward" gives 2 and -3.
"Toward zero" cuts the halves and gives 2 and -2. The last line puts the mode back to the default, which is a good habit.
Example 3 — Why Banker's Rounding Helps in Sums
Add up four values that all end in .5. Normal half-up rounding drifts upward, while nearbyint() stays close to the real sum.
The three variables add the original numbers, the round() results, and the nearbyint() results.
round() turns the four halves into 1, 2, 3, and 4. They add up to 10, which is too high by 2.
nearbyint() gives 0, 2, 2, and 4, which add up to 8. Sometimes up and sometimes down, so the errors cancel out.
Frequently Asked Questions
std::nearbyint(x) rounds x to a whole number using the current rounding mode. The default mode rounds to the nearest, and halves go to the even number, so nearbyint(2.5) is 2.
The rounded value as a floating-point number. The result depends on the rounding mode set with std::fesetround(), and no inexact exception is raised.
They give the same rounded value. The difference is that rint() may raise the "inexact" floating-point exception when the result differs from the input, and nearbyint() never does.
The default rounding mode picks the even number when a value is exactly halfway. 2 is even and 3 is odd, so 2.5 goes to 2. 3.5 goes to 4.
Include <cmath> and write std::nearbyint(). That always works and picks the C++ overloads. The global name ::nearbyint often works too, but the standard does not promise it, and it may only take a double.