Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

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
You can’t perform that action at this time.