Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 382
Maxim Galushka Avatar asked Dec 13 '25 05:12

Maxim Galushka


1 Answers

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>.

like image 107
BoBTFish Avatar answered Dec 15 '25 19:12

BoBTFish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!