[Python-Dev] Revised decimal type PEP

Michael McLay mclay@nist.gov
Wed, 1 Aug 2001 04:26:58 -0400

On Wednesday 01 August 2001 10:55 am, Guido van Rossum wrote:
> I believe it is typically global per thread, but that's up to the
> langage binding.  A Java binding for Cowlishaw's decimals apparently
> requires passing in a context as a third argument on each operation.

I'm trying to understand how a decimal number context would work.  Is the=
context a variable and/or flag that defines the rounding rules and precis=
of a number when it is used in a calculation?  How is it associated with =
number or a calculation?  The "global per thread" description seems to=20
associate the context with threads.  Can the context be altered inside th=
thread?  Is it possible to change the context at different levels in a=20

I would assume there is a default context will be used until the context =
changed.  If this is the case I would expect a default context would be=20
defined at startup.

Would it make sense to have a simple decimal type with no features that c=
be modified (a fixed context)? This simple type could be extended by deri=
a new numerical type from the base decimal type.  This base decimal type=20
would be targeted at the newbie user.  It would have no surprises.  It wo=
have a default precision of 18 and the rules for rounding would emulate t=
typical hand held calculator. Accountants who need special rounding rules=
would use a derived type that allowed the default rules to be overridden.

It would be possible to round numbers of the simple based type, but it wo=
be an explicit step to remove insignificant digits.  An accounting decima=
type might automatically round calculations to the smallest denomination.=
For instance, an accounting context might have automatically managed the=20
final rounding in the following calculation:

p>>> quantity =3D 6
>>> tax =3D .06
>>> price =3D 2.99
>>> total =3D price * quantity * (1 + tax)
>>> total
>>> round(total,2)

> > M.-A. Lemburg"  suggested looking at the SQL specification for Decima=
> > datatypes.  A decimal type is also defined as a type in XML Schema.=20
> > Since this is an XML datatype there isn't a definition for how these
> > numbers are created.
> Do these say anything about semantics under numeric operations?  That
> would seem to be outside the realm of XML and possibly even outside
> SQL.  So I'm not sure how these help.

You are correct that it doesn't deal with numeric operations.  It does de=
a minimum precision requirement.  I am only referencing it here because i=
t is=20
another instance where having a decimal type in Python would be useful an=
because they have set a minimum requirement.  Setting this minmum as a=20
default behavior would probably make newbies comfortable with the languag=
e. =20

> >   NOTE: All =B7minimally conforming=B7 processors =B7must=B7 support =
> > numbers with a minimum of 18 decimal digits (i.e., with a =B7totalDig=
> > of 18). However, =B7minimally conforming=B7 processors =B7may=B7 set =
> > application-defined limit on the maximum number of decimal digits the=
> > are prepared to support, in which case that application-defined maxim=
> > number =B7must=B7 be clearly documented.
> > 	- http://www.w3.org/TR/xmlschema-2/#decimal