Converts a
Scientific with a
repetend (a repeating part
in the fraction), which starts at the given index, into its
corresponding
Rational.
For example to convert the repeating decimal
0.03(571428) you
would use:
toRationalRepetend 0.03571428 2 == 1 % 28
Preconditions for
toRationalRepetend s r:
r >= 0
r < -(base10Exponent s)
WARNING: toRationalRepetend needs to compute the
Integer magnitude:
10^^n. Where
n is based on
the
base10Exponent of the scientific. If applied to a huge
exponent this could fill up all space and crash your program! So don't
apply this function to untrusted input.
The formula to convert the
Scientific s with a
repetend starting at index
r is described in the paper:
turning_repeating_decimals_into_fractions.pdf and is defined as
follows:
(fromInteger nonRepetend + repetend % nines) /
fromInteger (10^^r)
where
c = coefficient s
e = base10Exponent s
-- Size of the fractional part.
f = (-e)
-- Size of the repetend.
n = f - r
m = 10^^n
(nonRepetend, repetend) = c `quotRem` m
nines = m - 1
Also see:
fromRationalRepetend.