C Standard Library

C <math.h> - fpclassify()



The C <math.h> fpclassify() macro returns a value of type int that categorizes floating point value x into the following categories: NAN, infinite, zero, subnormal, normal, or implementation-defined category.

MacrosDescription
FP_NAN indicates that the value is not-a-number (NaN).
FP_INFINITE indicates that the value is positive or negative infinity (overflow).
FP_ZERO indicates that the value is positive or negative zero.
FP_SUBNORMAL indicates that the value is subnormal (underflow).
FP_NORMAL indicates that the value is normal, i.e. not an infinity, subnormal, not-a-number or zero.

Syntax

fpclassify(x)

Parameters

x Specify a floating point value to classify.

Return Value

Returns one of the following int values: FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL or implementation-defined type, specifying the category of x.

Example:

The example below shows the usage of fpclassify() function.

#include <stdio.h>
#include <float.h>
#include <math.h>

const char* Show_Classification (double x) {
  switch(fpclassify(x)) {
    case FP_NAN: 
      return "nan.";
    case FP_INFINITE:   
      return "inf."; 
    case FP_ZERO:   
      return "zero."; 
    case FP_SUBNORMAL:  
      return "subnormal.";
    case FP_NORMAL:  
      return "normal.";  
    default:
      return "unknown.";
  }
}

int main (){

  printf("1.0/0.0 is %s\n", Show_Classification(1.0/0.0));
  printf("0.0/0.0 is %s\n", Show_Classification(0.0/0.0));
  printf("DBL_MIN/3 is %s\n", Show_Classification(DBL_MIN/3));
  printf("-0.0 is %s\n", Show_Classification(-0.0));
  printf("10.5 is %s\n", Show_Classification(10.5));
  return 0;
}

The output of the above code will be:

1.0/0.0 is inf.
0.0/0.0 is nan.
DBL_MIN/3 is subnormal.
-0.0 is zero.
10.5 is normal.

❮ C <math.h> Library