Range function
Xah Lee
xah at xahlee.org
Sun May 15 05:52:12 EDT 2005
Here's the Python solution.
----------
# -*- coding: utf-8 -*-
# Python
# http://xahlee.org/tree/tree.html
# Xah Lee, 2005-05
# implementation note: When iStep is a decimal, rounding error
# accumulates. For example, the last item returned from
# Range(0,18,0.3) is 17.7 not 18. A remedy is to turn iStep into a
# fraction and do exact arithmetics, and possibly convert the result
# back to decimal. A lesser workaround is to split the interval as to
# do multiple smaller range and join them together.
def Range(iMin, iMax=None, iStep=None):
if (iMax==None and iStep==None):
return Range(1,iMin)
if iStep==None:
return Range(iMin,iMax,1)
if iMin <= iMax and iStep > 0:
if (isinstance(iStep,int) or isinstance(iStep,long)):
return range( iMix, iMax, iStep)
else:
result=[];temp=iStep
while iMin <= iMax:
result.append(iMin)
iMin += iStep
return result
# test
print Range(0, 18, 0.3)
More information about the Python-list
mailing list