Floating point comparison

Started by
6 comments, last by GameDev.net 18 years, 10 months ago
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!
Advertisement
The two compare statements you give are equivalent. Whatever funny stuff is going on in your code must originate from something else.
Try this concept:


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

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

Most 3D engines use it.
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.




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.
I see. I will write some functions to do the compare and then play with the floating point consistency.

Thanks all!
sjf is right : a floating comparision never use the = operator.
Compare that the difference between the 2 values is less than an Epsilon.
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

This topic is closed to new replies.

Advertisement