Can you compare floating point values exactly to zero?

后端 未结 8 1327
一生所求
一生所求 2020-12-18 17:57

I know we can\'t compare 2 floating point values using ==. We can only compare they are within some interval of each other. I know

if(val == 0.512)
         


        
8条回答
  •  一生所求
    2020-12-18 18:44

    I don't think you can in general - the calculation that occurs in getVal() might logically result in zero but that dosn't mean it will return zero. If you explicilty return a zero to indicate some condition then the compare should always work but I don't think it would be best practice. I'd modify the function to return a status code and pass the value to be changed byref.

提交回复
热议问题