Why not FP for Money?

Carlos Ribeiro carribeiro at gmail.com
Thu Sep 23 13:41:38 CEST 2004

On 22 Sep 2004 22:03:43 -0700, Chris Barker <barkmann at gmail.com> wrote:
> ... Personally, thinking of money it
> terms of significant digits would be fine with me. Is there really any
> point in keeping track of the pennies when talkin gof trillions of
> dollars?

In Brazil, there is tax for money transfers (all transfers to, from
and between accounts). It's 0.38% of each operation. Some operations
are really smal, just a few reals, and the tax is measured in cents.
For big operations -- those in the range of millions -- the tax gets 
bigger, but still manageable. But when you sum up all the transfers,
the sum has to be exact to the cent. Imagine if you sum all transfers
done in one year, for accounting purposes. The number is really big --
in the order of billions. But the sum has to be precise to the cent.
(Ok, it's not trillions -- I'm only showing you and example of a
situation that stress the limits of numeric precision)

> """
> Beware! Floating point errors may bite you *even for non-fractional
> numbers*
> """
> I tried to address that in the OP. 64 bit IEEE FP carries a lot of
> digits! This isn't an issue till you get to quadrillions of dollars.
> The same issue comes up if you use standard integers, though then you
> will get overflow instead. If not handled right, that could REALLY
> give strange results! Fortunatly Python now automatically rolls over
> to long integers.

... and ...

> One interesting distiction here: binary vs. decimal and floating point
> vs. fixed point are orthoganal concepts that have been mingled here.
> Alex pointed out that decimal floating point has the same problems as
> binary as far as accuracy is concerned.

You're right. When I made my comment about floating point errors for
non-fractional numbers, I didn't made this distinction. Normally, when
I think about a money type, I don't think about floating point
decimal, I think about fixed point.

Now that we are talking about money types,  I had an idea some time
ago and I would appreciate to hear some opinions. What do you think
about using a different notation for fixed point decimal literals in
Python, using the $ sign, as in:

a) $1.0000 --> fixed point decimal, four digits of precision
b) 1$0000 -> alternative syntax, also with four digits

and operations would also be easier to write, with type coercion, etc:

$1.00 + 0.99212 --> $1.99 (rounded)

That would make easy to supply fixed point literals. And I dont think
that the use of a symbol, in this case, is a case of Perlish
contamination -- even in countries where the money sign is different,
I think that most programmers would readily recognize the $ sign for
it. How about it?

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