Is relational comparison between int and float directly possible in C?

后端 未结 11 2094
Happy的楠姐
Happy的楠姐 2020-12-03 18:48

I am using Visual Studio 6 with some old time code written in c. I found an issue where the code looks like this..

int x = 3;
float y = 3.0;

if(x == y){
            


        
11条回答
  •  南方客
    南方客 (楼主)
    2020-12-03 19:27

    For your specific example, "do some crazy stuff" will execute. 3.0 will not be 3.0000001 at run-time.

    The other answers are more for general cases, but even a hardcoded epsilon is not the greatest idea in the world. A dynamic epsilon based on the actual numbers involved is much better since the more positive and more negative the numbers are, the less likely the hardcoded epsilon will be relevant.

提交回复
热议问题