09-10-2023, 01:50 AM
If one ever needs to determine if a calculation generates a Nan result or an INF result, they can always use the following little snippet of code:
Save as isnan.h:
And the test code to check for NaN and Inf:
Notice the output is:
0 / 0 is NaN but is *not* INF.
1 / 0 is NaN and is also INF.
0 / 1 is neither NaN, nor is it INF.
Save as isnan.h:
Code: (Select All)
#include <stdio.h>
#include <math.h>
#include <float.h>
int IsNan (long double n);
int IsInf (long double n);
IsNan (long double n) { return -isnan(n); }
IsInf (long double n) { return -isinf(n); }
And the test code to check for NaN and Inf:
Code: (Select All)
DECLARE LIBRARY "isnan"
FUNCTION IsNan% (BYVAL n AS _FLOAT)
FUNCTION IsInf% (BYVAL n AS _FLOAT)
END DECLARE
PRINT IsNan(0 / 0), IsInf(0 / 0)
PRINT IsInf(1 / 0), IsInf(1 / 0)
PRINT IsInf(0 / 1), IsInf(0 / 1)
Notice the output is:
0 / 0 is NaN but is *not* INF.
1 / 0 is NaN and is also INF.
0 / 1 is neither NaN, nor is it INF.