Search code examples
cfloating-pointcompare

Why the C standard library haven't isequal?


C standard library have: isgreater,isgreaterequal,isless,islessequal,islessgreater, isunordered. And why the C standard library haven't isequal? It should be easy to implement like that:

bool isequal(float x, float y)
{
    return !isunordered(x, y) && isgreaterequal(x, y) && !isgreater(x, y);
}

Solution

  • The C99 rationale V5.10 explains that these macros were added to the language for the sole purpose of being safe and not raising floating point exceptions, like their corresponding operators might do:

    The relational operators (<, <=, >=, >), but not the equality operators (== and !=), may raise floating-point exceptions when one of the operands is a NaN (as an error indicator for programs written without consideration of NaNs). There is a need for comparisons that are guareented [sic] not to raise any floating-point exceptions; this also is a requirment [sic] of IEC-60559. That is why the isgreater, isgreaterequal, isless, islessequal, islessgreater and isunordered function-like macros were invented.