[NTLUG:Discuss] The wrong computation example from the newsgroup
Christopher Browne
cbbrowne at localhost.brownes.org
Sun Mar 18 12:30:25 CST 2001
On Sat, 17 Mar 2001 23:05:08 CST, the world broke into rejoicing as
Chris Cox <cjcox at acm.org> said:
> Here's the original problem... for those NTLUGgers who want a challenge.
> > On all Linux distros, and only on Linux distros, ranging from an ancient
> > Slackware setup to the latest Red Hat, I get 9=10. On everything else, I ge
t
> > 10=10. Go figure, and remember that the whole OS is compiled with that.
> >
> > I think I'll just stick to FreeBSD as far as my intel boxes are concerned.
> > "
> > http://slashdot.org/comments.pl?sid=01/02/28/1712232&cid=93
Note that it's a straight _falsehood_ that this is true for "all Linux
distributions."
--- % Cut here % ---
int main(void)
{
int a = 60, b = 6, c = 10;
printf("%d = %d\n",
(int) (((60/6)*0.3) + (10*0.7)),
(int) ((( a/b)*0.3) + ( c*0.7)));
exit(1);
}
--- % Cut here % ---
Results of testing, on two machines, are as follows:
% hostname
chvatal
% arch
i686
% gcc ctest.c -o ctest
% ./ctst
10=9
% gcc ctest.c -O2 -o ctest
% ./ctst
10=10
% rlogin dantzig
Password:
...
% arch
alpha
% gcc ctest.c -o ctest
% ./ctst
10=10
% gcc ctest.c -O2 -o ctest
% ./ctst
10=10
%
Note that when I ran it on an Alpha-based system, the "error" did not
occur. Apparently a slightly different rounding takes place there,
thus meaning that when the second value gets truncated by the int
cast, the value had been very slightly _greater_ than 10.
It's fairly surprising that the compiler evaluates the value
differently from the computation in the compiled program, and the
reasons for that are well worth investigating. But it should come as
no shock to the gentle reader for a floating point approximation to 10
to be either slightly larger, or slightly smaller than 10.
--
(concatenate 'string "aa454" "@freenet.carleton.ca")
http://vip.hex.net/~cbbrowne/oses.html
Rules of the Evil Overlord #17. "When I employ people as advisors, I
will occasionally listen to their advice."
<http://www.eviloverlord.com/>
More information about the Discuss
mailing list