I wrote a piece of code and came across a really strange problem. A comparison between two floats returns NO
even when the actual comparison is true. I even used safe floating point comparison by comparing to FLT_EPSILON
. This is the code:
//To start the process run this:
[self increment:0.0f];
- (void)increment:(float)f {
f += 0.02f;
if ((fabs(f - 1.0f) < FLT_EPSILON)) {
NSLog(@"STOP");
}
else {
NSLog(@"F %f", f);
[self increment:f];
}
}
And the comparison will always fail and the code it will go into an infinite loop. I have tested this on a 32 bit device on iOS 7 and on the iPhone 5S simulator on iOS 8.