Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iphone / Objective C - Comparing doubles not working

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 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) );
like image 346
azeven Avatar asked Jan 19 '11 07:01

azeven


2 Answers

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

like image 95
Adam Eberbach Avatar answered Oct 13 '22 10:10

Adam Eberbach


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.

like image 37
Stephen Canon Avatar answered Oct 13 '22 11:10

Stephen Canon