Created
June 21, 2012 16:10
-
-
Save bmispelon/2966765 to your computer and use it in GitHub Desktop.
Useless math function.
This file contains hidden or 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 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