CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Jul 2003
    Posts
    260

    debug build ok but release build fails

    Good Evening.

    This problem has been causing me pain for awhile. I hope someone can provide me help..

    I am seeing that for the below code, on the debug build, it is fine, but on release build it fails. I am using VC 7.0.

    int index - 1;
    float *Range;
    float min = 1.50f;
    float increment_step = 0.1;
    Range = new float [steps];
    Range[0] = min;

    while (index<= steps) {
    Range[index] = Range[index-1] + increment_step;
    index++;
    }

    later I want to check the index of a given value in Range[]

    for ( int i = 0; i < index; i++ ) {
    printf (" values %f %f \n", value, Range[i]);
    if (value == Range[i] ) {
    index = i;
    break;
    }
    }

    the problem is, I can see in the for-loop, the values do match, but it never jumps to the if condition.

    Please... help..

    Jiac

  2. #2
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    1. When you post code, post complete code with complete variables that compiles.
    2. Since you are dealing with arrays, I would say you are overwriting a bounds and stepping on memory, a common occurance.
    3. A quick check to verify it's not the range array, is to call delete [] Range; in the DEBUG build, after you have finished modifing the array, if you have stepped into no-mans land (0xFD) 4 byte fill around your new allocation, the integrity checking code in the CRT will breakpoint.

    Read:

    http://www.codeproject.com/debug/survivereleasever.asp

    PS: when you post code, place your code in CODE tags, read the forum FAQ on how to use the TAGS

    eg:
    Code:
        if(t != 0)
        {
            int a = 3;
        }
    Last edited by Mick; September 10th, 2003 at 11:27 PM.

  3. #3
    Join Date
    Jul 2003
    Posts
    260
    thanks.

    I am able to step through the code in both modes, and when encountering the delete Range; it doesn't break out of the program abruptly.


    Jiac

  4. #4
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Never test float and double variables for equality. Always use a confidence interval. Floating point values which seem to be identical in the debugger are very likely to differ in decimal places which are not displayed.

  5. #5
    Join Date
    Jul 2003
    Posts
    260
    How do I guarantee that this line produces a float? (I am thinking it is this line where you think there is double?)

    Range[index] = Range[index-1] + increment_step;


    I have tried:
    Range[index] = (float)(Range[index-1] + increment_step);
    Range[index] = ((float)(Range[index-1] + increment_step));

    thanks.
    Jiac

  6. #6
    Join Date
    Jul 2003
    Posts
    260
    What is a confidence level?
    "Confidence Interval: The computed interval with a given probability (ie, 95%) that the true value of the statistic"
    ??


    In any case, can you throw me an example of how to implement this?

    Jiac

  7. #7
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    I don't meant not to compare float with double, but not even float with float and double with double.

    Instead of comparing two values like this:
    Code:
    double a = 1.0;
    double b = 1.0;
    
    // ... Some math here 
    
    if(a == b)
    {
      // May never be called, even if a *seems* equal to b
    }
    you should define a maximal acceptable difference for two doubles to be considered equal:
    Code:
    const double maxdiff = 0.000001;
    
    if(fabs(a-b) <= maxdiff)
    {
      // ...
    }
    Of course, you could make this a macro or a function.

  8. #8
    Join Date
    Jul 2003
    Posts
    260
    thanks. I tried that and it worked on one part of the code.

    Ultimately, I still need to deal with comparing 1.75 (with some precision off) to 1.75 defined through IEEE format.

    any ideas?

    (not-so-fond-of-floating-points)

    Jiac

  9. #9
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by jiac
    Ultimately, I still need to deal with comparing 1.75 (with some precision off) to 1.75 defined through IEEE format.
    I don't see the problem. The float and double datatypes are using the IEEE format - or what format do you mean?

  10. #10
    Join Date
    Jul 2003
    Posts
    260
    I use the following to check the precision, can't think of better ways.

    union FI {
    float fval;
    int ival;
    };

    union FI fi;
    fi.fval = 1.75;

    say, check to see fi.ival = 1071225341

    but in my Range[index] , this can possibly be 1071225342.

    My question is, how to make floating pt less precise, but still precise enough to do comparison or I am asking too much?

    In the printouts, I can see the values being 1.75, but it is probably not the exact 1.75.

    really, thanks for your help.

  11. #11
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by jiac
    I use the following to check the precision, can't think of better ways.
    Code:
    union FI {
      float fval;
      int ival;
    };
    
    union FI fi;
    fi.fval = 1.75;
    I don't know how defining a union with an int and float helps you in any way. If anything, your "solution" adds confusion.

    Why not just simply do what gstercken suggested -- get the absolute value of the two floating point values and see if it is within some tolerance level. Historically, this is how floating point values are compared in languages such as C and C++.

    Regards,

    Paul McKenzie

  12. #12
    Join Date
    Jul 2003
    Posts
    260
    I did try that, and it works, but I have another part of the code where the floating pts are defined via IEEE, I can't change that. It is a hash - needs to be exact.

    yeah, I am fairly confused when I used the unions. I tried using sprintf to do rounding; I couldn't find a build-in, it seems to be working. I guess by * 1000 and then divide by 1000 doesn't help w/ rounding..

    thanks..

    Jiac..

  13. #13
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by jiac
    I did try that, and it works, but I have another part of the code where the floating pts are defined via IEEE, I can't change that.
    Sorry, but I still don't get the point. AFAIK, float and double are always in IEEE format, at least with the VC++ compiler.

    It is a hash - needs to be exact.

    yeah, I am fairly confused when I used the unions. I tried using sprintf to do rounding; I couldn't find a build-in, it seems to be working. I guess by * 1000 and then divide by 1000 doesn't help w/ rounding..
    If you just want to round or truncate: You are right, unfortunately there are no C/C++ standard library functions like round() or trunc(). However, there are floor() or ceil(). floor() can be used to implement truncating like this:
    Code:
    #include <math.h>
    
    double trunc(double value, unsigned int decimals)
    {
      double factor = pow(10,decimals);
      return floor(value * factor) / factor;
    }
    For rounding, just add 0.5:
    Code:
    double round(double value, unsigned int decimals)
    {
      double factor = pow(10,decimals);
      return floor((value * factor) + 0.5) / factor;
    }
    Last edited by gstercken; September 11th, 2003 at 03:06 PM.

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