fpclassify() method in C/C++ with Examples
The fpclassify() function are defined in header math.h header in C and cmath library in C++. This function is used to get the value of type int that matches one of the classification macro constants (depending on the value of x).
Syntax:
int fpclassify(int x);
int fpclassify(float x);
int fpclassify(double x);
int fpclassify(long double x);
Parameters: This method accepts a parameter x which is the value to be matched with one of the macro constants of this method. It can be integer, float, double or long double.
Return Value: This function returns integer values for the macro constants as follows:
- FP_INFINITE: When the specified value is Positive or negative infinity
- FP_NAN: When the specified value is not a Number
- FP_ZERO: When the specified value is a zero.
- FP_SUBNORMAL: When the specified value is a positive or negative denormalized value
- FP_NORMAL: When the specified value is a positive or negative normalized non-zero value
Below example demonstrate the use of fpclassify() method:
#include <iostream>
#include <math.h>
using namespace std;
void fpclassification( double x)
{
switch (fpclassify(x)) {
case FP_INFINITE:
cout << "Infinite Number \n" ;
break ;
case FP_NAN:
cout << "Not a Number \n" ;
break ;
case FP_ZERO:
cout << "Zero \n" ;
break ;
case FP_SUBNORMAL:
cout << "Subnormal value \n" ;
break ;
case FP_NORMAL:
cout << "Normal value \n" ;
break ;
default :
cout << "Invalid number \n" ;
}
}
int main()
{
double a = 1.0 / 0.0;
cout << "For 1.0/0.0: " ;
fpclassification(a);
double b = 0.0 / 0.0;
cout << "For 0.0/0.0: " ;
fpclassification(b);
double c = -0.0;
cout << "For -0.0: " ;
fpclassification(c);
double d = 1.0;
cout << "For 1.0: " ;
fpclassification(d);
return 0;
}
|
Output:
For 1.0/0.0: Infinite Number
For 0.0/0.0: Not a Number
For -0.0: Zero
For 1.0: Normal value
Last Updated :
25 Mar, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...