floating point range generator

Paul Rubin http
Mon Jul 28 02:40:33 CEST 2003


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


>>> for i in frange(3., 4., 0.2): print i
... 
3.0
3.2
3.4
3.6
3.8
>>> 




More information about the Python-list mailing list