Skip to content

Instantly share code, notes, and snippets.

@orlp
Last active August 29, 2015 14:19
Show Gist options
  • Save orlp/e92b3b7d26ad9b11378e to your computer and use it in GitHub Desktop.
Save orlp/e92b3b7d26ad9b11378e to your computer and use it in GitHub Desktop.

If you have a number x of the form:

a.bc

Where a is the integer part, b is the non-repeating fractional part, and c is the repeating fractional part, then the resulting fraction has value:

a.b + c / ((10^num_digits(c) - 1) * 10^num_digits(b))

So for 0.333333... we have:

a = 0
b = 0
c = 3

Giving:

0.0 + 3 / ((10^1 - 1) * 10^0)
1 / 3

The more complex .20151515151515... we have:

a = 0
b = 20
c = 15

Giving:

0.20 + 15 / ((10^2 - 1) * 10^2)
133 / 660
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment