Strange problem comparing floats in objective-C

后端 未结 7 2332
别跟我提以往
别跟我提以往 2020-11-27 16:25

At some point in an algorithm I need to compare the float value of a property of a class to a float. So I do this:

if (self.scroller.currentValue <= 0.1)          


        
7条回答
  •  有刺的猬
    2020-11-27 17:24

    0.1 is actually a very dificult value to store binary. In base 2, 1/10 is the infinitely repeating fraction

    0.0001100110011001100110011001100110011001100110011...
    

    As several has pointed out, the comparison has to made with a constant of the exact same precision.

提交回复
热议问题