# Floating point comparison

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

## 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 on other sites
The two compare statements you give are equivalent. Whatever funny stuff is going on in your code must originate from something else.

##### Share on other sites
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 on other sites
Quote:
 Original post by megizinDoes 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 havefloat x = 0.0001f;will the 2 statements below yield different results?x <= 0x <= 0.0fThanks 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 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 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 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 on other sites
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

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 14
• 46
• 22
• 27
• ### Forum Statistics

• Total Topics
634047
• Total Posts
3015231
×