Search code examples
c++gccfloating-pointrounding

Why does gcc -O1 affects std::rint()?


Why does the following code produce -1 when compiled using g++ -O1? My gcc version is 13.2.1.

#include <iostream>
#include <cmath>
#include <cfenv>

using namespace std;

int main() {
    fesetround(FE_DOWNWARD);
    cout << rint(-1.6) << endl;
    return 0;
}

Solution

  • For GCC, You should compile your program with -frounding-math if your program may change the FP rounding mode dynamically, or may be executed with a non-default rounding mode

    godbolt