[issue43602] Include Decimal's in numbers.Real

Oscar Benjamin report at bugs.python.org
Fri Apr 16 07:08:05 EDT 2021


Oscar Benjamin <oscar.j.benjamin at gmail.com> added the comment:

I've never found numbers.Real/Complex to be useful. The purpose of the ABCs should be that they enable you to write code that works for instances of any subclass but in practice writing good floating point code requires knowing something e.g. the base, precision, max exponent etc of the type. Also many implementations like Decimal have contexts and rounding control etc that need to be used and the ABC gives no way to know that or to do anything with it.

The main thing that is useful about the Rational/Integer ABCs is that they define the numerator and denominator attributes which makes different implementations interoperable by providing exact conversion. If Real was presumed to represent some kind of floating point type then an analogous property/method would be something that can deconstruct the object in an exact way like:

mantissa, base, exponent = deconstruct(real)

You would also need a way to handle nan, inf etc. Note that as_integer_ratio() is not suitable because it could generate enormous integers unnecessarily e.g. Decimal('1E+100000000').as_integer_ratio().

Instead the Real ABC only defines conversion to float. That's useful in the sense that you can write code for float and pass in some other floating point type and have everything reduce to float. You don't need an ABC for that though because __float__ does everything. In practice most alternate "real" number implementations exist precisely to be better than float in some way by either having greater range/precision or a different base but anything written for the Real ABC is essentially reduced to float as a lowest common (inexact) denominator.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue43602>
_______________________________________


More information about the Python-bugs-list mailing list