Created
August 2, 2022 10:43
-
-
Save willmcgugan/11fd2f0b6e88168d1c7247621a7344ee to your computer and use it in GitHub Desktop.
Fractions are accurate
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 segments_float(size, parts): | |
end = 0.0 | |
part = size / parts | |
for n in range(parts): | |
start = int(end) | |
end += part | |
print(str(n) * int(end - start), end="") | |
print() | |
from fractions import Fraction | |
def segments_fraction(size, parts): | |
end = Fraction(0) | |
part = Fraction(size, parts) | |
for n in range(parts): | |
start = int(end) | |
end += part | |
print(str(n) * int(end - start), end="") | |
print() | |
SIZE = 24 | |
PARTS = 7 | |
print("-" * SIZE) | |
segments_float(SIZE, PARTS) | |
segments_fraction(SIZE, PARTS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment