ado@elsie.UUCP (04/12/84)
I compiled the following program with the 4.1bsd C compiler:
main()
{
int i;
i = 100;
i = i * .2;
printf("%d\n", i);
i = 100;
i *= .2;
printf("%d\n", i);
}
I got this output:
20
0
Is this a bug?
--
UUCP: decvax!harpo!seismo!rlgvax!cvl!elsie!ado
DDD: (301) 496-5688
peters@cubsvax.UUCP (04/15/84)
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."
smq@hlhop.UUCP (Steven M. Queriolo) (04/16/84)
I tried both i=100; i *= .2; and i=100; i /= 3.3; on our Unix System V (Release 2.0) and both of these 'bugs' produced the correct answers. -- Steven M. Queriolo AT&T Bell Laboratories ..........hlhop!smq
hansen@pegasus.UUCP (04/16/84)
I waited a couple of days before following up on this question to see if anyone else posted a reply. None has so far that I've seen, so here goes. The reported problem: integer *= float is not the same as integer = integer * float This is indeed a definite bug which has been around for many a moon. (It has been fixed in the System V compiler.) Dennis Ritchie himself commented on this very same problem in this very same forum last fall, acknowledging that it was a bug. Fix those compilers! Tony Hansen pegasus!hansen
allan@qtlon.UUCP (04/24/84)
<no, no, no. Please don't eat me> This is the same on 4.2bsd also. I Thought I would try it on our C compiler on our Prime and the results were 19 19 So it is sort of better, with floats the result was 19.999996 so it was almost 20. -- UUCP: {decvax,cbosgd,vax135}!qusavx!qtlon!allan ukc!qtlon!allan Phone: +44 1 637 7061
chris@basser.SUN (Chris Maltby) (04/26/84)
>This is the same on 4.2bsd also. I Thought I would try it on our C >compiler on our Prime and the results were >19 >19 >So it is sort of better, with floats the result was 19.999996 so it was >almost 20. Everyone knows that floating point units on PR1ME machines are up the sh*t. If anyone would like proof of this, I can send you a nice program to run. Chris Maltby University of Sydney