Why not FP for Money?

Robert Brewer fumanchu at amor.org
Sat Sep 25 03:52:38 CEST 2004

Jeff Shannon wrote:
> Carlos Ribeiro wrote:
> >A last note: unless dictated by context otherwise, I think 
> that adding
> >two fixed point numbers should round/truncate to the smallest number
> >of decimal places, as in:
> >
> >12.34d + 10.001d --> 32.34
> >  
> >
> Personally,  I think that it makes the most sense (i.e., will 
> result in 
> the fewest surprises) if the greatest possible number of places is 
> preserved.  If you truncate decimal places, then you might get this:
>     x = 1.1d
>     for _ in range(5):
>         x += 0.02d
>     print x              ----> 1.1d
> Each addition of 0.02 to 1.1 yields 1.12, which is truncated back to 
> 1.1, which leaves us in the same place that integer division did.  I 
> think it's pretty clear that this *should* produce 1.2.  In 
> order to do 
> that, you need to preserve the largest number of decimal places.

That would certainly keep Decimal objects from becoming the default
number type in Python 3.0. But if a suffixed "d" is retained, it becomes
Yet Another Thing To Learn, not unlike the current surprises with

The "end-user" would have to be more explicit in order to achieve the
desired behavior:
    x = 1.10d	# Notice the trailing 0
    for _ in range(5):
        x += 0.02d
    print x              ----> 1.20d

In the case of using Decimals as a Money type, this wouldn't be much of
a burden on any society with a decimalized currency (IMO); it's standard
practice to write the trailing zero.

Robert Brewer
Amor Ministries
fumanchu at amor.org

More information about the Python-list mailing list