[gnu.gcc] IEEE FLP question: Use of NAN and Inf

phd_ivo@gsbacd.uchicago.edu (12/19/89)

******************************************

Can someone please tell me how to use the NAN
definition in math.h?

	double d;
	d=(double) NAN;
	printf("%d", d==(double) NAN);

prints 0 in NeXT version 16 (Gnu version 1.34).
What am I doing wrong?

Moreover, can someone please tell me how to define
the IEEE double's of +Inf, -Inf, and NaN, so that
I can assign and test for them?

Will standard operations like + or - generate an
exception if attempted on NaN? That would be useful
for debugging.

Help is appreciated.

/ivo

 

rfg@ics.uci.edu (Ron Guilmette) (12/20/89)

In article <6788@tank.uchicago.edu> phd_ivo@gsbacd.uchicago.edu writes:
>
>Moreover, can someone please tell me how to define
>the IEEE double's of +Inf, -Inf, and NaN, so that
>I can assign and test for them?

That's a damn good question, and one for which I would like an answer
as well.

The basic problem appears to be that there is no such thing as a
"bitwise-verbatim" conversion in C (or in GCC).  Thus, you cannot
take an arbitrary bit pattern and do anything like the following:

	float f = (float) 0x80000000;

without getting the (unwanted) effect of int => float conversion.

I suggest that this problem may warrant a small syntax extension for
GCC.  How about if casts could include a trailing `=' to indicate
"bitwise-verbatim" conversion.  Thus:

	float f = (float = ) 0x80000000;

Would give you an unmangled bit pattern in the mantle of a C float.
This would allow you to implement either MACROs or const's for all
of the Infinities, NaN's and other weird things that you would ever
want or need.

If this were to be implemented, it would seem reasonable to limit the
use of this "bitwise-verbatim" casting to builtin floating-point types
only.

// rfg

eggert@twinsun.com (Paul Eggert) (12/20/89)

phd_ivo@gsbacd.uchicago.edu writes:

	Moreover, can someone please tell me how to define
	the IEEE double's of +Inf, -Inf, and NaN, so that
	I can assign and test for them?

Here's how.

	#define Inf (1e300 * 1e300)
	#define NaN (Inf - Inf)

`-Inf' gets you minus infinity.

There are lots of NaNs; the above method gives you only one of them.
Use isnan(X) to test whether X is a NaN.
If your vendor doesn't supply isnan() (sigh), this should work:

	int isnan(x) double x; {return x!=x;}

Warning: GCC 1.36 is buggy in this area; 1.37 will have some fixes.

Eventually GCC should have full IEEE support rather than relying on vendors.

mac@rhea.ardent.com (Mike McNamara) (12/21/89)

rfg>  In article <258EB169.7950@paris.ics.uci.edurfg> rfg@ics.uci.edu (Ron Guilmette) writes:
rfg>  
rfg>  In article <6788@tank.uchicago.edurfg> phd_ivo@gsbacd.uchicago.edu writes:

phd_ivo> 
phd_ivo> Moreover, can someone please tell me how to define
phd_ivo> the IEEE double's of +Inf, -Inf, and NaN, so that
phd_ivo> I can assign and test for them?

rfg>  
rfg>  That's a damn good question, and one for which I would like an answer
rfg>  as well.
rfg>  
rfg>  The basic problem appears to be that there is no such thing as a
rfg>  "bitwise-verbatim" conversion in C (or in GCC).  Thus, you cannot
rfg>  take an arbitrary bit pattern and do anything like the following:
rfg>  
rfg>  	float f = (float) 0x80000000;
rfg>  
rfg>  without getting the (unwanted) effect of int =rfg> float conversion.
rfg>  
	Try 
		float f;
		*(int*)&f = 0x80000000;

	this works on every c compiler I'm aware of.

rfg>  // rfg

	-mac

--
Michael McNamara	(St)ardent, Inc.		mac@ardent.com

phd_ivo@gsbacd.uchicago.edu (12/22/89)

> 
>	Try 
>		float f;
>		*(int*)&f = 0x80000000;
> 
>	this works on every c compiler I'm aware of.

Yes, with floats, but not with doubles. Moreover, since it's not a
preprocessor definition, its ugly when your code should read

	f=NaN;

at some spot. The best ways to deal with these IEEE numbers (thanks for all
the mail) are probably:

	#define NaN (0.0/0.0)
	#define Inf (1.0/0.0)
	#define isNaN (x!=x)

One problem here is that I would like to generate a signal (exception/interupt)
when an invalid operation is attempted. However, if this is possible on some
computer(s), then the above definition will generate this upon assignment---
which is clearly not the intended result.

/ivo