CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Mar 2004
    Location
    Kennesaw, GA, USA
    Posts
    39

    Question Debug works, Release doesn't?

    I came across this in my MFC app and simplified the code into this console app for this example. When I compile and run the following code in Debug mode, it gives the correct answer for i, (10). When I compile and run it in Release mode, it gives the wrong answer for i, (11). Anyone know why?


    Code:
    #include <iostream>
    using namespace std;
    
    void main(){
        int i;
        double yTest;
    
        i=0;
        yTest = 100000000.;
        while (yTest-.01 > 0.){
            yTest /= 10.;
            i++;
        }
    
        cout<<i<<endl;
    }
    VS 2003 .Net Version 7.1.3088
    Regards,
    Lowell

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Debug works, Release doesn't?

    Release mode frequently uses "fast math" computations which are faster but slightly less accurate. Of course, expecting something like this to be completely accurate seems rather dubious anyway.....floats dinnae work like that.

    Any math that can be "broken" by a change in compile settings does not exhibit numerical stability, which is a highly desirable property for computer programs.

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Debug works, Release doesn't?

    I've just tested this code in VS2008 and result is the same for both Release and Debug: it is 10
    Victor Nijegorodov

  4. #4
    Join Date
    Mar 2004
    Location
    Kennesaw, GA, USA
    Posts
    39

    Re: Debug works, Release doesn't?

    So where is the instability in my code? I totally expected this to work no matter how it was compiled? How should it be written?
    Regards,
    Lowell

  5. #5
    Join Date
    Mar 2004
    Location
    Kennesaw, GA, USA
    Posts
    39

    Re: Debug works, Release doesn't?

    Thanks, Victor. I was afraid of that. Guess I'll have to write some hack code to deal with it until I can upgrade.
    Regards,
    Lowell

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Debug works, Release doesn't?

    Interesting! VC++6.0 gave me the same results as your VS 2003:
    10 - for Debug
    11 - for Release
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Debug works, Release doesn't?

    Quote Originally Posted by lspecht View Post
    So where is the instability in my code?
    0.1 is not an exact binary floating point number. Therefore your loop is ill-formed, as there is no guarantee how many times it will execute.

    The bottom line is that loop counters should be done in integer, not floating point. If you know you must loop 10,000 times, then specifiy that explicitly using integer.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Debug works, Release doesn't?

    Read the message here, and go to the link concerning using floating point math as loop controllers.

    http://www.codeguru.com/forum/showpo...52&postcount=3

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Mar 2004
    Location
    Kennesaw, GA, USA
    Posts
    39

    Re: Debug works, Release doesn't?

    The problem is I don't know how many times the loop needs to run. It will likely be different every time. Your point should be taken care of by the fact that I'm using > and not an exact equality to exit the loop.
    Regards,
    Lowell

  10. #10
    Join Date
    Mar 2004
    Location
    Kennesaw, GA, USA
    Posts
    39

    Re: Debug works, Release doesn't?

    Paul,

    I have a better understanding now that I read the post you referenced. Thanks for the information. I'll have to think of a solution that will work no matter which compiler option I use.
    Regards,
    Lowell

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Debug works, Release doesn't?

    OK, let's simplify this test:
    Code:
    #include <iostream>
    #include "tchar.h"
    using namespace std;
    
    int _tmain(int argc, TCHAR* argv[])
    {
        double yTest;
        yTest = 0.1;
        yTest /= 10.;
    	if(yTest > .01)
    		printf("yTest > .01\n");
    	else
    		printf("yTest <= .01\n");
    
        return 0;
    }
    The results:
    VC++6.0, Debug:
    Code:
    yTest <= .01
    VC++6.0, Release:
    Code:
    yTest > .01
    VS2008, Debug:
    Code:
    yTest <= .01
    VS2008, Release:
    Code:
    yTest <= .01
    Last edited by VictorN; July 7th, 2009 at 11:27 AM.
    Victor Nijegorodov

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Debug works, Release doesn't?

    Quote Originally Posted by lspecht View Post
    Paul,

    I have a better understanding now that I read the post you referenced. Thanks for the information. I'll have to think of a solution that will work no matter which compiler option I use.
    Unless I'm totally off, you can try the following as a hint:

    The number of times you loop will be based on the log10 of yTest.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Debug works, Release doesn't?

    Quote Originally Posted by VictorN View Post
    OK, let's simplify this test:
    Code:
    #include <iostream>
    #include "tchar.h"
    using namespace std;
    
    int _tmain(int argc, TCHAR* argv[])
    {
        double yTest;
        yTest = 0.1;
        yTest /= 10.;
    	if(yTest > .01)
    		printf("yTest > .01\n");
    	else
    		printf("yTest <= .01\n");
    
        return 0;
    }
    The results:
    VC++6.0, Debug:
    Code:
    yTest <= .01
    VC++6.0, Release:
    Code:
    yTest > .01
    VS2008, Debug:
    Code:
    yTest <= .01
    VS2008, Release:
    Code:
    yTest <= .01
    The problem is that 0.1 has no exact binary representation, and dividing that number by 10 incurs more round-off error. How much round-off error depends on compiler, compiler options, etc.

    If you started yTest at 0.5 and then divided by 2, you should see that the results are the same, regardless of compiler, compiler options, etc.

    Regards,

    Paul McKenzie

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: Debug works, Release doesn't?

    Quote Originally Posted by Paul McKenzie View Post
    The problem is that 0.1 has no exact binary representation, and dividing that number by 10 incurs more round-off error.
    I agree!
    What I couldn't understand is why:
    either binary representation of 0.1 differs for Debug and Release
    or FP-divide operation is performed differently for Debug and Release.
    Victor Nijegorodov

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Debug works, Release doesn't?

    Quote Originally Posted by VictorN View Post
    I agree!
    What I couldn't understand is why:
    either binary representation of 0.1 differs for Debug and Release
    or FP-divide operation is performed differently for Debug and Release.
    Probably the latter. I remember that different compilers do different things with the floating point unit, even things that seem as simple as a division.

    A look at the assembly code would answer the question of what really happens during the division.

    Regards,

    Paul McKenzie

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured