round issue

Robin Becker robin at reportlab.com
Tue Jul 13 12:06:36 EDT 2010


On 12/07/2010 19:59, Mark Dickinson wrote:
............
>> It does look inconsistent however, and it seems to me rounding and
>> interpolation should behave similarly.
>
> Agreed.  In this case it's a minor bug that round(-9.85, 1)
> produces -9.9 instead of -9.8;  both string formatting
> and round should give -9.8.  This bug is fixed in Python
> 2.7 and in Python 3.x.
>
> Note that in 2.7 there's still a legitimate difference: round
> rounds halfway cases away from 0, while string formatting
> rounds them to even.  So the following results are correct:
>
> Python 2.7 (r27:82500, Jul 11 2010, 22:38:53)
> [GCC 4.2.1 (Apple Inc. build 5659)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
>>>> round(1.25, 1)
> 1.3
>>>> '%.1f' % 1.25
> '1.2'
>
> (1.25 *is* an exact halfway case, since it's exactly
> representable as a binary float.)
>
> In Python 3.x, round always does round-half-to-even, so
> string formatting and round should agree (and if they don't,
> it's definitely a bug: please report it!)
>
> With all this said, asking for *decimal* rounding of
> *binary* approximations to *decimal* halfway cases to give
> the results you expect is ... optimistic, to say the least.
> Use the decimal module if you care about which way
> your (almost) halfway cases get rounded.
>
> [I already replied to this earlier through Google groups, but
> I'm not sure whether it went through properly.  Apologies
> for the duplication, if so.]
>

yes thanks I saw that, but no problem about the dup. I suspect we might end up 
using some kind of fixed point.

Anyhow does anyone have a good algorithm for ensuring rounded percentages do add 
up to 100%? :) How about grouped percentages ie ensure the group sums and the 
groups display correctly in rounded form.
-- 
Robin Becker




More information about the Python-list mailing list