Skip to content

the value is error from std::nearbyint with FE_UPWARD when using clang -O2 #56509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chumarshal1 opened this issue Jul 13, 2022 · 1 comment
Closed
Labels
llvm:optimizations question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

Comments

@chumarshal1
Copy link

chumarshal1 commented Jul 13, 2022

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

int main()
{           
    std::fesetround(FE_UPWARD); 
    float v = 0.125;
    float v1 = (float)std::nearbyint((float)(0.125));
    float v2 = (float)std::rint ((float)(0.125));

    std::cout << "nearbyint(0.125) = " << v1 << std::endl;
    std::cout << "rint(0.125)      = " << v2 << std::endl;
    return (v1+v2);
}
$ clang++ test.cpp
nearbyint(0.125) = 1
rint(0.125)      = 1
$ clang++ -O2 test.cpp
nearbyint(0.125) = 0
rint(0.125)      = 0

As above show, the result using -O2 is different from using -O0.

@rotateright
Copy link
Contributor

I think the code as-is is invalid. See discussion in issue #8472.

You need to use "-frounding-math" (or "-ffp-model=strict" if you're expecting exceptions to be registered/preserved).

Feel free to re-open or file other bugs if things are still not working as expected with the FP environment correctly specified.

@EugeneZelenko EugeneZelenko added the question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead! label Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:optimizations question A question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!
Projects
None yet
Development

No branches or pull requests

3 participants