macro/function
<cmath> <ctgmath>

fpclassify

macro
fpclassify(x)
function
int fpclassify (float x);int fpclassify (double x);int fpclassify (long double x);
Classify floating-point value
Returns a value of type int that matches one of the classification macro constants, depending on the value of x:

valuedescription
FP_INFINITEPositive or negative infinity (overflow)
FP_NANNot-A-Number
FP_ZEROValue of zero
FP_SUBNORMALSub-normal value (underflow)
FP_NORMALNormal value (none of the above)
Note that each value pertains to a single category: zero is not a normal value.

These macro constants of type int are defined in header <cmath> (<math.h>).

In C, this is implemented as a macro, but the type of x shall be float, double or long double.
In C++, it is implemented with function overloads for each floating-point type.

Parameters

x
The value to classify.

Return value

One of the followoing int values: FP_INFINITE, FP_NAN, FP_ZERO, FP_SUBNORMAL or FP_NORMAL.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* fpclassify example */
#include <stdio.h>      /* printf */
#include <math.h>       /* fpclassify, signbit, FP_* */

int main()
{
  double d = 1.0 / 0.0;
  switch (fpclassify(d)) {
    case FP_INFINITE:  printf ("infinite");  break;
    case FP_NAN:       printf ("NaN");       break;
    case FP_ZERO:      printf ("zero");      break;
    case FP_SUBNORMAL: printf ("subnormal"); break;
    case FP_NORMAL:    printf ("normal");    break;
  }
  if (signbit(d)) printf (" negative\n");
  else printf (" positive or unsigned\n");
  return 0;
}

Output:

infinite positive or unsigned


See also