/-INFINITY and NaN

Andrew Koenig ark at alice.UucP
Thu Dec 26 01:04:58 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>

It has the disadvantage of being extremely implementation-specific.

In general, there may be variation from one implementation to another
as to the location of the various bits within a word.  There may also
be alignment problems with this technique on some machines.



More information about the Comp.lang.c mailing list