# 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:

 #include #include #include float roundHalfToEven(float f) { const float r = round(f); // Result is round-half-away-from-zero const float d = r – f; // Difference // Result is not half, RHAFZ result same as RHTE if ((d != 0.5f) && (d != –0.5f)) { return r; } // Check if RHAFZ result is even, then RHAFZ result same as RHTE if (fmod(r, 2.0f) == 0.0f) { return r; } // Switch to even value return f – d; } int main() { const std::array fvals {{-5.5, –4.5, –3.5, –0.5, 0.5, 3.5, 4.5, 5.5}}; for (float f : fvals) { const float r = roundHalfToEven(f); std::cout << "roundHalfToEven(" << f << "): " << r << std::endl; } return 0; }

view raw
roundHalfToEven.cpp
hosted with ❤ by GitHub

## 2 thoughts on “How to round half to even”

1. Steve says:

std::nearbyint and std::rint both round to even in halfway cases (when the current rounding mode is FE_TONEAREST).

Like

1. Steve: Thanks! I wasn’t aware of these other rounding methods. I have updated my post now.

Like

This site uses Akismet to reduce spam. Learn how your comment data is processed.