[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[IMP-dev] Be very careful with floating point numbers



Since it keeps coming up, I'll say again, never depend on floating point equality checks (or transitivity). As an example, I just ran into a problem with domino caused by C++ code which looked like:
float min=big_number;
loop over things:
   if (thing.get_value() < min) min= thing.get_value()

loop over things again:
 if (thing.get_value() == min) return thing;

Code like this can fail if:
- the type of thing.get_value() is not exactly the same as the type of min
- or thing.get_value() performs some computations and the compiler decides to inline them - or for whatever other reason, thing.get_value() is never written to main memory

The reason is that on intel chips, floating point values are stored as 32 bit, 64 bit or 80 bit values depending on what the compiler feels like doing. They are only truncated into the specified 32 bit (float) or 64 bit (double) values when stored as the appropriate value type in cache or memory or when copied to a different type of register. If one copy of a value is truncated to a 32 bits or 64 bits by being moved, then it is no longer equal to its old value.

In this case the fix is trivial: just combine the two loops and keep track of the min element as you go along. Other times you will have to jump through more hoops.