Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Maybe a bug in VC++

Hybrid View

  1. #1
    Junior Member Newbie
    Join Date
    Dec 2000
    Posts
    6

    Maybe a bug in VC++

    Did anyone encounter the following unknown result from VC++?

    Assume both "var3" and "plane.x_low_bound" are double floating point variables and are absolutely the same numerically.

    if (var3 == plane.x_low_bound)
    .......

    "var3 == plane.x_low_bound" returns "false"

  2. #2
    Member Regular Contributor
    Join Date
    May 2000
    Posts
    454

    Re: Maybe a bug in VC++

    Yes, this is a common problem. It is due to imprecession in floating point numbers. Performing 2 difference calcultions that should theoretically arrive at the same result can actually have slightly different results.

    And due to the way the IDE rounds values before displaying them in the watch, the two values may even look the same. For instance, one value may be 1.000000 and the other value may be 1.000001, but the IDE will show them both as 1.000, so they look equal.

    The trick to testing floating point numbers for equality is to accept that the numbers may have a slight bit of error in them, and then see if the difference between these numbers is less than a set threshold.

    EX:
    if ((var1-var2) < 0.000001)
    cout << "var1 and var 2 are equal" << endl;
    else
    cout << "var1 and var 2 are NOT equal" << endl;


    [This message has been edited by LordKronos (edited 01-21-2001).]
    Ron Frazier

  3. #3
    Senior Member OpenGL Pro
    Join Date
    May 2000
    Location
    Hannover, Germany
    Posts
    1,258

    Re: Maybe a bug in VC++

    Oh, Lord Kronos, I think it's just because you write so fast...
    Actually your method works only if the first floating point value is bigger than the second.

    it has to be:

    Code :
    #define ALLOWED_DELTA 0.0000001f
    #define ARE_FLOATS_EQUAL(x,y) \
       (((x-y)<ALLOWED_DELTA) &amp;&amp; \
       (((y-x)<ALLOWED_DELTA) )
    Maybe that's after all worng too...
    - Michael Steinberg

  4. #4
    Member Regular Contributor
    Join Date
    May 2000
    Posts
    454

    Re: Maybe a bug in VC++

    Originally posted by Michael Steinberg:
    Oh, Lord Kronos, I think it's just because you write so fast...
    Actually your method works only if the first floating point value is bigger than the second.
    In the words of a very wise man named Homer...Doh!

    Yes, you are right, I was typing faster than I was thinking. I meant to do

    (fabsf(var1-var2) < 0.000001f)

    But your way will work too. Not sure which is faster though. I tend to trust the library functions, hoping that they will use something faster (like hardware instructions) that doesn't involve conditionals (which cause pipeline stalls in the CPU). Using VC++, trusting the libraries and the optimizer usually pays off, but Im not sure about this case.




    [This message has been edited by LordKronos (edited 01-21-2001).]
    Ron Frazier

  5. #5
    Senior Member OpenGL Guru Humus's Avatar
    Join Date
    Mar 2000
    Location
    Stockholm, Sweden
    Posts
    2,444

    Re: Maybe a bug in VC++

    Library functions are always that fast, they tend to like to set FPU state and stuff instead of just doing what they're supposed to do. If you want it fast, you should write a small assembler functions and use them instead to the library ones. Like this:

    #pragma warning(disable: 4035)
    float sin(float x){
    __asm {
    FLD x
    FSIN
    }
    }

    float cos(float x){
    __asm {
    FLD x
    FCOS
    }
    }

    float sqrt(float x){
    __asm {
    FLD x
    FSQRT
    }
    }

    float fabs(float x){
    __asm {
    FLD x
    FABS
    }
    }

  6. #6
    Member Regular Contributor
    Join Date
    Apr 2000
    Location
    Redlands, CA, USA
    Posts
    292

    Re: Maybe a bug in VC++

    > (fabs(var1-var2) < 0.000001f)
    this version is faster: floating point comparison is slow.

    It may be even faster to compare as integer numbers:

    float small_delta = 0.000001f; // must be >=0
    //===============
    float fcmptemp = fabs(var1-var2);
    if(*(int*)&fcmptemp < *(int*)&small_delta)
    {
    }

    float fabs(float x){
    __asm {
    FLD x
    FABS
    }
    }
    This replacement is slower then regular fabs().
    (for VC with optimization).

  7. #7
    Member Regular Contributor
    Join Date
    May 2000
    Posts
    454

    Re: Maybe a bug in VC++

    Originally posted by Serge K:
    This replacement is slower then regular fabs().
    (for VC with optimization).
    Actually, his version of fabs (and the other functions) dont do anything, since they dont return any values.
    Ron Frazier

  8. #8
    Member Regular Contributor
    Join Date
    Apr 2000
    Location
    Redlands, CA, USA
    Posts
    292

    Re: Maybe a bug in VC++


    Actually, function should return floating point result on top of the FPU stack.

    Floating Point Coprocessor and Calling Conventions

  9. #9
    Member Regular Contributor
    Join Date
    May 2000
    Posts
    454

    Re: Maybe a bug in VC++

    Originally posted by Serge K:

    Actually, function should return floating point result on top of the FPU stack.
    Nope, when I tried to compile his fabs function, VC++ complained to me about not returning a value and refused to compile it. I dont really know much about integrating assembly into VC++ code, so maybe there is some keyword that you need to use to indicate the function returns it's value on the FPU stack, but as it stands it wouldnt compile for me.


    [This message has been edited by LordKronos (edited 01-22-2001).]
    Ron Frazier

  10. #10
    Senior Member OpenGL Pro
    Join Date
    May 2000
    Location
    Hannover, Germany
    Posts
    1,258

    Re: Maybe a bug in VC++

    Is there any printable book in the internet which teaches assembler language very well? However, I was always working platform independent. Maybe my version is faster than the normal fabs function, where the assembler optimization only works on x86. Maybe not?
    - Michael Steinberg

Posting Permissions

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