Search code examples
c++floating-pointintegercomparison-operators

C++ comparison of integers with float


I have the following code.that demonstrates the problem I am having. How come the comparison is not evaluating to true? Is this a limitation of integer and floating point comparisons?

#include <iostream>

int main(){

    double m = -0.625;
    if((-1)<=m<=0){
        std::cout << "Enter here" <<std::endl;
    }
    return 0;
}

Solution

  • You can't do three way comparison like that in C. Change it to the following and it'll work:

    if((-1) <= m && m <= 0){
            std::cout << "Enter here" <<std::endl;
        }