Name

isless, islessequal, islessgreater

Synopsis

Compares two floating-point values without risking an exception

#include <math.h>
intisless( x, y );
int islessequal( x, y );
int islessgreater( x, y );

The macro isless() tests whether the argument x is less than the argument y, but without risking an exception. Both operands must have real floating-point types. The result of isless() is the same as the result of the operation (x) < ( y), but that operation could raise an “invalid operand” exception if either operand is NaN (“not a number”), in which case neither is greater than, equal to, or less than the other.

The macro isless() returns a nonzero value (that is, true) if the first argument is less than the second; otherwise, it returns 0. The macro islessequal() functions similarly, but corresponds to the relation (x) <= ( y), returning true if the first argument is less than or equal to the second; otherwise 0. The macro islessgreater() is also similar, but corresponds to the expression (x) < ( y) || (x) > ( y), returning true if the first argument is less than or greater than the second; otherwise 0.

Example

double minimum( double a, double b )
{
  if (islessgreater( a, b ) )
    return ( isless( a, b ) ? a : b );
  if ( a == b )
    return a;
  feraiseexcept( FE_INVALID );
  return NAN;
}

See Also

isgreater(), isgreaterequal(), isunordered()

Get C in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.