lundi 27 avril 2015

Is this comparison to floating point literal zero valid?

From N3337 (C++11 draft) section 3.9.1.8:

The value representation of floating-point types is implementation-defined.

Does this apply to any and all usage of a float type, regardless of if it is a literal or not? Here is the example that is causing me some concern:

float foo{0.0f};
if (foo == 0.0f)
{
    // Am I always guaranteed to get here?
}

If we assume that 0.0f is not really true 0 as far as the implementation is concerned, but some undefined number, would this comparison still technically be valid since both operands were obtained via constants and even though I may not know its true value, they will both still be the same?

Equality comparisons with float literals like this always have a code smell and I just want to make sure there aren't certain use cases where this makes sense or is valid.

Aucun commentaire:

Enregistrer un commentaire