Results 1 to 9 of 9

Thread: unexact decrementing GLfloat

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2000
    Posts
    2

    unexact decrementing GLfloat

    Hi all!

    Although my question is not 100% OpenGL related, I stumbled across this problem when modifying example code from NeHe's tutorials.

    Using Visual C++ 6 (SP4) I decremented a GLfloat variable (initialized with 1.0f) by 0.1f until it is 0.0f. When the variable was 0.2f and then was decremented by 0.1f, the result was 0.999999f (and not 0.1f as it should have been).

    Has anyone a hint or a workaround for this? Should I round the result (how?).

    Thanks in advance,

    elvin

  2. #2
    Junior Member Newbie
    Join Date
    Nov 2000
    Posts
    2

    Re: unexact decrementing GLfloat

    One other thing:

    I multiplied every value by 10.0f so that I don't subtract 0.1f but 1.0f. Now it works!

    elvin

  3. #3
    Senior Member Frequent Contributor
    Join Date
    Aug 2000
    Location
    Portsmouth, Hampshire, England
    Posts
    926

    Re: unexact decrementing GLfloat

    When dealing with floating point numbers you should NEVER do equality tests (==). Always use <= or >= . eg. if(f_vlaue <= 0.0f)

    Because floats store values in binary, any non power of 2 number representation is only an approximation, so comparing it to a precise value like 0 is bound to fail lots.

    Happy coding,
    Nutty

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: unexact decrementing GLfloat

    >> any non power of 2 number representation is only an approximation

    What I know, this isn't entirely correct. Consider the number 0.75f. This one is not a power of two, but it is a sum of 0.5f and 0.25f, which in turn is, all of them, a power of two. So this number CAN be represented exactly, even though it isn't a power of two. But the number 0.1f, howerver, is an approximation. 0.1f can't be split up in only power-of-two-components, and therefore isn't exact. This has probably something to to with why elvin's program "fails". Because of roundoff errors or whatever. When it comes to comparsions, it's always a good habit to do what Nutty says, by as often as possible comparig with a >= or <=, whatever datatype you are using.

  5. #5
    Senior Member Frequent Contributor
    Join Date
    Aug 2000
    Location
    Portsmouth, Hampshire, England
    Posts
    926

    Re: unexact decrementing GLfloat

    Well.. you know what I meant. Just did a little test program, and even though .75 can be expressed exactly, .5 and .25 cannot.

    This program also proved that doing == 0.0f is bad. Here it is if you wanna see.

    #include <stdio.h>

    void main(void)
    {
    float f = 0.75f;
    int i;

    for(i=0; i<20; i++)
    {
    printf("\n%.05f %X", f, *((int*)&f) );

    f -= 0.05f;

    if(f == 0.0f)
    {
    printf("Wehay\n");
    }
    }
    }

    P.S. Sorry the formatting went all Pete Tong!

    [This message has been edited by Nutty (edited 11-29-2000).]

  6. #6
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: unexact decrementing GLfloat

    Hmm, not sure about what that code is supposed to do/prove. Maybe it sounds strange, but can you explain in case I misunderstood the whole thing?

    Note: Not trying to be rude or anything. And I do understand what you meant. Hope you understand me correct aswell.

    First, when running the code, I can see that 0.75 (=3F000000), 0.5 (=3F400000) and 0.25 (=3E800000) is represented as exact numbers (and you said they couldn't), and I get these numbers by altering the startnumber to 0.5 and 0.25 respectively, and recompile and run the program. However, when running it with 0.75 as startnumber, 0.5 and 0.25 (the theoretical values) is not exact represented. And this is becaues 0.05 isn't representable exatly, and therfore you get roundoff errors while decrementing, and you never get an exact zero because the error gets too large.

  7. #7
    Senior Member OpenGL Pro
    Join Date
    Dec 2007
    Posts
    1,077

    Re: unexact decrementing GLfloat

    Consider 0.000001f to be your precision.
    You can't use a == for floats because of the said reasons.

    So do something like this

    a first float
    b second float

    #define precision 0.000001f

    if( (a-b)<precisoion && (a-b)>-precision ) {
    its equal
    }

  8. #8
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: unexact decrementing GLfloat

    Erm, nice tip

    Never thought of that one...

  9. #9
    Senior Member Frequent Contributor
    Join Date
    Aug 2000
    Location
    Portsmouth, Hampshire, England
    Posts
    926

    Re: unexact decrementing GLfloat

    Ooops you're right Bob. For some reason I thought .5 and .25 couldn't be exact. (probably cos I wasn't subtracting exactly 0.05)

    Anyway it was proving that the Wehay message never got printed (not on mine anyway).

    cheers,
    Nutty

Similar Threads

  1. Why should i use GLfloat instead of float ?
    By greg2 in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 08-22-2007, 08:21 PM
  2. OpenGL texturing very unexact?
    By BlackJack in forum OpenGL: Advanced Coding
    Replies: 5
    Last Post: 06-21-2002, 12:39 AM
  3. printf (GLfloat) ??
    By vofka in forum OpenGL: Advanced Coding
    Replies: 7
    Last Post: 04-20-2002, 02:50 PM
  4. Glfloat etc...
    By zzapatista in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 02-21-2001, 10:22 PM
  5. GLfloat problems
    By imported_robert in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-11-2001, 12:19 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
  •  
Proudly hosted by Digital Ocean