Floating point puzzle

John F. Haugh II jfh at rpp386.UUCP
Fri Aug 12 01:34:33 AEST 1988


In article <15370 at apple.Apple.COM> turk at 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 at rpp386.uucp          |         -- apologizes to Dennis O'Connor



More information about the Comp.lang.c mailing list