Simple addition

Terry Reedy tjreedy at udel.edu
Mon Feb 16 18:53:41 CET 2004


"Piet van Oostrum" <piet at cs.uu.nl> wrote in message
news:wzisi7b9nj.fsf at cs.uu.nl...
> >>>>> Brian <pythonista_at_sbcglobal_dot_net at invalid.net> (B) wrote:
>
> >>>> print str(0.1)
> B> 0.1
>
> B> It's important to realize that this is, in a real sense, an illusion:
the
> B> value in the machine is not exactly 1/10, you're simply rounding the
> B> display of the true machine value."
>
> On the other hand, python could have done better.

Python gives you a choice between most exact and 'pleasant'.  This *is*
better, in my opinion, than no choice.

> There are algorithms to
> print floating point numbers properly with a more pleasant output[1]:
> in this particular case python could have given "0.1" also with "print
0.1".

What?  In 2.2:
>>> print 0.1
0.1

did this change in 2.3?

> Unfortunately most C libraries only use the stupid algorithm which often
> gives some useless digits.

They are not useless if you want more accuracy about what you have and what
you will get with further computation.  Tracking error expansion is an
important part of designing floating point calculations.

> This is because ideally it should print the representation with the least
> number of digits that when read back gives the same internal value as the
> number printed. In this case that is obviously "0.1".

This is opinion, not fact.  Opinions are divided.

Terry J. Reedy





More information about the Python-list mailing list