Rounding curiosity

Delaney, Timothy C (Timothy) tdelaney at avaya.com
Wed Nov 17 01:27:01 EST 2004


PyBo wrote:

> To all those who are feeling so smug and superior, perhapts you could
> have noticed that I was pointing out that the round() function does
> not perform as documented?

Your two initial emails never mentioned the round() documentation. I
fail to see how that counts as "pointing out" a possible deficiency in
the docs.

BTW, round() performs exactly as documented - it returns a floating
point number.

As for feeling smug and superior - how the hell did you construe that?
Not understanding floating point arithmetic is a very common mistake,
and many otherwise experienced programmers are surprised by it when they
come to Python because Python doesn't lie to you about it.

People (including myself) kindly pointed you towards documentation that
should ensure that you never make the mistake again. It's unlikely that
such people (including one of the most important developers of Python)
are likely to do so again. I *strongly* advise you to read the following
link.

http://www.catb.org/~esr/faqs/smart-questions.html

> By the way,
>    print "%0.2f" % (5e8/(1024*1024))
> yields the same result as
>    print "%0.2f" % round(5e8/(1024*1024),2)
> so the 'round()' portion of the expression is superfluous.

In that particular case, sure - because you're only concerned about two
decimal places. Whatever's beyond that doesn't matter. It's only if you
*care* about the additional accuracy that it matters.

It's irrelevant though - the important part there is showing you how to
display 2 decimal places as you want.

Tim Delaney



More information about the Python-list mailing list