i = i * f vs. i *= f

peters at cubsvax.UUCP peters at cubsvax.UUCP
Mon Apr 16 06:12:31 AEST 1984


Looks like you found a real bug!  on the 4.1bsd compiler, the following
program -- which compares i = i / f with i /= f -- gives the results indicated.

Obviously, the problem is that when the compiler encounters
"int op= float;" it casts the float to an int before doing the evaluation,
instead of promoting the int to a float, then casting the result.

According to K&R, p191, the two expressions "may be taken as equivalent"
(slight paraphrase), which is *very* misleading if this isn't a bug.  They
then say, "... however, [for "op=" ] E1 [left-hand side] is evaluated only
once."  This is opaque to me.  Can someone explain it?  I. e., does that
mean it's not a bug?  It's egregious [look it up!] in any case.

	main()
	{
		int	i;

		i = 100;
		i = i / 3.3;
		printf("i = i/3.3 gives: %d\n", i); /* gives 30, as expected */
		i = 100;
		i /= 3.3;
		printf("i /= 3.3 gives: %d\n", i);  /* gives 33!!!!   */
	}

{philabs,cmcl2!rocky2}!cubsvax!peters            Peter S. Shenkin 
Dept of Biol. Sci.;  Columbia Univ.;  New York, N. Y.  10027;  212-280-5517
"In accordance with the recent proclivity for clever mottos, this is mine."



More information about the Comp.lang.c mailing list