开发者

float comparison problem objective c

开发者 https://www.devze.com 2023-03-13 20:10 出处:网络
strange behav开发者_如何学运维ior: (xcode 4 breakpoint screenshot) why does this comparison yield true? I really dont get it..The code seems to be working correctly,

strange behav开发者_如何学运维ior:

(xcode 4 breakpoint screenshot)

float comparison problem objective c

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


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:

float comparison problem objective c

0

精彩评论

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