[Python-Dev] PEP 327: Decimal Data Type

Michael Chermside mcherm at mcherm.com
Fri Jan 30 10:06:21 EST 2004

Facundo Batista writes:
> I'm proud to announce that the PEP for Decimal Data Type is now published 
>         http://www.python.org/peps/pep-0327.html 

VERY nice work here.

Here's my 2 cents:

    (1) You propose conversion from floats via:
      Decimal(1.1, 2) == Decimal('1.1')
      Decimal(1.1, 16) == Decimal('1.1000000000000001')
      Decimal(1.1) == Decimal('110000000000000008881784197001252...e-51')

        I think that we'd do even better to ommit the second use. People who 
really want to convert floats exactly can easily write "Decimal(1.1, 60)". But 
hardly anyone wants to convert floats exactly, while lots of newbies would 
forget to include the second parameter. I'd say just make Decimal(someFloat) 
raise a TypeError with a helpful message about how you need that second 
parameter when using floats.

    (2) For adding a Decimal and a float, you write:
I propose to allow the interaction with float, making an exact conversion and 
raising ValueError if exceeds the precision in the current context (this is 
maybe too tricky, because for example with a precision of 9, Decimal(35) + 1.2 
is OK but Decimal(35) + 1.1 raises an error).

        I suppose that would be all right, but I think I'm with Aahz on this 
one... require explicit conversion. It prevents newbie errors, and non-newbies 
can provide the functionality extremely easily. Also, we can always change our 
minds to allow addition with floats if we initially release with that raising 
an exception. But if we ever release a version of Python where Decimal and 
float can be added, we'll be stuck supporting it forever.

Really, that's all I came up with. This is great, and I'm looking forward to 
using it. I would, though, be interested in a couple more syntax-related 
    (a) What's the syntax for changing the context? I'd think we'd want 
a "pushDecimalContext()" and "popDecimalContext()" sort of approach, since most 
well-behaved routines will want to restore their caller's context.
    (b) How about querying to determine a thread's current context? I don't 
have any use cases, but it would seem peculiar not to provide it.
    (c) Given a Decimal object, is there a straightforward way to determine its 
coefficient and exponent? Methods named .precision() and .exponent() might do 
the trick.

-- Michael Chermside

More information about the Python-Dev mailing list