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

jimc at ucla-cs.UUCP jimc at ucla-cs.UUCP
Sat Dec 14 04:20:02 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.
>
>Can anyone recommend a way to quickly compare a float against
>INFINITY?
I munged a "C" library with emulated floating point (Microsoft format) to
use an 8087 (IEEE format), but having no sources for the compiler I couldn't
hack the constant interpreter.  I got around it this way:
   short pi[4] = {0x4008,0x1234,etc,etc};
   double x, y;
   x = y * *(double *)pi
A similar method should work for you.  It's ugly but it works, and a
#define macro neatens up the code a little.

James F. Carter            (213) 206-1306
UCLA-SEASnet; 2567 Boelter Hall; 405 Hilgard Ave.; Los Angeles, CA 90024
UUCP:...!{ihnp4,ucbvax,{hao!cepu}}!ucla-cs!jimc  ARPA:jimc at locus.UCLA.EDU



More information about the Comp.lang.c mailing list