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

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Aug 28 12:53:06 CEST 2013


On 28 August 2013 11:48, Draic Kin <drekin at gmail.com> wrote:
> On Wed, Aug 28, 2013 at 12:30 PM, Vernon D. Cole <vernondcole at gmail.com>
> wrote:
>>
>> Darn right it should return False.  Given the principle of least surprise
>> (and my prejudices built up over 40 years as a computer programmer) I would
>> expect that decimal.Decimal data would be stored internally as some form of
>> decimal data, and would store into a database as such.  It would be expected
>> to be in a fixed point format.  Real, on the other hand, I would expect to
>> be stored as an IEEE double precision floating point number, or something
>> like that.
>>   I don't care whether a fixed point decimal number might be defined by a
>> mathematician as "real" -- I care whether it can be processed by an FPU, and
>> whether it will loose precision in large financial calculations.

Decimals are effectively stored internally as integers. They won't be
processed by the FPU which operates on binary floating point types.
Also the decimal module has traps for people who want to prevent
inexact etc. operations.

> For the same reason, I could think that isinstance(Decimal, Rational) ->
> True and issubclass(Rational, Real) -> False. It's more about exact vs.
> non-exact computations which is orthogonal to number hierarchy. Maybe there
> should be some ExactNumber abstract base class and some convention that
> exact shouldn't coerce with non-exact. So Decimal + float should raise an
> exception even if both would be subclasses of Real (and Decimal even of
> Rational). Or maybe it would be enough if there were just non-exact variants
> of Real and Complex since non-exactness if just issue of them.

I agree that an Exact ABC would be good. The PEP has a reference to
exact as a superclass of Rational but I guess it's just left over from
a previous edit:
http://www.python.org/dev/peps/pep-3141/#numeric-classes


Oscar


More information about the Python-ideas mailing list