开发者

iphone / Objective C - Comparing doubles not working

开发者 https://www.devze.com 2023-02-05 05:10 出处:网络
I think I\'m going insane. \"counter\" and \"开发者_如何学Gointerval\" are both doubles. This is happening on accelerometer:didAccelerate at an interval of (.01) . \"counter\" should eventually incre

I think I'm going insane. "counter" and "开发者_如何学Gointerval" are both doubles. This is happening on accelerometer:didAccelerate at an interval of (.01) . "counter" should eventually increment to "interval". For some reason i cant get this "if" to ring true.

Am I overlooking something?

double interval = .5;
 if( counter == interval ){ //should eventually be .50000 == .50000
  NSLog( @"Hit!" );
  [self playSound];
  counter = 0;
 }else{
  counter += .01;
 }
NSLog( @"%f, %f, %d",counter,interval,(counter == interval) );


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


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

精彩评论

暂无评论...
验证码 换一张
取 消