How to round half to even

The round function available in C and C++ uses the rounding mode called round half away from zero. In this mode, the floating point value is rounded to the nearest integer value. When the floating point value is exactly midway, then the integer value away from zero is picked.

Many applications require the rounding mode to be round half to even. In this mode, when the floating point value is midway, the integer value that is even is chosen.

If you need round half to even, then you use the nearbyint or rint methods and set the rounding mode to FE_TONEAREST before you use them. For more information on these rounding modes, see this post.

#include <cfenv>
#include <cmath>
std::fesetround(FE_TONEAREST); // This is the default rounding mode for std::nearbyint
std::nearbyint(3.5f); // Result is 4
std::nearbyint(4.5f); // Result is 4

FE_TONEAREST is the default rounding mode, so you do not need to actually set it. Unless you believe it was set to some other rounding mode earlier in your code.

If you wish to do this rounding manually, then here is one possible implementation with test code:

Advertisements