I have a set of NSTimeIntervals like this:
NSArray *mySet = [NSArray arrayWithObjects:
[NSNumber numberWithDouble: time1], //
[NSNumber numberWithDouble: time2],
[NSNumber numberWithDouble: time3],
nil];
// suppose that at this time
// time1 = 0.00000
// time2 = 18.3200
// time3 = 21.6500
at some point in my code I want to test if currentTime is greater or equal to one of the times on the array, so I do
// currentTime = 18.32 right now
for (int i=0; i<[mySet count]; i++) {
if (currentTime >= [[mySet objectAtIndex:i] doubleValue]) {
NSLog(@"%d...", i);
}
}
The output should be “1…2…”
but the output is just “1…”
when i = 1,
it is comparing 18.32 to 18.32 and failing to recognize that the value is equal or greater than the other? WTF???
How can that be?
thanks for any help.
Typically when comparing floating point values you want to decide how close one needs to be to the other (an epsilon or error bound), then just check if they are within that. The easiest way to do that is subtract one from the other then check if the absolute value of the result is less than or equal to your epsilon.
Pseudocode: