Skip to content

Instantly share code, notes, and snippets.

@bmispelon
Created June 21, 2012 16:10
Show Gist options
  • Select an option

  • Save bmispelon/2966765 to your computer and use it in GitHub Desktop.

Select an option

Save bmispelon/2966765 to your computer and use it in GitHub Desktop.
Useless math function.
def intfract(p, q, precision=10):
"""Return a tuple from the integer division p/q.
The first element is the integer part (p // q).
The second is a list of length `precision` containing the decimals.
Note: if the division is exact, the decimals list will not contain the
trailing 0s.
"""
intpart, rest = divmod(p, q)
decimals = []
for _ in xrange(precision):
if not rest:
break
decimals.append(rest // q)
rest *= 10 * (rest % q)
return intpart, decimals
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment