Against PEP 240

Roman Suzi rnd at onego.ru
Thu May 31 03:09:15 EDT 2001


On Thu, 31 May 2001, Tim Peters wrote:

> [Roman Suzi]
> > Rounding and formating is a MUST in anything nrealy financial ;-)
> > So, lets not put additional burden on the Python and
> > just put "%11.2f" % round(var, 2) were due.
> 
> I doubt that's going to make Clark feel better, as I can't imagine a context
> in which that code snippet would make good sense.  That's the kind of thing
> he's doubtless suffering from:  well-intentioned attempts to worm around
> problems too dimly understood.  "%" alone would introduce one rounding
> error, and that's the best that can be done.  "round()" introduces at least
> two rounding errors of its own (although you can't know that without
> studying the implementation of round()), so all by itself is worse than just
> printing the thing.  Even if Clark sticks to plain "%" and has an exactly
> representable (in binary fp) value, he's still in a x-platform bind; e.g.,
> 
> >>> print "%11.2f" % 100.125
> 
> displays
> 
>      100.12
> 
> on most modern Unix boxes (following 754's nearest/even rounding), but
> displays
> 
>      100.13
> 
> on others and on Windows (following add-a-half-and-chop rounding).  The
> financial world has strict rules about how rounding is supposed to be done
> too.

Wow! Could you please explain what errors round() adds and in the 
following if a solution:

def finance_round(x, z=2):
  """Portable (?) add-a-half-and-chop rounding"""
  p10 = 10.0**z
  return int(x*p10+0.5)/p10

IMHO, UNIX approach is scientifically correct (adds
less noise if large data arrays are rounded before summation), while 
Windows one is more "financially" minded...

I wish Python docs were more specific on the rules 
of formatting "%", round(), etc.

> spend-6-months-writing-accounting-apps<wink>-ly y'rs  - tim

It's also interesting to know if "official" rounding rules differ from
country to country, because I was told there is no bookkeeping regulation
(in Russia) on how to round, so bookkeepers are on their own.

Sincerely yours, Roman A.Suzi
-- 
 - Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
 





More information about the Python-list mailing list