prePEP: Decimal data type

Emile van Sebille emile at fenx.com
Fri Oct 31 17:04:41 EST 2003


"Batista, Facundo" <FBatista at uniFON.com.ar> wrote in message
news:mailman.295.1067625441.702.python-list at python.org...
> Here I send it.
>
> Suggestions and all kinds of recomendations are more than welcomed.
>

[snip]

> Items In Discussion
> -------------------
>
> When in a case like ``Decimal op otherType`` (see point 12 in
Requirements_
> for details), what should happen?
>
>     if otherType is an int or long:
>
>         a. an exception is raised
>         b. otherType is converted to Decimal
>         c. Decimal is converted to int or long (with ``int()`` or
> ``long()``)
>

Wouldn't you want the result to return a Decimal?  eg:

price = Decimal(3.15)
qty = 3
extension = qty * price

extension should now be a Decimal instance.

In that case, it looks like (b.) wins by default, as (a.) is clearly not
what you'd want; and (c.) yields potential results that give rise to needing
Decimal in the first place.  I'd expect anything functionally less than the
above pseudo-snippet to be cumbersome to use.


>     if otherType is a float:
>
>         d. an exception is raised
>         e. otherType is converted to Decimal (rounding? see next item in
>            discussion)
>         f. Decimal is converted to float (with ``float()``)
>

retail = Decimal(3.15)
discount = 35
wholesale = retail * (100-discount)/100

Here again, if the choices are limited to one of d|e|f then e seems the
best.

>     if otherType is a string:
>
>         g. an exception is raised
>         h. otherType is converted to Decimal
>         i. Decimal is converted to string (bizarre, huh?)
>

(g.) - I can't think of a use case where you'd want a known Decimal to work
with a string.


>
> When passing floating point to the constructor, what should happen?
>
>     j. ``Decimal(1.1) == Decimal('1.1')``
>     k. ``Decimal(1.1) ==
> Decimal('110000000000000008881784197001252...e-51')``
>

Well, persistence will bring in a whole set of problems.  (j.) is, of
course, what I'd want.

[snip]

> 3. To exist a Context.  The context represents the user-selectable
> parameters
>    and rules which govern the results of arithmetic operations.  In the
>    context the user defines:
>
>        - what will happen with the exceptional conditions.
>        - what precision will be used
>        - what rounding method will be used
>
> 4. The Context must be omnipresent, meaning that changes to it affects all
>    the current and future Decimal instances.

Does this imply then that there is unlimited precision being maintained
under the covers?


Emile van Sebille
emile at fenx.com







More information about the Python-list mailing list