Sadly missing in the Python standard library, this function allows to use ranges, just as the built-in function range(), but with float arguments.
All thoretic restrictions apply, but in practice this is more useful than in theory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | def frange(start, end=None, inc=None):
"A range function, that does accept float increments..."
if end == None:
end = start + 0.0
start = 0.0
if inc == None:
inc = 1.0
L = []
while 1:
next = start + len(L) * inc
if inc > 0 and next >= end:
break
elif inc < 0 and next <= end:
break
L.append(next)
return L
|
Despite all rhetoric considerations about rounding effects, this was useful to my several times, so I guess it might be interesting for others as well.
Tags: shortcuts
This is faster. You can get a substantial speed boost by pre-allocating the list instead of calling append over and over. This also allows you to get rid of the conditionals in the inner loop. For 1 element, this version is barely faster, and above about 10 elements it's consistently about 5 times faster. I get identical output for every test case I can think of.
A little simplification. count = int(math.ceil((end-start)/inc) so you don't need if start + count * inc != end: ...
More Correct.
Even more correct, but not nearly complete... The 'start' and 'end' arguments in the previous scripts are out of place: the function initially starts at 0, and stop at 'end', 'end' itself exclusive, not the other way around.
Suggestion: Allow a precision to be specified. (Not implemented yet.)
Use Numeric. I think this is best solved by
then if you really need a list:
More memory-efficient implementation with generators. A naive but working xrange() -like implementation using generators could be as follows:
Usage:
Fast, flexible and memory-efficient; also accepts integers; does not require Numeric. The following implementation does not require Numeric and is fast, as the generator is created directly by python. It also accepts integers. There is no accumulation of errors, as the increment is not added incrementally.
This can break, unfortunately.
breaks the above frange4.
A possible solution would be to set start = None as a default argument and test whether start is None.
range -> xrange, for memory efficiency. In the example above, the range function should really be replaced the xrange function, if memory efficiency is desired.
Precision: in the doc string, "list(frange)" means "list(frange(start,...))".
Typo: int(ceil(...)/increment) -> int(ceil((...)/increment)). There is a small typo in the original code, which should be corrected as:
Also, it is possible to mimic the behavior of the built-in range even better: frange(0,5,-1) should return an empty list. This can be accomplished with:
Needs to test inputs. This should test the types of the input. If it's accidentally called with string inputs (e.g., "0.01", "1", "0.01") it will continue appending to the list until the machine runs out of memory.
Not that I have any experience of this...
short, no roundoff problems, fast. The following algorithm is short, fast, and immune to roundoff errors. It only has one float divide, and it treats start and stop values on equal footing. The downside (I guess) is that it takes the number of points as the third argument, not the step size.
Of course, you can modify it to take step size if you really want.
A less naive generator-based version:
Can be used like this:
or like this:
My versions use the original range function to create multiplicative indices for the shift. This allows same syntax to the original range function. I have made two versions, one using float, and one using Decimal, because I found that in some cases I wanted to avoid the roundoff drift introduced by the floating point arithmetic.
It is consistent with empty set results as in range/xrange.
Passing only a single numeric value to either function will return the standard range output to the integer ceiling value of the input parameter (so if you gave it 5.5, it would return range(6).)