float / rounding question

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Sat Mar 8 18:52:40 EST 2008


Mark Dickinson schreef:
> On Mar 7, 11:23 pm, Steven D'Aprano <st... at REMOVE-THIS-
> cybersource.com.au> wrote:
>> On Fri, 07 Mar 2008 23:12:27 +0100, Piet van Oostrum wrote:
>>> Sorry to come in so late in this discussion. Although it is correct to
>>> say that many real numbers that have an exact decimal representation
>>> cannot be exactly represented in binary, that is no excuse to print 53.6
>>> as 53.600000000000001. This is just lousy printing and the fact that
>>> this kind of question comes up every week shows that it is confusing to
>>> many people.
>> Good. That's a feature, not a bug.
> 
> Even so, it's not clear that Python's current behaviour couldn't be
> improved.  I have a mild dislike of the lack of consistency in the
> following, which arises from Python arbitrarily stripping trailing
> zeros from the result returned by the C library functions:
> 
>>>> 10.1
> 10.1
>>>> 10.2
> 10.199999999999999
>>>> 10.3
> 10.300000000000001
>>>> 10.4
> 10.4

Actually I don't see what all the fuss is about. If you want a nicely 
rounded number, use print or str() or the % operator.

*Only* when you use repr() or when the interactive interpreter uses it 
to print the value of the expression, you get something that doesn't 
look as nice.

-- 
The saddest aspect of life right now is that science gathers knowledge
faster than society gathers wisdom.
   -- Isaac Asimov

Roel Schroeven



More information about the Python-list mailing list