prePEP: Decimal data type

Aahz aahz at pythoncraft.com
Tue Nov 4 15:07:24 EST 2003


In article <mailman.295.1067625441.702.python-list at python.org>,
Batista, Facundo <FBatista at uniFON.com.ar> wrote:
>
>    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()``)

otherType is converted to Decimal unless precision in the current
Context would be exceeded; in that case you raise ValueError.

>    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()``)

Raise an exception.  Because of the precision issues in floating point,
conversion to Decimal must always be explicit.

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

Exception is raised, just as with all other uses of strings and numbers.

>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')``

That's tough.  I'm inclined toward requiring an explicit conversion
through a function call that isn't the Decimal constructor, but ease of
use is also a factor.

>2. The value could be of the type:
>
>       - another Decimal
>       - int or long
>       - float
>       - string 

Also a tuple of int/longs.

>4. The Context must be omnipresent, meaning that changes to it affects all
>   the current and future Decimal instances.

Here's the tricky part (and where I abandoned work): the Context must be
thread-local.  Also, Context applies only to operations, not to Decimal
instances; changing the Context does not affect existing instances if
there are no operations on them.

>15. To be immutable.

This is why the Context can't affect existing instances.  ;-)
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan




More information about the Python-list mailing list