When to use C float comparison functions?

Unlike the relational operators, these macros do really only return a boolean value and do never raise any floating point exception.

In short: You only have to deal with true/false and nothing else.


references:

The Open Group descriptions (not the C or C++ standard, but highly relevant in the Unix/Linux world and almost always similar to the standards):

  • http://pubs.opengroup.org/onlinepubs/009695399/functions/islessgreater.html
  • http://pubs.opengroup.org/onlinepubs/009695399/functions/isgreater.html
  • http://pubs.opengroup.org/onlinepubs/009695399/functions/isgreaterequal.html
  • http://pubs.opengroup.org/onlinepubs/009695399/functions/isless.html
  • http://pubs.opengroup.org/onlinepubs/009695399/functions/islessequal.html
  • http://pubs.opengroup.org/onlinepubs/009695399/functions/islessgreater.html
  • http://pubs.opengroup.org/onlinepubs/009695399/functions/isunordered.html

C++ standard:

C Library [c.math]:

The classification/comparison functions behave the same as the C macros with the corresponding names defined in 7.12.3, Classification macros, and 7.12.14, Comparison macros in the C Standard. Each function is overloaded for the three floating-point types, as follows […]

C standard:

7.12.14 Comparison macros

[…] For any ordered pair of numeric values exactly one of the
relationships — less, greater, and equal — is true. Relational operators may raise the
‘‘invalid’’ floating-point exception when argument values are NaNs. For a NaN and a
numeric value, or for two NaNs, just the unordered relationship is true
.
The following subclauses provide macros that are quiet (non floating-point exception raising) versions of the relational operators, and other comparison macros that facilitate writing efficient code that accounts for NaNs without suffering the ‘‘invalid’’ floating-point exception. In
the synopses in this subclause, real-floating indicates that the argument shall be an
expression of real floating type.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)