How to test for +/- INFINITY, etc. in C with IEEE

Joseph S. D. Yao jsdy at hadron.UUCP
Wed Dec 18 18:22:31 AEST 1985


In article <993 at turtlevax.UUCP> ken at turtlevax.UUCP (Ken Turkowski) writes:
>I would like to be able to test for INFINITY, NaN, and other such
>things within C.  Now, if I try to cast the value for INFINITY
>(0x7F800000 in single) to a float,
>	((float)(0x7F800000))
>the C compiler changes it into 0x4EFF0000 which does not compare the
>same as 0x7F800000.
> ...
>The C compiler should assume that only decimal constants need be
>converted from integer to floating point.

The object 0x80000000 has data type integer, and means a particular
(if somewhat indeterminate) integer.  If I had a constant 0x1 or 01
or 1, I'd expect them all to equal 1.  Their use as bit patterns on
particular machines is wholy machine-dependent.  Try it on Cottrell's
ternary (trinary?) machine!

>Can anyone recommend a way to quickly compare a float against
>INFINITY?  I can't use a subroutine, because the C compiler changes the
>float to a double.

union liffle {		/* long int / float union is a liffle. */
	long int lf_l;
	float lf_f;
};
const union liffle inf = { 0x7f80000 };	/* initialises 1st elem */
#define INFINITY (inf.lf_f)
-- 

	Joe Yao		hadron!jsdy at seismo.{CSS.GOV,ARPA,UUCP}



More information about the Comp.lang.c mailing list