floating point range generator

Bengt Richter bokr at oz.net
Mon Jul 28 21:36:21 CEST 2003

```On 28 Jul 2003 11:23:03 -0700, Paul Rubin <http://phr.cx@NOSPAM.invalid> wrote:

>Carl Banks <imbosol at aerojockey.com> writes:
>> In the interests of practicality beats purity, when you're
>> interpolating (instead of stepping as in range) you should include the
>> final endpoint.  In my experience, when when you divide a segment into
>> intervals, you almost always want to include both endpoints.
>
>of a "range" function, so an frange that includes both endpoints
>should be called something different, maybe "fstep" or something like
>that.  For what I was doing, the frange that I posted did the job.
>
>I see now that that the problem is subtle enough that IMO, a fully
>worked out solution should be written and included in the Python
>library or at least the Python cookbook.

How about if we indicate the kind of interval, e.g.,

>>> def interiter(start, stop, n_intervals, ends='[]'):
...     fn=float(n_intervals)
...     for i in xrange(ends[0]=='(', n_intervals+(ends[1]==']')):
...         yield ((n_intervals-i)/fn)*start + (i/fn)*stop
...
>>>

>>> for f in interiter(0, 8, 8): print f,
...
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0
>>> for f in interiter(0, 8, 8, ends='[)'): print f,
...
1.0 2.0 3.0 4.0 5.0 6.0 7.0
>>> for f in interiter(0, 8, 8, ends='(]'): print f,
...
1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0
>>> for f in interiter(0, 8, 8, ends='()'): print f,
...
1.0 2.0 3.0 4.0 5.0 6.0 7.0
>>> for f in interiter(0, 8, 8, ends='[]'): print f,
...
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0

Regards,
Bengt Richter

```