C++ Standard Library C++ STL Library

C++ <cmath> - FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL



The C++ <cmath> FP_NORMAL, FP_SUBNORMAL, FP_ZERO, FP_INFINITE, FP_NAN macros represents distinct category of floating-point numbers and possible values returned by fpclassify function. These macros expand to an integer constant expression.

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.

Definition in the <cmath> header file is:

#define FP_NAN       /* implementation defined */
#define FP_INFINITE  /* implementation defined */
#define FP_ZERO      /* implementation defined */
#define FP_SUBNORMAL /* implementation defined */
#define FP_NORMAL    /* implementation defined */

Example:

The example below shows the usage of these macros.

#include <iostream>
#include <cfloat>
#include <cmath>

using namespace std;

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 (){
  cout<<"1.0/0.0 is "<<Show_Classification(1.0/0.0)<<"\n";
  cout<<"0.0/0.0 is "<<Show_Classification(0.0/0.0)<<"\n";
  cout<<"DBL_MIN/3 is "<<Show_Classification(DBL_MIN/3)<<"\n";
  cout<<"-0.0 is "<<Show_Classification(-0.0)<<"\n";
  cout<<"10.5 is "<<Show_Classification(10.5)<<"\n";
  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++ <cmath> Library