prePEP: Decimal data type

Batista, Facundo FBatista at uniFON.com.ar
Tue Nov 4 16:04:21 EST 2003


Aahz 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.

Like it!


#- >    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.

Seems to be the best solution, according to that we can't found a "common
sense" behaviour.


#- >    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.

We all agree here.



#- >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.

We can have a Decimal.fromFloat(...), but with behaviour j or k?



#- >2. The value could be of the type:
#- >
#- >       - another Decimal
#- >       - int or long
#- >       - float
#- >       - string 
#- 
#- Also a tuple of int/longs.

I didn't include that in the prePEP because it seems that the actual
implementation support this just to dec == Decimal(repr(dec)), for every
Decimal as dec.

Has any other advantage?


#- >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.

OK. I'll change the item 4.


#- 
#- >15. To be immutable.
#- 
#- This is why the Context can't affect existing instances.  ;-)

You're right. Again. :)

.	Facundo





More information about the Python-list mailing list