Determining IEEE NaN and INF on RISC Ultrix

Mark G. Johnson mark at mips.COM
Sun Sep 23 00:39:03 AEST 1990


In article <59582 at bbn.BBN.COM> fkittred at spca.bbn.com (Fletcher Kittredge) writes:
 >
 >I need to determine whether a double precision floating point number is
 >a IEEE NaN or INF (+ or -) value on Sun, HP and DEC RISC systems.  Sun


You could resort to coding your own routines.  Just as an simple-simon
demonstration of this possibility, the following hack was thrown together
in 3 minutes.  It was tested on the DECstation 3100 and works under
compiler optimization levels 0, 1, 2, and 3.  These codes are far from
optimum, but they give a flavor of how you might proceed.  Flame if you
must, keeping in mind that the goal below is getting an answer, not efficiency.

The basic ideas are (1) if you compare IEEE NaN with anything, the comparison
fails.  Even a==a fails if a is NaN.  (2) Infinity is what you get when you
overflow a computation.
-----------------------------------------------------------------------------
#include <stdio.h>
#include <math.h>

/* test if argument is IEEE double precision NaN */
int	inn(a)
double	a;
{
	if(a==a) return(0) ;
	else return(1);
}

/* test if argument is IEEE double precision Infinity (+ or -) */
int	iff(b)
double	b;
{
	double	c, d;
	/* laboriously create +Infinity and -Infinity */
	c = 1e10;
	c = (c*c);
	c = (c*c);
	c = (c*c);
	c = (c*c);
	c = (c*c);
	c = (c*c);
	d = -1.0 *c;
	if((b==c) || (b==d)) return(1);
	else return(0);
}



main()
{
	double	x, y, z, q, v ;

	/* set z equal to +Infinity */
	z = 1e10 ;
	z = z*z ;
	z = z*z ;
	z = z*z ;
	z = z*z ;
	z = z*z ;
	z = z*z ;
	z = z*z ;
	z = z*z ;

	/* set y equal to NaN */
	y = z/z ;


	/* set x equal to 1.0 */
	x = 1.0 ;


	printf(" Is %le a NAN?  answer: %d \n", x, inn(x));
	printf(" Is %le a NAN?  answer: %d \n", y, inn(y));
	printf(" Is %le a NAN?  answer: %d \n", z, inn(z));

	printf(" Is %le an infinity?  answer: %d \n", x, iff(x));
	printf(" Is %le an infinity?  answer: %d \n", y, iff(y));
	printf(" Is %le an infinity?  answer: %d \n", z, iff(z));

}
-- 
 -- Mark Johnson	
 	MIPS Computer Systems, 930 E. Arques M/S 2-02, Sunnyvale, CA 94086
	(408) 524-8308    mark at mips.com  {or ...!decwrl!mips!mark}



More information about the Comp.unix.ultrix mailing list