Skip to content

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

Closed
@chumarshal1

Description

@chumarshal1
#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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    llvm:optimizationsquestionA question, not bug report. Check out https://llvm.org/docs/GettingInvolved.html instead!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions