Skip to content

Instantly share code, notes, and snippets.

@mdbecker
Created September 24, 2011 20:05
Show Gist options
  • Save mdbecker/1239792 to your computer and use it in GitHub Desktop.
Save mdbecker/1239792 to your computer and use it in GitHub Desktop.
frange
def get_round_digits(step):
"""
Given input step, determine the number of digits step can be rounded to and
still equal step (limit 53)
"""
round_digits = 1
while round(step, round_digits) != step and round_digits <= 53:
round_digits += 1
if round_digits > 1:
round_digits -= 1
return round_digits
def frange(start, end, step):
"""
A float range generator. Only start is included in the output.
"""
result = []
while start + step < end:
result.append(start)
start += step
round_digits = get_round_digits(step)
if round(start, round_digits) < round(end, round_digits):
result.append(start)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment