Xref: utzoo comp.lang.c:11805 comp.std.c:265 sci.math:4379 Path: utzoo!utgpu!water!watmath!clyde!bellcore!rutgers!mailrus!ames!killer!vector!rpp386!jfh From: jfh@rpp386.UUCP (John F. Haugh II) Newsgroups: comp.lang.c,comp.std.c,sci.math Subject: Re: Floating point puzzle Summary: fun with unions ... Keywords: floating point representation Message-ID: <5337@rpp386.UUCP> Date: 11 Aug 88 15:34:33 GMT References: <3117@emory.uucp> <15370@apple.Apple.COM> Reply-To: jfh@rpp386.UUCP (The Beach Bum) Organization: HASA, "S" Division Lines: 48 In article <15370@apple.Apple.COM> turk@apple.com.UUCP (Ken "Turk" Turkowski) writes: >The problem is that floats are converted to doubles (or extendeds on the >macintosh). A double has an 11-bit exponent, whereas a float only has 8. >If you print out the next word, you'll see that the two hex representations >differ somewhere in the next 3 bits. correct. obviously there is a difference. here is how i faked out the conversion rules using a union: Script is typescript, started Thu Aug 11 10:27:39 1988 Subscript out of range. 1 - rpp386-> cat puzzle.c main () { float x,y; union { long ul; float uf; } ux, uy; ux.uf = x = 1.0/10.0; uy.uf = y = 1677721.0/16777216.0; printf("x: %08x:%08x, ux.ul: %08x,",ux.uf,ux.ul); printf("%20.17f\n",x); printf("y: %08x:%08x, uy.ul: %08x,",uy.uf,uy.ul); printf("%20.17f\n",y); } 2 - rpp386-> cc puzzle.c puzzle.c 3 - rpp386-> a.out x: a0000000:3fb99999, ux.ul: 3dcccccd, 0.10000000149011612 y: 00000000:3fb99999, uy.ul: 3dccccc8, 0.09999996423721313 4 - rpp386-> logout Not a terminal: Not a character device John's Words of Wisdumb - A great many people think they are thinking when they are merely rearranging their prejudices. -- William James Script done Thu Aug 11 10:27:59 1988 the output shows the float, which was passed as a long for the ux.ul and uy.ul outputs, are different in 32 bits. -- John F. Haugh II +--------- Cute Chocolate Quote --------- HASA, "S" Division | "USENET should not be confused with UUCP: killer!rpp386!jfh | something that matters, like CHOCOLATE" DOMAIN: jfh@rpp386.uucp | -- apologizes to Dennis O'Connor