[Python-ideas] float vs decimal

William Dode wilk at flibuste.net
Wed Mar 11 22:32:10 CET 2009


On 11-03-2009, Chris Rebert wrote:
> On Wed, Mar 11, 2009 at 12:25 PM, William Dode <wilk at flibuste.net> wrote:
>> Hi,
>>
>> I just read the blog post of gvr :
>> http://python-history.blogspot.com/2009/03/problem-with-integer-division.html
>>
>> And i wonder why
>>>>> .33
>> 0.33000000000000002
>> is still possible in a "very-high-level langage" like python3 ?
>>
>> Why .33 could not be a Decimal directly ?
>
> I proposed something like this earlier, see:
> http://mail.python.org/pipermail/python-ideas/2008-December/002379.html
> Obviously, the proposal didn't go anywhere, the reason being that
> Decimal is currently implemented in Python and is thus much too
> inefficient to be the default (efficiency/practicality beating
> correctness/purity here apparently). There are non-Python
> implementations of the decimal standard in C, but no one could locate
> one with a Python-compatible license. The closest was the IBM
> implementation whose spec the decimal PEP was based off of, but
> unfortunately it uses the ICU License which has a classic-BSD-like
> attribution clause.

Thanks to resume the situation. 

I think of another question. Why it's so difficult to mix float and 
decimal ?

For example we cannot do Decimal(float) or float * Decimal.

And i'm afraid that with python 3 it will more often a pain because 
operation with two integers can sometimes return integer (and accept an 
operation with decimal) and sometimes not when the operation will return 
a float.

>>> a = Decimal('5.3')
>>> i = 4
>>> j = 3
>>> i*a/j
Decimal('7.066666666666666666666666667')
>>> i/j*a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'float' and 'Decimal'

I mean, why if a Decimal is in the middle of an operation, float didn't 
silently become a Decimal ?


-- 
William Dodé - http://flibuste.net
Informaticien Indépendant




More information about the Python-ideas mailing list