[Python-Dev] index (was str with base)

Adam Olsen rhamph at gmail.com
Tue Jan 17 17:48:08 CET 2006


On 1/17/06, Guido van Rossum <guido at python.org> wrote:
> On 1/17/06, Jim Jewett <jimjjewett at gmail.com> wrote:
> > Guido wrote:
> >
> > > more important to implement __index__() in Python 2.5.
> > > This behaves like __int__() for integral types, but is not
> > > defined for float or Decimal.
> >
> > Why not for Decimal, or even float?  I would not be surprised
> > if 10.798 failed, but I would expect 1000D to work.
> >
> > If indexing worked with more arbitrary extension integers,
> > then I would expect it work with Decimal, and possibly float,
> > when the number == a whole number, and to raise ValueError
> > otherwise.
>
> Sorry, I forgot to explain this. There's a very good reason why this
> should not be allowed.
>
> You don't know if the 1000D was the result of an exact or of an
> approximate calculation. It could be a pure coincidence that it's
> 1000D instead of 999.9D; and we certainly don't want the latter to be
> allowed for indexing. Floating point (including Decimal floating
> point) calculations are fraught with uncertainties about the precision
> / accuracy of the outcome. Even if when you test your algorithm you
> always get exact outcomes, there's no guarantee that with real input
> data the same will hold.
>
> Requiring the index to be an int by *type* solves this by forcing you
> to think about rounding/truncating. I talked to some Numeric folks and
> they understand and agree.

IMO this is better done by replacing float with an interval type, or
atleast float context objects with explicit rounding and precision
control.  The former would tell you when the result is inexact (the
gap or "length" is non-zero), the latter would let you pick rounding
behavior when you know enough math to back it up.

And rather than leave you all hanging I'll say that I believe it could
be done by using LLVM (circumventing C entierly.)
http://rhamphoryncus.blogspot.com/2006/01/floating-point-and-nans-in-python.html

(Why have float at all if the documentation essentially says it
channels /dev/urandom?)

--
Adam Olsen, aka Rhamphoryncus


More information about the Python-Dev mailing list