5

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..

2
  • 2
    Read this paper by Goldberg before doing any more programming involving floating point arithmetic: download.oracle.com/docs/cd/E19957-01/806-3568/… Commented Jun 19, 2011 at 19:04
  • Are you sure you're running the debugger on a "Debug" build? If you're running it on an optimised build with debugging symbols stripped, your debugger will have a very hard time matching breakpoints to lines in your source code. Commented Jun 20, 2011 at 11:21

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for taking time to look into this! The strange thing is that the test seems to yield true. At least when i set a breakpoint on the return, the debugger hits it.
On my Mac everything is working properly, checkout the screenshot.
Hi Anne, thanks again for taking time to reproduce this! I also did some tests meanwhile and it seems, the comparison actually does not yield true in my case. However, if i am in debug mode and set a breakpoint on the return in my if-body, the breakpoint is hit everytime just before the function is left. I am currently not sure why this happens in xcode/obj-c but the results are correct. Thanks again for your help and your time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.