H
Harald Massa
which one should I use? Recommendations?
Harald
Harald
Paul said:I needed this for something I was doing just now. It came out pretty
enough that I thought I'd post it. It's like xrange, except for floating
point values.
def frange(start, stop, step=1.0):
sign = cmp(0, step)
while cmp(start, stop) == sign:
yield start
start += step
Carl said:3. The last iteration might not happen because of floating point
uncertainties, even if rounding errors are minimized
Carl Banks said:...
A more robust version would look something like this (but it still has
problem #3):
def frange(start, stop, step=1.0):
sign = cmp(0,step)
for i in xrange(sys.maxint):
v = start+i*step
if cmp(v,stop) != sign:
break
yield v
The most robust way to handle this is to iterpolate, i.e., instead of
passing start, stop, and step, pass start, stop, and n_intervals:
def interiter(start, stop, n_intervals):
diff = stop - start
for i in xrange(n_intervals+1):
yield start + (i*diff)/n_intervals
An IEEE 754 (whatever) geek can probably point out even more accurate
ways to do these.
The most robust way to handle this is to iterpolate, i.e., instead of
passing start, stop, and step, pass start, stop, and n_intervals:
def interiter(start, stop, n_intervals):
diff = stop - start
for i in xrange(n_intervals+1):
yield start + (i*diff)/n_intervals
Bengt said:To guarantee the exact end points, maybe:
def interiter(start, stop, n_intervals):
fn=float(n_intervals)
for i in xrange(n_intervals+1):
yield ((n_intervals-i)/fn)*start + (i/fn)*stop
Good.
but shouldn't that be xrange(n_intervals) and leave out
the final point (thus exact but invisible ;-).
Carl Banks said: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.
I think you're right about this, but it goes against the Python notion
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.
... fn=float(n_intervals)>>> def interiter(start, stop, n_intervals, ends='[]'):
...>>> for f in interiter(0, 8, 8, ends='[)'): print f,
...>>> for f in interiter(0, 8, 8, ends='(]'): print f,
...>>> for f in interiter(0, 8, 8, ends='[]'): print f,
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.