C programming math.h fpclassify


In C programming the <math.h> fpclassify function purpose is to classify it’s argument as NaN,infinite,normal,subnormal,zero or it’s implementation-defined category.

int fpclassify(float x );
int fpclassify(double x );
int fpclassify(long double x);

Parameters:
x -A floating point type value whose category is to be checked for.

Return type
int -Returns the value of the number classification macro appropriate to the value of its argument.

All the functions are same,the only difference is the parameter type accepted by the function.For the function to specify the parameter type it returns certain values signifying that type.The values returned vary from compiler to compiler.For instance,in Code::Blocks Some of the value returned are:

i)If the argument passed is double value the value returned is 1024.
ii)If the argument passed is ‘infinity‘ the value returned is 1280.
iii)If the argument passed is ‘NaN‘ the value returned is 1024.

and so on.Do check the values returned by the function in your compiler.


Some points to note:

 i) If any,argument is represented in a format wider than its semantic type,it is converted to its semantic type.Then classification is based on the type of the argument.This simply means if the value passed as argument has a precision and range wider than the type than it might be converted to the appropriate type first.Sometimes a normal long double type might become subnormal if converted to double type or 0 when converted to float type.

 ii) The value specifying each type is compiler dependent and it will vary from compiler to compiler.For instance,MinGW/Code::Blocks gives 1024 for normal type but,Visual Studio compiler gives -1.Do check out the value for your compiler.

 iii) If int type is passed as parameter the double version is called or another appropriate function may be called depending on your compiler.

Code example

double d1=12 , INF=INFINITY , nan=NAN ;

char c[]=”New text” ;

printf( “%i” , fpclassify( d1 ) );
printf( “\n%i” , fpclassify( INF ) );
printf( “\n%i” , fpclassify( NAN ) );

/*printf( “\n\n%i” , fpclassify( c ) ); //error! fpclassify does not accept string type argument */

Output in Code::Blocks,

1024
1280
256