[NTLUG:Discuss] The wrong computation example from the newsgroup
Fred James
fredjame at concentric.net
Sun Mar 18 12:16:14 CST 2001
Having read all of the replies to date I must admit to feeling silly for having been sucked in - but sucked in I was.
I appreciate the cool heads in the group for seeing through to the core issue and sharing that knowledge.
Computers do what they are told, and since people must decide whether the results are appropriate an understanding of how
the computer accomplishes a task is necessary.
My humble thanks to all.
Peter Koren wrote:
> Oh well, I've seen others have nailed this but here was my approach to
> the problem:
>
>
> int main(void)
> {
> int a = 60, b = 6, c = 10;
> double x;
>
> x = 7.0 - (c*0.7);
> printf("(int)(c*0.7)=%d\n\n", (int)(c*0.7));
> printf("(float)(0.7)=%26.19e\n\n", (0.7));
> printf("x=7.0-(c*0.7)=%26.19e\n\n", x);
>
> exit(1);
> }
>
> which yields:
>
> (int)(c*0.7)=6
>
> (float)(0.7)= 6.9999999999999995559e-01
>
> x=7.0-(c*0.7)= 4.4408920985006261617e-16
>
> As Steve Baker said, if you depend on exact floating point results, you
> are apt to get screwed. Some base ten numbers like 0.3 and 0.7 can not
> be exactly represented in binary. Then there is the semantics issue of
> what should the result be. BTW, The Python community is debating
> changing the semantics of integer arithmetic and there is no consensus
> yet.
>
>
> 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
> _______________________________________________
> 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/20010318/31266fab/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/20010318/31266fab/fredjame.vcf
More information about the Discuss
mailing list