floating point range generator

Bengt Richter bokr at oz.net
Mon Jul 28 13:33:16 EDT 2003


On Mon, 28 Jul 2003 02:22:59 GMT, Carl Banks <imbosol at aerojockey.com> wrote:
[...]
>
>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
>

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 
 
but shouldn't that be xrange(n_intervals) and leave out
the final point (thus exact but invisible ;-).

Regards,
Bengt Richter




More information about the Python-list mailing list