[NTLUG:Discuss] The wrong computation example from the newsgroup
Fred James
fredjame at concentric.net
Sat Mar 17 23:46:58 CST 2001
That is interesting. Using a Red Hat 6.2 box, I took this two steps further.
First I added these two lines of code just to look at pieces:
printf("%d\n", (int) ((a/b)*0.3));
printf("%d\n", (int) (c*0.7));
Second I changed the type of a, b, and c to float, initializing their values to 60.00, 6.00, and 10.00 respectively.
In both cases the result of the original printf line remained "10 = 9", and in both cases the results of my two new
lines were 2 and 6 respectively.
Then, just as a point of interest, changing the original printf statement to
printf("%2.0f = %2.0f\n", (((60/6)*0.3) + (10*0.7)), ((( a/b)*0.3) + ( c*0.7)))
did yield "10 = 10"
What up with that?
Chris Cox wrote:
> Here's the original problem... for those NTLUGgers who want a challenge.
>
> > From: John Smith (jmsith at generic.com)
> > Subject: Can linux be trusted?
> > Newsgroups: comp.lang.c, comp.os.linux.development.apps, comp.os.linux.development.system, comp.sys.be.programmer,
> > comp.sys.mac.programmer.misc, comp.unix.bsd.freebsd.misc, comp.unix.bsd.misc, gnu.gcc
> > Date: 2001-02-28 17:52:12 PST
> >
> >
> >
> > "
> > If you've got a minute, and one of those 'bleeding edge' OSes, try compiling
> > with no optimizations and running this for fun:
> >
> > 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);
> > }
> >
> > (BTW the lameness filter defies logic, that was the best I could do with the
> > C snippet)
> >
> > A friend was tortured for a few hours doing an assignment until I took a
> > look at the code and realized the problem boiled down to something that can
> > be reduced to this snippet.
> >
> > I compiled this with default compiler settings on every platform I could
> > find. This means Digital Unix 4.0, OpenVMS 7.2, Solaris 8, IRIX 6.4, HP-UX
> > 10.20, FreeBSD 4-STABLE, OpenBSD 2.8 and various Linux distros, from ancient
> > to cutting edge - both with gcc and any commercial compilers that happened
> > to be available at each box.
> >
> > 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 get
> > 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
> >
> _______________________________________________
> http://ntlug.org/mailman/listinfo/discuss
--
Consider Fractals
a large, calm, black cat
who seems often to be my spiritual leader.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://ntlug.org/pipermail/discuss/attachments/20010317/de8fb3c0/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fredjame.vcf
Type: text/x-vcard
Size: 150 bytes
Desc: Card for Fred James
Url : http://ntlug.org/pipermail/discuss/attachments/20010317/de8fb3c0/fredjame.vcf
More information about the Discuss
mailing list