[Python-ideas] Float range class

Chris Barker chris.barker at noaa.gov
Thu Jan 8 17:31:43 CET 2015

On Thu, Jan 8, 2015 at 8:01 AM, Todd <toddrjen at gmail.com> wrote:

> Currently, the range class is limited to integers.  However, in many
> applications, especially science and mathematics, ranges of float values
> are the norm.
almost -- see below.

>   So it would be nice to have a float equivalent of the existing range
> class.

> The second approach is to create a new class that handles floating-point
> ranges

I think this is the way to go, but not for the same reasons you gave.

from numpy experience: numpy provides an arange() ("array range", so names
back in the days of import *) with integer inputs is is essentially the
same as the py2 range() function -- i.e. creates a 1-d numpy array of
integers using start, stop, step.

Called with floating point arguments, it does much the same thing, but with
floats, and, of course, start, stop and step need not be integers -- but
here's the rub:

it does  this in the naive way: starting with start, continuing to add
step, until the value is >= stop, at which point it terminates without
including that last value. This is fine and dandy for integers, but is a
mess with floats, due to rounding and binary storage under the hood (i.e
floats can not precisely represent common base-10 steps). This is
particularly a problem when you ant a particular end point to be included,
you then need to increase stop to a bit more than you want, so as to make
sure it gets included, but then you can accidentally get one too many.

This is almost never what people really want, though it does work as
expected most of the time -- that most of the time is the catch -- future
bugs lurking.

So: the accepted practice in numpy (and often espoused to newbies on the
mailing list) is to not use arange() for floats. Instead, the "linspace()"
function is recommended. This function returns evenly spaced numbers over a
specified interval, guaranteeing that you get the start an end points you
want included. It has optional arguments that let you define the spacing in
various ways, but the key is that the algorithms are designed to give the
user what is expected,  regardless of floating point nuance.

Anyway, if such an object were to be added to the pyton standard libary, it
should be more like the numpy linspace(), rather than range.


linspace docstring::
Return evenly spaced numbers over a specified interval.

Returns `num` evenly spaced samples, calculated over the
interval [`start`, `stop` ].

The endpoint of the interval can optionally be excluded.

start : scalar
    The starting value of the sequence.
stop : scalar
    The end value of the sequence, unless `endpoint` is set to False.
    In that case, the sequence consists of all but the last of ``num + 1``
    evenly spaced samples, so that `stop` is excluded.  Note that the step
    size changes when `endpoint` is False.
num : int, optional
    Number of samples to generate. Default is 50.
endpoint : bool, optional
    If True, `stop` is the last sample. Otherwise, it is not included.
    Default is True.
retstep : bool, optional
    If True, return (`samples`, `step`), where `step` is the spacing
    between samples.
dtype : dtype
    The type of the output array.  If `dtype` is not given, infer the data
    type from the other input arguments.

    .. versionadded:: 1.9.0

samples : ndarray
    There are `num` equally spaced samples in the closed interval
    ``[start, stop]`` or the half-open interval ``[start, stop)``
    (depending on whether `endpoint` is True or False).
step : float (only if `retstep` is True)
    Size of spacing between samples.

> (perhaps called "frange") and/or a new class that handles duck-typed
> ranges (perhaps called "drange").  However, there isn't really a good place
> right now to put these as far as I can see.

> The third approach is to create a new module for various range classes.
> For example there could be a float range, a datetime range, a decimal
> range, a duck-typed range, an abstract base class for making new range
> classes, etc.  However, this is a lot more work (although it wouldn't need
> to all be done at one time).
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


Christopher Barker, Ph.D.

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/ca1ed572/attachment-0001.html>

More information about the Python-ideas mailing list