[Python-ideas] Python Numbers as Human Concept Decimal System

Chris Angelico rosuav at gmail.com
Fri Mar 7 01:29:34 CET 2014


On Fri, Mar 7, 2014 at 10:58 AM, Mark H. Harris <harrismh777 at gmail.com> wrote:
> But, it should not do this:
>
>>>> from pdeclib import *
>>>> x=1
>>>> y=Decimal(.1)
>>>> x+y
> Decimal('1.10000000000000000555111512312578270211816')
>>>>
>
> The policy hidden under the abstraction way down in the guts of the Class
> hierarchy should make the decision to do this:
>
>>>> ====== RESTART =====
>>>> from pdeclib import *
>>>> x=1
>>>> y=.1
>>>>
>>>> def add_(x, y):
> return d(x)+d(y)
>
>>>> add_(x, y)
> Decimal('1.1')
>>>>
>
> Please don't pick at this.  The point is not the code or the syntax. The
> point
> is that the policy in a correctly related Class hierarchy can handle
> somebody
> asking the system to add a  1.23b with a 1.23d , and if setup correctly,
> will
> just work.

If you had a literal syntax "0.1d" meaning Decimal("0.1"), this would
be solved. The only reason your code seems to work is that you're
rounding off your binary floats instead of using them with as much
accuracy as they have. You're deceiving yourself that
str(float('0.1')) seems to round-trip, and therefore that's the right
way to get a decimal value from a float. But with floats completely
out of the picture, there's no need to do any of this.

>>> x=1
>>> y=Decimal(".1") # y=.1d
>>> x+y
Decimal('1.1')

So... I'm +1 for adding a literal syntax for decimals. +1 for adding a
stating-the-default for floats (do it straight away, then code that
uses it can be backported all the way even when there's a change of
default). +0.5 for adding a syntax for fractions; support in principle
but the devil's in the details. -1 for trying to unify everything.

ChrisA


More information about the Python-ideas mailing list