[Python-ideas] Float range class

Andrew Barnert abarnert at yahoo.com
Fri Jan 9 05:46:30 CET 2015

On Jan 8, 2015, at 14:35, Chris Barker <chris.barker at noaa.gov> wrote:

> On Thu, Jan 8, 2015 at 1:33 PM, Andrew Barnert <abarnert at yahoo.com> wrote:
>> Implementing it badly isn't much of a problem; even if you don't precompute the stop, you're just adding the cost of a multiplication to every __getitem__, and I doubt that's going to be a bottleneck in most applications. 
> I wasn't talking about performance, I was talking about the vagaries of floating point.

I think you may be disagreeing with the wrong person here (Todd is the one who proposed this feature; I was pointing out problems with it), but just in case you actually meant that a properly-implemented frange actually _could_ solve problems with floating-point vagaries:

> for example,  an obvious (easy, anyway) thing to do is somethign like:
> val = start
> while val < stop:
>     val += step
> but that's not a great idea with fp.

But this is just as bad an idea--and it's presumably exactly equivalent to what frange would do:

    for i in count():
        val = start + i*step
        if val >= stop: break

For example, try it with start=0, step=0.3, stop=0.9, and you'll get 0, 0.3. 0.6, and 0.8999999999999999--exactly the same (probably-)incorrect result you get from repeated addition.

In other words, as I said, the problem isn't that it's difficult to implement correctly; the problem is that it's difficult to _use_ correctly. Which, to me, seems like an argument against it being in the stdlib, not for it.

>> It's _using_ it badly that's the more serious problem. Just as with np.arange, a naive use of frange will lead to rounding problems, and I can't see how a builtin could provide any API that avoids that problem…
> which is why I'm suggesting that a built in be more like numpy's linspace than like range. See Warren's note for a better explanation with examples.

Sure; I said effectively the same thing. That's why I mentioned that arange has the same problem. A float linspace view might be useful; a float range view is an attractive nuisance. So I think we agree?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150108/c20148cc/attachment.html>

More information about the Python-ideas mailing list