Can you compare floating point values exactly to zero?

后端 未结 8 1084
一生所求
一生所求 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:43

    Even though 0 has an exact representation, you can't rely on the result of a calculation using floats to be exactly 0. As you noted, this is due to floating point calculation and conversion issues.

    So, you should test for 0 against your tolerance epsilon.

提交回复
热议问题