C programming math.h isinf


In C programming the <math.h> isinf() function determine if the argument is infinity.The infinity can either be positive or negative.

bool isinf(float x);
bool isinf(double x);
bool isinf(long double x);

Parameters:
x -A floating point type value.

Return type
bool -non zero value or true(1) if the argument is infinite.

All the three functions given above are same.The only difference is in their parameter type.

Code example

printf( “%i” , isfinite( NAN ) );
printf( “\n%i” , isfinite( 123.45 ) );
printf( “\n%i” , isfinite( INFINITE ) );

Output,

0
0
1

The return value is 1 only in the case of INFINITE as argument.Note the ‘INFINITE’ macro represent the infinite value.



Some points to note

The function will determine the argument base on the specific semantic types.This means if the argument passed is wider than it’s semantic type than it is converted to it’s semantic type and the determination is based on that type.To understand what this means consider that we are passing a very large number -not representable by any floating point type-in this case the argument type cannot represent the value so here the argument is treated as ‘infinite’ value and the function returns false signifying that the argument is not finite.A code example is given below.

Code example

printf( “%i” , isfinite( 2e200 ) );

printf( “\n%i” , isfinite( 2e350 ) );

printf( “\n%i” , isfinite( -INFINITE ) );

printf( “\n%i” , isfinite( “INFINITE” ) ); //error, cannot accept string argument

Output in Code::Blocks,

0
1
1

Note -INFINITE is still an infinite value.