# [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=
=20
context a variable and/or flag that defines the rounding rules and precis=
ion=20
of a number when it is used in a calculation?  How is it associated with =
a=20
number or a calculation?  The "global per thread" description seems to=20
associate the context with threads.  Can the context be altered inside th=
e=20
thread?  Is it possible to change the context at different levels in a=20
stackframe?=20

I would assume there is a default context will be used until the context =
is=20
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=
an=20
be modified (a fixed context)? This simple type could be extended by deri=
ving=20
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=
uld=20
have a default precision of 18 and the rules for rounding would emulate t=
he=20
typical hand held calculator. Accountants who need special rounding rules=
=20
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=
uld=20
be an explicit step to remove insignificant digits.  An accounting decima=
l=20
type might automatically round calculations to the smallest denomination.=
=20
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
19.0164
>>> round(total,2)
19.02
>>>

> > M.-A. Lemburg"  suggested looking at the SQL specification for Decima=
l
> > 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=
fine=20
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=
d=20
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 =
decimal
> > numbers with a minimum of 18 decimal digits (i.e., with a =B7totalDig=
its=B7
> > of 18). However, =B7minimally conforming=B7 processors =B7may=B7 set =
an
> > application-defined limit on the maximum number of decimal digits the=
y
> > are prepared to support, in which case that application-defined maxim=
um
> > number =B7must=B7 be clearly documented.
> > 	- http://www.w3.org/TR/xmlschema-2/#decimal

```