<p dir="ltr">On 8 Jan 2015 21:30, "Andrew Barnert" <<a href="mailto:abarnert@yahoo.com">abarnert@yahoo.com</a>> wrote:<br>
><br>
> On Jan 8, 2015, at 10:40, Nathaniel Smith <<a href="mailto:njs@pobox.com">njs@pobox.com</a>> wrote:<br>
><br>
> > On Thu, Jan 8, 2015 at 5:58 PM, Todd <<a href="mailto:toddrjen@gmail.com">toddrjen@gmail.com</a>> wrote:<br>
> >> On Thu, Jan 8, 2015 at 6:41 PM, Andrew Barnert <<a href="mailto:abarnert@yahoo.com">abarnert@yahoo.com</a>> wrote:<br>
> >>><br>
> >>> More generally: if an frange that works exactly like range but with floats<br>
> >>> (and rounding errors) is all you want, that's so trivial to write that it<br>
> >>> doesn't seem worth putting in the stdlib, unless the use case is very<br>
> >>> common.<br>
> >><br>
> >> In numerical computing the use-case is extremely common, in fact I would say<br>
> >> it practically universal.  Ideally it would be vectorized, but in practice<br>
> >> this is often not possible, requiring the use of loops.<br>
> ><br>
> > Can you give a more specific example? I believe your problem is real,<br>
> > but as a numerical programmer I can't think of an example of where<br>
> > I've ever wanted anything like this, so it's hard for me to visualize<br>
> > what exactly you're talking about.<br>
> ><br>
> > And is what you want just:<br>
> ><br>
> > class FRange:<br>
> >    def __init__(start, stop, step):<br>
> >        self.start = start<br>
> >        self.stop = stop<br>
> >        self.step = step<br>
> ><br>
> >    def __getitem__(self, i):<br>
> >        x = self.start + i * self.step<br>
> >        if x > self.stop:<br>
> >            raise IndexError<br>
><br>
> >    def __iter__(self):<br>
> >        i = 0<br>
> >        while True:<br>
> >            try:<br>
> >                yield self[i]<br>
> >            except IndexError:<br>
> >                return<br>
> >            i += 1<br>
><br>
> This isn't really necessary; any type whose __getitem__ takes int values from 0 up to some point and then raises IndexError beyond that is automatically iterable (and the built-in sequence iterator is basically a C-optimized version of what you just wrote).<br>
><br>
> While we're at it, you probably also want __len__, __contains__ (one more place for naive float rounding problems…), __reversed__, index, count, a __getitem__ that handles slices and negative values, and __hash__. All pretty simple to add. (In fact, if you inherit collections.abc.Sequence you'll get everything but __getitem__, __len__, and __hash__ for free, but you'd probably want to implement __contains__, index, and count anyway, because the default versions will be linear instead of constant.)</p>
<p dir="ltr">Most of those I forgot about :-), but leaving out __contains__ and friends was intentional, because my feeling is that there's *really* no valid use case for those in the floating point case. Best to pretend that == just doesn't exist on FP numbers.</p>
<p dir="ltr">I'd still like an answer to the question about what some or all of this is good for, though. Certainly you *can* implement all of those things -- but why? Concretely, what's the use case?</p>
<p dir="ltr">-n</p>