Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 27

Thread: Weird problem with real variable

  1. #11
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    I once remeber reading an article on how CPUs make mathematical mistakes. You wouldnt happen to be encountering these issues on a Pentium chip would you? As far as I an awarem this is the Pentium specialty - although there was a time when google had this problem also . The inherent problem is how to debug stuff if its the CPU producing waked up results.

    If I remember correctly, its specifically related to the floating point XOR gates for division in the ALU... (Someone please check this). Other than that I woud say your best bet is to either round or use an approximate...
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  2. #12
    Quote Originally Posted by KidPaddle View Post
    Small changes, cause you forgot function call ABS(a - b)

    Code:
    const
      //A treshold for float comparison
      EPSILON = 1e-5;
    
    implementation
    
    function Equals(const a, b: Single): Boolean;
    begin
      Result := (abs(a - b) < EPSILON);
    end;
    Thomas
    Argh.. that's indeed a stupid mistake. Thanks for the heads up.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  3. #13
    Quote Originally Posted by code_glitch View Post
    I once remeber reading an article on how CPUs make mathematical mistakes. You wouldnt happen to be encountering these issues on a Pentium chip would you? As far as I an awarem this is the Pentium specialty - although there was a time when google had this problem also . The inherent problem is how to debug stuff if its the CPU producing waked up results.
    Nah, I'm on a Celeron.

    Quote Originally Posted by code_glitch View Post
    If I remember correctly, its specifically related to the floating point XOR gates for division in the ALU... (Someone please check this). Other than that I woud say your best bet is to either round or use an approximate...
    Yeah, I went the Round() way. It's not a problem at all, but it just made me curious as to the cause of it and the internal workings of the CPU.
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

  4. #14
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    Just re-read the article. Turns out therevis a wiki on it here: http://en.wikipedia.org/wiki/Pentium_FDIV_bug which is a prime example of how serious it can get. Funny enough, the bug did not hit some of the celeron chips based on the architecture... Oh well, intel whatever. Whats next - a cpu that takes tea leaves and makes coffee of it? I cant wait to see the day that AMD rolls out a cheap chip that beats the i7 extreme edition whatever its called
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  5. #15
    Ouch. That's a bad bug to have in your CPU! I'm sure somebody lost their job over this!

    I believe my old computer was a Pentium 90MHz. Didn't really experience any problems with this, though, even though I did a lot of programming on that machine as well.
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

  6. #16
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2
    If reals work like they used to, it's because the bit values can end up being things like 1, 0.5, 0.25, 0.125 and so on, each time dividing by 2. I can't remember the exact details as it was a while since I actually cared about it, but thats why reals can vary slightly from what you think they are, because it's impossible to set a bit pattern that matches exactly the value you want. The error might be very small, but it is still an error and comparing 3 with 3.000000000000000000000000000000001 (you get the picture) will still give a 'does not equal' result.

    Wikipedia has what looks to be a decent explanation of FP storage formats here.
    :: AthenaOfDelphi :: My Blog :: My Software ::

  7. #17
    PGD Staff code_glitch's Avatar
    Join Date
    Oct 2009
    Location
    UK (England, the bigger bit)
    Posts
    933
    Blog Entries
    45
    Which is why I literally never use Real variables... Except when I have to use some atrocious C/C++ code written with floating point variables. Or when I use OpenGl . I've had enough headaches with Reals to last a long time.
    I once tried to change the world. But they wouldn't give me the source code. Damned evil cunning.

  8. #18
    Quote Originally Posted by code_glitch View Post
    Or when I use OpenGl
    ...and that would be my situation.
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

  9. #19
    PGD Community Manager AthenaOfDelphi's Avatar
    Join Date
    Dec 2004
    Location
    South Wales, UK
    Posts
    1,245
    Blog Entries
    2
    I think the key thing here is that what you're actually doing is using the display mechanisms of your engine/display solution (OpenGL) to store state. If you need to do that kind of comparison, then isn't it worthy of having it's own state variable somewhere along the line. An integer field somewhere for example, faster absolute comparisons that are guaranteed to work. There is also the issue that you are performing absolute comparisons with specific values in your code... what happens if you change the look of something? Comparisons that used this data will all stop working unless of course you start comparing and setting values with constants.

    Just a few thoughts
    :: AthenaOfDelphi :: My Blog :: My Software ::

  10. #20
    Without getting into too many details of my program, the piece of code posted here was used to check whether or not that particular light source in the game belonged to the firing of a weapon (not OpenGL lighting; the game is 2D isometric tile based and the "lighting" is just how dark/light and with which colors each tile is to be drawn to the screen -- I just use OpenGL for fast sprite blitting). The "light" emitted from a firing weapon has a tile radius of 1.5 and an R, G, B value of 0.5, 0.5 and 0.3, which is why I was checking for those values. So the value I'm checking is not some OpenGL value, it is actually a state variable stored in the program (in the LightSource array).
    Laserbrain Studios - Currently developing Hidden Asset!
    Ascii Sector
    - Real-time roguelike in space!

Page 2 of 3 FirstFirst 123 LastLast

Bookmarks

Posting Permissions

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