[Python-ideas] Float range class

M.-A. Lemburg mal at egenix.com
Thu Jan 8 17:18:32 CET 2015


On 08.01.2015 17:01, Todd wrote:
> Currently, the range class is limited to integers.  However, in many
> applications, especially science and mathematics, ranges of float values
> are the norm.
> 
> The current approaches for float ranges all have significant limitations.
> numpy.arange pre-allocates the values and doesn't use python floats, both
> of which affect performance.  List comprehensions also have to be
> pre-allocated.  Generators and values created in the body of a loop cannot
> be indexed or re-used and are much more verbose and harder to read.  So it
> would be nice to have a float equivalent of the existing range class.
> 
> Assuming such a class is worthwhile, I see three possible ways to handle
> float ranges, each with its own strengths and weaknesses.  I am not sure
> which, if any, is preferable.
> 
> The first is to extend the existing range class so it can support floats or
> to use duck-typing to support any class that has the necessary methods.
> However, I would be worried about making such a fundamental change to such
> a core class, and either approach could hurt performance.
> 
> The second approach is to create a new class that handles floating-point
> ranges (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).

I think you'd first have to describe some use cases, since it's
not at all clear what you mean with "float range", e.g. you
could be referring to:

* interval arithmetics: http://en.wikipedia.org/wiki/Interval_arithmetic

* ranges of numbers with a fixed stepping (which can create problems
  near the edges due to rounding issues)

* ranges in the sense of mathematical sets, where you're
  mostly interested in using set operations

etc.

Looping over such ranges doesn't sound like a useful operation,
except for the case where you have a fixed stepping (and those
can easily be implemented by mapping an integer to a stepping
in your interval).

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Jan 08 2015)
>>> Python Projects, Coaching and Consulting ...  http://www.egenix.com/
>>> mxODBC Plone/Zope Database Adapter ...       http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/


More information about the Python-ideas mailing list