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

Steven D'Aprano steve at pearwood.info
Thu Aug 29 04:02:38 CEST 2013


On 28/08/13 20:48, Draic Kin wrote:
> For the same reason, I could think that isinstance(Decimal, Rational) ->
> True

If Decimal were a subclass of Rational, so should float. The only fundamental difference between the two is that one uses base 10 floating point numbers and the other uses base 2.


>and issubclass(Rational, Real) -> False. It's more about exact vs.
> non-exact computations which is orthogonal to number hierarchy.

The numeric tower is precisely about the numeric hierarchy of Number > Complex > Real > Rational > Integral, and since they are all *abstract* base classes, exact and inexact doesn't come into it. Concrete classes can be inexact or exact, or one could implement separate Exact and Inexact towers.

In practice, it's hard to think of a concrete way to implement exact real numbers. Maybe a symbolic maths application like Mathematica comes close?


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


Personally, I would implement inexact/exact as an attribute on the type:

if type(x).exact: ...


sort of thing.



-- 
Steven


More information about the Python-ideas mailing list