Yet another pcc botch

David Goodenough dg at lakart.UUCP
Tue Jan 24 00:58:47 AEST 1989


lvc at cbnews.ATT.COM (Lawrence V. Cipriani) sez:
] This program demonstrates a bug that is in some versions of pcc:
] 
] main()
] {
] 	float f, g;
] 	unsigned short u, v;
] 	short s, t;
] 
] 	u = 250;
] 	s = 250;
] 
] 	t = 100;
] 	v = 100;
] 
] 	f = t - s;
] 	g = v - u;
] 
] 	printf("f = %f, g = %f\n", f, g);
] }
] 
] When compiled and executed the output is:
] 
] f = -150.000000, g = 65386.000000
] 
] The correct value for g is -150.000000.

No. The correct value for g is 65386.0 [1]. If you read the gospel
according to Kernighan and Ritchie [2], it says that when you do
mathematics, and at least one of the operands is unsigned, and the
other is NOT float or better (as above), it is done in an unsigned
manner. Now last time _I_ checked, -150 was a negative number, which
simply doesn't exist in the land of unsigned. HOWEVER this type
conversion DOES NOT OCCUR ACROSS ASSIGNMENTS.

[1] depends on your size of short. 65386.0 is correct for those with
16 bit shorts. Check your system, as your actual mileage may vary.

[2] :-)
-- 
	dg at lakart.UUCP - David Goodenough		+---+
						IHS	| +-+-+
	....... !harvard!xait!lakart!dg			+-+-+ |
AKA:	dg%lakart.uucp at xait.xerox.com		  	  +---+



More information about the Comp.lang.c mailing list