Why not FP for Money?

Carlos Ribeiro carribeiro at gmail.com
Wed Sep 22 10:32:58 EDT 2004


On Wed, 22 Sep 2004 06:55:50 -0700, Al Christians
<achrist at easystreet.com> wrote:
> 1. You have to remember to re-round after every calculation.  Instead of
> "a = a + b" you have to write "x = round_money(a + b)".  When just about
> every statement is decorated like this, you can see that your
> programming language is missing something at a low level that you really
> might want it to have. OTOH, the rounding rules are very often
> domain-specific, so having one particular set of rules built into the
> language might be more of a nuisance than a help.

When I programmed in Delphi, that's what we had to do. Early versions
of Turbo Pascal had BCD libraries, and later versions added support
for currency types. But most of the time, we would use extended
(80-bit) numbers, and round after every calculation. To forget to
round would cause problems pretty quickly -- for example, when running
a report, it's fairly easy to have the wrong sum. If I remember it
well, at some point Delphi's standard library could be customized to
choose between two or three different rounding algorithms, including
one called "banker's rounding", where exact half numbers would be
converted to the closest even number. The goal was to make sure that
the end result of several roundings would not exhibit any tendency,
however small, towards the highest value:

Normal rounding:
0.5 -> 1
1.5 -> 2
2.5 -> 3
3.5 -> 4
sum of the rounded numbers -> 10
 
Banker's rounding:
0.5 -> 0
1.5 -> 2
2.5 -> 2
3.5 -> 4
sum of the banker's rounded numbers -> 8

sum of the original numbers -> 8


-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list