Search code examples
c++c++11rounding-errorrounding

C++ rounding to FE_TONEAREST


Can anyone, please, explain why rounding 0.5 to FE_TONEAREST is giving 0? Shouldn't it give `1 as result? Is there a way to "fix" this?

#include <fenv.h>
#include <iostream>
#include <cmath>

int main() {
    fesetround(FE_TONEAREST);
    std::cout << "Rounding 0.5 to nearest = " << std::rint(0.5) << std::endl;
    return 0;
}

Runnable code on coliru: http://coliru.stacked-crooked.com/a/9c179ca56f251628


Solution

  • FE_TONEAREST causes std::rint to round halfway cases (±n.5) to the nearest even integer value. 0 is even.

    If you were to instead use std::round with this mode, halfway cases round "up" (away from zero), and this would result in 1.

    See documentation for std::rint.

    Why you might want this unusual form of rounding is explained over on Mathematica.SE. (so your data doesn't all get shifted in one direction).

    As an aside, C++ provides properly wrapped version of standard headers, with names added to the std namespace (although that hardly matters here): it is usual to #include <cfenv> rather than #include <fenv.h>.