How to detect NaN's

Alex Martelli martelli at cadlab.sublink.ORG
Tue Jun 4 18:00:49 AEST 1991


warren at atmos.washington.edu (David Warren) writes:

:In article <1991May30.204332.16506 at litwin.com> vlr at litwin.com (Vic Rice) writes:
	...
:   I have encountered a problem on a DECStation 5000 using the Mips Fortran
	...
:how about linking with this c function:
:
:int *check_nan_(num)
:/*make sure num != NaN*/
:float *num;
:{
:    int true=1,false=0;
:    if(isnan((double)num))return(&true);
:    else return(&false);
:}
:
:then do
:
:ISTAT = CHECK_NAN(RVAL)

There are several problems here!  Integer and Logical function results are to
be returned by-value, not by-reference, on DECstations as well as all other
platforms I know; Fortran .TRUE. is generally -1, not 1; it is dangerous
for a C function to return the address of a variable of 'automatic' storage
class, since the storage it is pointing to is "going away" (i.e. about to be
recycled for automatic variables of further called functions) just after the
execution of the return statement; it is wrong to cast num, which is the
ADDRESS of a float, to a double - what you want to do is cast the POINTED-TO
value, *num, and such a cast is not needed anyway since floats are promoted
to double by default in C when used in expressions.
All in all, the idea is not bad, but the implementation should preferably be
something like:

int checknan(num) float *num; {
    return isnan(*num) ? -1 : 0 ;
}

to be declared and used like this:

	LOGICAL CHECKNAN
	...
	IF(CHECKNAN(RVAL)) THEN
		WRITE(*,*) 'HELP! RVAL is Not A Number!'
	ELSE
		WRITE(*,*) 'All is fine so far...'
	ENDIF
-- 
Alex Martelli - CAD.LAB s.p.a., v. Stalingrado 53, Bologna, Italia
Email: (work:) martelli at cadlab.sublink.org, (home:) alex at am.sublink.org
Phone: (work:) ++39 (51) 371099, (home:) ++39 (51) 250434; 
Fax: ++39 (51) 366964 (work only), Fidonet: 332/407.314 (home only).



More information about the Comp.unix.ultrix mailing list