<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Having read all of the replies to date I must admit to feeling silly for
having been sucked in - but sucked in I was.
<br>I appreciate the cool heads in the group for seeing through to the
core issue and sharing that knowledge.
<br>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.
<br>My humble thanks to all.
<br>
<p>Peter Koren wrote:
<blockquote TYPE=CITE>Oh well, I've seen others have nailed this but here
was my approach to
<br>the problem:
<br>
<p> int main(void)
<br> {
<br> int a = 60, b = 6, c = 10;
<br> double x;
<p> x = 7.0 - (c*0.7);
<br> printf("(int)(c*0.7)=%d\n\n", (int)(c*0.7));
<br> printf("(float)(0.7)=%26.19e\n\n", (0.7));
<br> printf("x=7.0-(c*0.7)=%26.19e\n\n", x);
<p> exit(1);
<br> }
<p>which yields:
<p>(int)(c*0.7)=6
<p>(float)(0.7)= 6.9999999999999995559e-01
<p>x=7.0-(c*0.7)= 4.4408920985006261617e-16
<p>As Steve Baker said, if you depend on exact floating point results,
you
<br>are apt to get screwed. Some base ten numbers like 0.3 and 0.7 can
not
<br>be exactly represented in binary. Then there is the semantics issue
of
<br>what should the result be. BTW, The Python community is debating
<br>changing the semantics of integer arithmetic and there is no consensus
<br>yet.
<br>
<p>Chris Cox wrote:
<br>>
<br>> Here's the original problem... for those NTLUGgers who want a challenge.
<br>>
<br>> > From: John Smith (jmsith@generic.com)
<br>> > Subject: Can linux be trusted?
<br>> > Newsgroups: comp.lang.c, comp.os.linux.development.apps,
comp.os.linux.development.system, comp.sys.be.programmer,
<br>> > comp.sys.mac.programmer.misc, comp.unix.bsd.freebsd.misc,
comp.unix.bsd.misc, gnu.gcc
<br>> > Date: 2001-02-28 17:52:12 PST
<br>> >
<br>> >
<br>> >
<br>> > "
<br>> > If you've got a minute, and one of those 'bleeding edge' OSes,
try compiling
<br>> > with no optimizations and running this for fun:
<br>> >
<br>> > int main(void)
<br>> > {
<br>> > int a = 60, b = 6, c = 10;
<br>> >
<br>> > printf("%d = %d\n", (int) (((60/6)*0.3) + (10*0.7)), (int) (((
a/b)*0.3) +
<br>> > ( c*0.7)));
<br>> >
<br>> > exit(1);
<br>> > }
<br>> >
<br>> > (BTW the lameness filter defies logic, that was the best I could
do with the
<br>> > C snippet)
<br>> >
<br>> > A friend was tortured for a few hours doing an assignment until
I took a
<br>> > look at the code and realized the problem boiled down to something
that can
<br>> > be reduced to this snippet.
<br>> >
<br>> > I compiled this with default compiler settings on every platform
I could
<br>> > find. This means Digital Unix 4.0, OpenVMS 7.2, Solaris 8, IRIX
6.4, HP-UX
<br>> > 10.20, FreeBSD 4-STABLE, OpenBSD 2.8 and various Linux distros,
from ancient
<br>> > to cutting edge - both with gcc and any commercial compilers that
happened
<br>> > to be available at each box.
<br>> >
<br>> > On all Linux distros, and only on Linux distros, ranging from an
ancient
<br>> > Slackware setup to the latest Red Hat, I get 9=10. On everything
else, I get
<br>> > 10=10. Go figure, and remember that the whole OS is compiled with
that.
<br>> >
<br>> > I think I'll just stick to FreeBSD as far as my intel boxes are
concerned.
<br>> > "
<br>> > <a href="http://slashdot.org/comments.pl?sid=01/02/28/1712232&cid=93">http://slashdot.org/comments.pl?sid=01/02/28/1712232&cid=93</a>
<br>> >
<br>> _______________________________________________
<br>> <a href="http://ntlug.org/mailman/listinfo/discuss">http://ntlug.org/mailman/listinfo/discuss</a>
<br>_______________________________________________
<br><a href="http://ntlug.org/mailman/listinfo/discuss">http://ntlug.org/mailman/listinfo/discuss</a></blockquote>
<pre>--
Consider Fractals
a large, calm, black cat
who seems often to be my spiritual leader.</pre>
</html>