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

    Re: Debug works, Release doesn't?

    Paul,
    The log10(yTest) is 8 so that won't work. I tried the following and it too fails for the Release mode which i=30 instead of 10. Debug i=10. I suspect this is the same problem. Exact equality won't work in this case either.


    Code:
    #include <iostream>
    using namespace std;
    
    void main(){
        int i;
        double yTest;
    
        yTest = 100000000.;
        for(i=0;i<30;i++){
            if(yTest-.01 == 0.) break;
            yTest /= 10.;
        }
    
        cout<<i<<endl;
    }
    Regards,
    Lowell

  2. #17
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Debug works, Release doesn't?

    Quote Originally Posted by lspecht View Post
    Paul,
    The log10(yTest) is 8 so that won't work.
    Add 2 to that, since you are dividing by 10^(-2). I stated that log10 is a hint, not the exact answer. The number of times you loop in the first example is related to the number of digits in the number.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; July 7th, 2009 at 11:59 AM.

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

    Re: Debug works, Release doesn't?

    That would work for this example, however, I oversimplified my example. In my app, yTest can be any power of 10 and the .01 can be any power of 10 less than yTest. For the next case it could be yTest = 1000. and .000001. The problem is to determine the number of powers of 10 in the spread.
    Regards,
    Lowell

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

    Re: Debug works, Release doesn't?

    Sorry. I didn't read all of your last post. I need to think about it some more.
    Regards,
    Lowell

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

    Re: Debug works, Release doesn't?

    When using logs to determine number of digits, you have to be careful about boundary cases. I don't recall the exact rule offhand, though.

  6. #21
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Debug works, Release doesn't?

    Quote Originally Posted by lspecht View Post
    That would work for this example, however, I oversimplified my example. In my app, yTest can be any power of 10 and the .01 can be any power of 10 less than yTest. For the next case it could be yTest = 1000. and .000001. The problem is to determine the number of powers of 10 in the spread.
    Another possible solution is to normalize yTest as an integer (multiply it by 100, 1000, whatever). Then use that to figure out

    1) How many times to loop.

    2) The formula you use within the loop.

    If you have 64-bit ints (you should), then maybe this is another approach you can take. Make yTest a 64-bit int, and see how that shakes out.

    Regards,

    Paul McKenzie

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

    Re: Debug works, Release doesn't?

    After thinking about your suggestion, Paul, I came up with this solution that should work. I'll have to add code to test for when y2Test is 1 or greater then subtract instead of add but that is straightforward. Note, I replaced the .01 with y2Test which I should have done in my original example since it is variable.

    Thanks for all the help, everyone.

    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    void main(){
        double i;
        double yTest;
        double y2Test;
    
        yTest = 100000000.;
        y2Test = .01;
    
        i = abs(log10(yTest)) + abs(log10(y2Test));
    
        cout<<i<<endl;
    }
    Regards,
    Lowell

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

    Re: Debug works, Release doesn't?

    Quote Originally Posted by lspecht View Post
    After thinking about your suggestion, Paul, I came up with this solution that should work. I'll have to add code to test for when y2Test is 1 or greater then subtract instead of add but that is straightforward. Note, I replaced the .01 with y2Test which I should have done in my original example since it is variable.
    OK. But make sure you test on the different compilers. It still uses some floating point, but as long as "i" is the same then you should be OK.

    I still think that normalizing everything to int, and doing the floating point calculations inside the loop is the way to go though.

    Also, one more thing -- main() returns int, not void.

    Regards,

    Paul McKenzie

Page 2 of 2 FirstFirst 12

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