浮动比较问题目标c

Duc*_*erd 5 floating-point comparison xcode objective-c

奇怪的行为:

(xcode 4断点截图) http://img3.picload.org/image/lawgcd/untitled.png

为什么这种比较产生真实?我真的不明白..

Ann*_*nne 6

代码似乎工作正常,
你确定比较验证是真的吗?

快速片段:

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

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
Run Code Online (Sandbox Code Playgroud)

编辑 - 回复评论:

在我的例子中,断点正常工作:

在此输入图像描述