Created
September 24, 2011 20:05
-
-
Save mdbecker/1239792 to your computer and use it in GitHub Desktop.
frange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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