It is common knowledge that one has to be careful when comparing floating point values. Usually, instead of using ==
, we use some epsilon or ULP based equality test
In my humble opinion, you should not rely on the ==
operator because it has many corner cases. The biggest problem is rounding and extended precision. In case of x86, floating point operations can be done with bigger precision than you can store in variables (if you use coprocessors, IIRC SSE operations use same precision as storage).
This is usually good thing, but this causes problems like:
1./2 != 1./2
because one value is form variable and second is from floating point register. In the simplest cases, it will work, but if you add other floating point operations the compiler could decide to split some variables to the stack, changing their values, thus changing the result of the comparison.
To have 100% certainty you need look at assembly and see what operations was done before on both values. Even the order can change the result in non-trivial cases.
Overall what is point of using ==
? You should use algorithms that are stable. This means they work even if values are not equal, but they still give the same results. The only place I know where ==
could be useful is serializing/deserializing where you know what result you want exactly and you can alter serialization to archive your goal.