iphone / Objective C - Comparing doubles not working

后端 未结 2 1165
庸人自扰
庸人自扰 2020-12-03 17:53

I think I\'m going insane. \"counter\" and \"interval\" are both doubles. This is happening on accelerometer:didAccelerate at an interval of (.01) . \"counter\" should even

相关标签:
2条回答
  • 2020-12-03 18:26

    In your else block, you are not adding 0.01 to counter, because that is not a representable double-precision value. You are actually adding the value:

    0.01000000000000000020816681711721685132943093776702880859375
    

    Unsurprisingly, when you repeatedly add this value to itself, you never get 0.5 exactly.

    Two options: the better is to replace the if condition with (counter >= interval). Alternatively, you could use a small power of two for the increment instead of something that cannot be represented, like 0.0078125.

    0 讨论(0)
  • 2020-12-03 18:35

    Don't ever compare doubles or floats with equality - they might look the same at the number of significant figures your are examining but the computer sees more.

    For this purpose, the Foundation Framework provides "epsilon" values for different types such as "float" and "double". If the distance between two numbers is smaller than epsilon, you can assume these two numbers are equal.

    In your case, you would use it as follow:

    - (BOOL)firstDouble:(double)first isEqualTo:(double)second {
        if(fabs(first - second) < DBL_EPSILON)
            return YES;
        else
            return NO;
    }
    

    Or in Swift 4:

    func doublesAreEqual(first: Double, second: Double) -> Bool {
        if fabs(first - second) < .ulpOfOne {
            return true
        }
        return false
    }
    

    Two very useful links:

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    Interesting discussion of Unit in Last Place (ULP) and usage in Swift

    Friday Q&A 2011-01-04: Practical Floating Point

    0 讨论(0)
提交回复
热议问题