[Python-ideas] isinstance(Decimal(), Real) -> False?

Draic Kin drekin at gmail.com
Wed Aug 28 13:14:26 CEST 2013


On Wed, Aug 28, 2013 at 12:49 PM, Oscar Benjamin <oscar.j.benjamin at gmail.com
> wrote:

> On 28 August 2013 11:22, Paul Moore <p.f.moore at gmail.com> wrote:
> > On 28 August 2013 11:15, Oscar Benjamin <oscar.j.benjamin at gmail.com>
> wrote:
> >>
> >> There's also no way to arithmetic with int/Fraction real and imaginary
> >> components (e.g. gaussian integers etc.) but these are still instances
> >> of Complex.
> >
> > The difference is that there is no implicit conversion from Decimal to
> > float:
> >
> >>>> from decimal import Decimal as D
> >>>> D('1.5') + 2.3
> > Traceback (most recent call last):
> >   File "<stdin>", line 1, in <module>
> > TypeError: unsupported operand type(s) for +: 'Decimal' and 'float'
> >>>> from fractions import Fraction as F
> >>>> F(1,2) + 2.3
> > 2.8
> >
> > IIRC, that's the reason Decimal is treated specially here - maybe it's
> not a
> > sufficiently good reason (I don't have an opinion on that) but from what
> > little I recall of the discussions around the time, it's the key
> > distinguishing feature of Decimal in these matters...
>
> Why shouldn't there be implicit conversion in Decimal arithmetic?
> There already is for all the other numeric types. Also explicit
> conversion seems to blocked in some cases. This one in particular
> bothers me (since it's often desirable to get a decimal representation
> of a fraction):
>
> $ python3.3
> Python 3.3.0 (default, Sep 29 2012, 17:14:58)
> [GCC 4.7.2] on linux
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from decimal import Decimal as D
> >>> from fractions import Fraction as F
> >>> D(F(1, 2))
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> TypeError: conversion from Fraction to Decimal is not supported
>
> I would think that it's because you can express a fraction as finite
decimal expansion iff the prime decomposition of denominator contains only
2s and 5s, since conceptualy decimal is just a fraction with power of 10 in
denominator. So Decimal is less expressible than Fraction.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130828/c2d59178/attachment.html>


More information about the Python-ideas mailing list