Generalized range
Michael Hoffman
cam.ac.uk at mh391.invalid
Thu Apr 26 15:47:37 EDT 2007
tkpmep at hotmail.com wrote:
> Thanks - you have covered a fair bit of gorund here - I will modify
> myRange taking your suggestions into account. The one suggestion that
> I'm going to have to think through is repeatedly incrementing res.
>
> I deliberately did not use this as repeated addition can cause
> rounding errors to accumulate, making the loop run a little longer or
> shorter than necessary. I thought I would be far less likely to run
> into rounding issues with a multiplicative construct - hence my use of
> epsilon, and my question about an appropriate value for it
You are right about rounding issues--with a sufficiently small step, the
way I have done it, it could become an infinite loop. But you can still
do it with multiplication, without using an epsilon constant. How about
something like this:
index = 0
while res < maximum:
yield minimum + (step * index)
index += 1
--
Michael Hoffman
More information about the Python-list
mailing list