On Tue, Nov 14, 2006 at 12:29:05PM +1100, Carlo Sogono wrote:
> Steve Lindsay wrote:
> >I suspect it's related to this:
> >
> >http://www.python.org/doc/faq/general.html#id34
>
> Thanks. Without having gone through all the links everyone posted, I
> have come across this line in your/Tim's link:
>
> "Again, this has nothing to do with Python, but with the way the
> underlying C platform handles floating point numbers, and ultimately
> with the inaccuracy you'll always have when writing down numbers as a
> string of a fixed number of digits."
It's not C's fault either, it's a result of the machine
not being infinite in size. Damn machine!
> However I'm pretty sure C applications do not handle floats the same
> way. This still gives me a 'normal' response:
>
> int main() {
> float a = 8.0, b = 0.45, res;
>
> res = a + b;
> printf("%f\n", res);
>
> return (EXIT_SUCCESS);
> }
>
> So my new question is, why doesn't C handle floats the same way?
Ah, but it does, look:
$ python
>>> "%f" % (8.0 + 0.45)
'8.450000'
and modifying your C program:
printf("%.20f\n", res);
gives
8.44999980926513671875
Also read
"What Every Computer Scientist Should Know About Floating-Point
Arithmetic"
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Regards,
Matt
psst! The exam question will be on Theorem 14!
_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders