[Python-Dev] Revised decimal type PEP

M.-A. Lemburg mal@lemburg.com
Tue, 31 Jul 2001 10:30:20 +0200

Michael McLay wrote:
> PEP: 2XX
> Title: Adding a Decimal type to Python
> Version: $Revision:$
> Author: mclay@nist.gov <mclay@nist.gov>
> Status: Draft
> Type: ??
> Created: 25-Jul-2001
> Python-Version: 2.2
> Introduction
>     This PEP describes the addition of a decimal number type to Python.
>     ...
> Implementation
>     The tokenizer will be modified to recognized number literals with
>     a 'd' suffix and a decimal() function will be added to __builtins__.

How will you be able to define the precision of decimals ? Implicit
by providing a decimal string with enough 0s to let the parser
deduce the precision ? Explicit like so: decimal(12, 5) ?

Also, what happens to the precision of the decimal object resulting
from numeric operations ?

>     A decimal number can be used to represent integers and floating point
>     numbers and decimal numbers can also be displayed using scientific
>     notation. Examples of decimal numbers include:
>     ...
>     This proposal will also add an optional  'b' suffix to the
>     representation of binary float type literals and binary int type
>     literals.

Hmm, I don't quite grasp the need for the 'b'... numbers without
any modifier will work the same way as they do now, right ?
>     ...
>     Expressions that mix binary floats with decimals introduce the
>     possibility of unexpected results because the two number types use
>     different internal representations for the same numerical value. 

I'd rather have this explicit in the sense that you define which
assumptions will be made and what issues arise (rounding, truncation,
loss of precision, etc.).

>     The
>     severity of this problem is dependent on the application domain.  For
>     applications that normally use binary numbers the error may not be
>     important and the conversion should be done silently.  For newbie
>     programmers a warning should be issued so the newbie will be able to
>     locate the source of a discrepancy between the expected results and
>     the results that were achieved.  For financial applications the mixing
>     of floating point with binary numbers should raise an exception.
>     To accommodate the three possible usage models the python interpreter
>     command line options will be used to set the level for warning and
>     error messages. The three levels are:
>     promiscuous mode,   -f or  --promiscuous
>     safe mode           -s or --save
>     pedantic mode       -p or --pedantic

How about a generic option:

	--numerics:[loose|safe|pedantic] or -n:[l|s|p]

>     The default setting will be set to the safe setting. In safe mode
>     mixing decimal and binary floats in a calculation will trigger a warning
>     message.
>     >>> type(12.3d + 12.2b)
>     Warning: the calculation mixes decimal numbers with binary floats
>     <type 'decimal'>
>     In promiscuous mode warnings will be turned off.
>     >>> type(12.3d + 12.2b)
>     <type 'decimal'>
>     In pedantic mode warning from safe mode will be turned into exceptions.
>     >>> type(12.3d + 12.2b)
>     Traceback (innermost last):
>       File "<stdin>", line 1, in ?
>     TypeError: the calculation mixes decimal numbers with binary floats
> Semantics of Decimal Numbers
>     ??

Marc-Andre Lemburg
CEO eGenix.com Software GmbH
Consulting & Company:                           http://www.egenix.com/
Python Software:                        http://www.lemburg.com/python/