Robert Smallshire added the comment:

Java makes no claim to have a numeric tower.  Amongst the dynamic languages I 
surveyed Matlab (isinteger), Javascript ES6 (isInteger), PHP (is_integer), R 
(is.integer), TCL (is entier), and as we have seen Scheme (integer?) all have 
methods for testing for integer values. Python has a numeric tower modelled on 
Scheme. In the Scheme documentation we find this:

"...the integer 5 may have several representations. Scheme's numerical 
operations treat number objects as abstract data, as independent of their 
representation as possible. Although an implementation of Scheme may use many 
different representations for numbers, this should not be apparent to a casual 
programmer writing simple programs."

This is what I'm advocating.

There isn't a single mathematical (as opposed to representational) method on 
int that isn't 'inherited' from the numeric tower.  There are exactly two 
methods on float which aren't inherited from the tower: is_integer and 
as_integer_ratio.  So I think it's would be a stretch to claim that "Most of 
the [numerical] ABCs have only a subset of the [numerical] methods in the 
[numerical] concrete types."

Rather than looking at the numeric tower as a construct which forces 
proliferation of methods, it would be better to look on it as a device to 
prevent bloat. I risk straying off topic here, but I want to give an example of 
why the numeric tower is important:

Were float to inherit from Rational, rather than Real (all finite floats are 
rationals with a power-of-two denominator, all Decimals are rationals with a 
power-of-ten denominator, so this is reasonable) then the as_integer_ratio 
method which was added to float and latterly Decimal 
(http://bugs.python.org/issue25928), arguably cluttering their interfaces, may 
have been deemed unnecessary. The numerator and denominator attributes present 
in Rational could have been used instead.  I think this is an example of lack 
of adherence to the numeric tower (at least in spirit in the case of Decimal) 
resulting in interface clutter or bloat.

The consequent control-flow complexity required handle numeric objects as 
'abstract data' is surprising: statistics._exact_ratio is a good example of 
this. I count five API tests just to be able to treat numbers as, well, just 
numbers.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26680>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to