[Python-Dev] PEP 0484 - the Numeric Tower

Random832 random832 at fastmail.com
Tue Oct 13 20:41:54 EDT 2015


Steven D'Aprano <steve at pearwood.info> writes:

> On Tue, Oct 13, 2015 at 04:37:43PM -0700, Raymond Hettinger wrote:
>
>> We could have (and still could) make the choice to always coerce to 
>> decimal (every float is exactly representable in decimal).  Further, 
>> any decimal float or binary float could be losslessly coerced to a 
>> Fraction, but that probably isn't what you really want most of the 
>> time.  I think people who work in decimal usually want to stay there 
>> and people who work with binary floating point want to stay there as 
>> well (invisible coercions being more likely to cause pain than relieve 
>> pain).
>
> Further to what Raymond says, if anyone wants to persue this further, 
> and wants to argue for such automatic promotion of float to Decimal (or 
> vice versa), I think a good place to start would be a survey of other 
> languages with a numeric tower. How do they handle similar situations?

I believe that in Scheme (which AIUI is where the term "numeric tower"
comes from) has a notion of "exact" and "inexact" numbers. A "flonum"
(float) would be an inexact number. And any operation between exact and
inexact numbers (including e.g. min and max, even if the exact number
wins) gives an inexact result.

A Decimal, though, could be regarded as an exact number (a special kind
of rational) or an inexact number (another kind of floating point). I
suspect some people who use them intend them one way and others intend
the other (especially since Fraction lacks a good way to format the
value in decimal notation).

If they are both inexact, then it doesn't much matter which one they're
promoted to, since they're both implementation details of the abstract
type "inexact real number". AIUI many implementations don't *actually*
have any other implementations of "inexact real number" except flonum,
and those that do just have them as a rational fraction with an
"inexact" flag set, but the spec does allow it.

Implementing a scheme-style exact/inexact numeric tower also suggests
more ABCs.



More information about the Python-Dev mailing list