Jump to content
  • Advertisement
Sign in to follow this  

Floating point comparison

This topic is 4718 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does comparing to '0' give different results than comparing to 0.0f? I have found this to be a possible answer to some funny stuff in my collision detection so I thought I might ask about it. Suppose I have float x = 0.0001f; will the 2 statements below yield different results? x <= 0 x <= 0.0f Thanks in advance!

Share this post


Link to post
Share on other sites
Advertisement
Try this concept:


bool Equal(double a, double b)
{
const double EPS = 1e-4;

return (fabs(b - a) < EPS);
}

Most 3D engines use it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by megizin
Does comparing to '0' give different results than comparing to 0.0f? I have found this to be a possible answer to some funny stuff in my collision detection so I thought I might ask about it.

Suppose I have

float x = 0.0001f;

will the 2 statements below yield different results?

x <= 0

x <= 0.0f

Thanks in advance!


You are correct comparison yield different results on x86 architecture, when compiling in release mode without the option floating point consistency.

This is what happens:
The reason is that in release mode intermediate values are not saved out to memory, this generates results that are truncated to zero in a 32 bit format but different that zero in 64 bit format.

In your test if the last x was the last result of an expression it may look like a zero when you see in the debugger but it may be either positive or negative when you see it in the x86 register.
The problem is not that you comparing against 0 or 0.0f it s the previous result of the x.
To get rid of this behavior use the option floating point consistency. This option force the complier to generate code that write intermediate value to memory before using then on compare statements.




Share this post


Link to post
Share on other sites
Note that this can also happen with doubles, since x86's FPU uses 80-bit registers internally. Depending on whether something has to be written to memory (using 64 bits) or not you can get different results. This makes for some nasty bugs. You can configure the FPU to only use 64 bits internally and then things should behave more nicely.

Share this post


Link to post
Share on other sites
I see. I will write some functions to do the compare and then play with the floating point consistency.

Thanks all!

Share this post


Link to post
Share on other sites
sjf is right : a floating comparision never use the = operator.
Compare that the difference between the 2 values is less than an Epsilon.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
This is and issue that had been recognized by GCC, Intel and Microsoft compilers, that is why they added the option improve floating point consistence.
This does not mean that proper care most be taken when coding with floating points by using tolerances on compare statements, but it is a fact that any code that does not used this option in a x87 architecture will yield different result in release than in debug

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!