Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

float comparison problem objective c

strange behavior:

(xcode 4 breakpoint screenshot) http://img3.picload.org/image/lawgcd/untitled.png

why does this comparison yield true? I really dont get it..

like image 398
DucatiNerd Avatar asked Jun 19 '11 17:06

DucatiNerd


1 Answers

The code seems to be working correctly,
are you sure the comparison validates true?

Quick snippet:

for(int i=0;i<10;i++){
    float value = (arc4random()%100)/(float)100.0f;
    NSLog(@"%f",value);
    if(value < 0.01f) {
        NSLog(@"YES");
    } else {
        NSLog(@"NO");
    }
}

Output:

0.520000
NO
0.520000
NO
0.100000
NO
0.000000
YES
0.390000
NO
0.690000
NO
0.770000
NO
0.930000
NO
0.320000
NO
0.230000
NO

EDIT - Response to comment:

In my case, the breakpoints are working correctly:

enter image description here

like image 97
Anne Avatar answered Sep 22 '22 16:09

Anne