Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IsNan and IsInf Functions
#1
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:
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.
Reply


Messages In This Thread
IsNan and IsInf Functions - by SMcNeill - 09-10-2023, 01:50 AM
RE: IsNan and IsInf Functions - by Dimster - 09-10-2023, 03:12 PM
RE: IsNan and IsInf Functions - by mnrvovrfc - 09-10-2023, 10:11 PM



Users browsing this thread: 1 Guest(s)