C++ Standard Library C++ STL Library

C++ <cmath> - fpclassify() Function



The C++ <cmath> fpclassify() function 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

int fpclassify (float x);
int fpclassify (double x);
int fpclassify (long double 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 <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

5