Previous Page
Next Page

fpclassify

Obtains a classification of a real floating-point number

#include <math.h>
int fpclassify ( x  );

The fpclassify( ) macro determines whether its argument is a normal floating-point number, or one of several special categories of values, including NaN (not a number), infinity, subnormal floating-point values, zero, and possibly other implementation-specific categories.

To determine what category the argument belongs to, compare the return value of fpclassify( ) with the values of the following number classification macros, defined in math.h:

FP_INFINITE
FP_NAN
FP_NORMAL
FP_SUBNORMAL
FP_ZERO

These five macros expand to distinct integer values.

Example

double minimum( double a, double b )
{
  register int aclass = fpclassify( a );
  register int bclass = fpclassify( b );

  if ( aclass == FP_NAN || bclass == FP_NAN )
    return NAN;

  if ( aclass == FP_INFINITE )        // -Inf is less than anything;
    return ( signbit( a ) ? a : b );  // +inf is greater than anything.

  if ( bclass == FP_INFINITE )
    return ( signbit( b ) ? b : a );

  return ( a < b ? a : b );
}

See Also

isfinite( ), isinf( ), isnan( ), isnormal( ), signbit( )


Previous Page
Next Page