[Python-ideas] Float range class

Chris Barker chris.barker at noaa.gov
Thu Jan 8 23:35:46 CET 2015


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. 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.


> 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.

Another of my points is that while ranges of floating point numbers are
very commonly used, the use cases are generally quite different than for
ranges of integers, so we may not want the same API, floating point issues
aside. The most common cases for range are:

a) Give me n numbers

b) Loop n times

c) Give me all the indexes for a sequence of length n

d) Give me all the numbers from a to b
  - then you need to think about whether you are including b or not, and
add 1 if you want it.

e) Give me every j-th number from a to b.
  - then you need to think even more about the end point (if you care)

Note that you need to think about the last value in (d) and (e), but that's
pretty easy to do, and not prone to error with integers.

But for floating point range-like things:

a -- c are not relevant.

d) is more likely to be spelled as: Give me n numbers evenly spaced from a
to b -- that's np.linspace()

e) is essentially: give me all the numbers form a to be, using this delta.
  -  this seems like a floating point range, but again, you need to think
carefully about the end points, so you're really better off with llnspace,
and then once the end points have been defined, you can write FP code that
will do the expected thing.

-Chris

-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150108/390611e7/attachment-0001.html>


More information about the Python-ideas mailing list