/-INFINITY and NaN

David Tribble tribble_acn%uta.csnet at csnet-relay.arpa
Mon Dec 23 20:13:18 AEST 1985


A question was raised as to how to compare a float (or double) to
the ANSI +/-infinity and Not-a-Number values.  One suggestion was to
use a struct; another way (though arguably not elegant) is-
	#define INFINITY	(*(float*)"\177\200\0\0")
	#define NaN		(*(float*)"\177\277\377\377")
(I'm not certain of the bit patterns, however). The idea is to cast a
string, which is coerced into a char*, into a float*, thus interpreting
the string chars as elements of a float data value.
The same idea can be extended to doubles.
(This works on one compiler that I know of, running on an 8086).

While this may not seem elegant, it has the advantage of not requiring
any structure initilizations.

	David R. Tribble	<tribble_acn at uta>



More information about the Comp.lang.c mailing list