toRational

Rational equivalent of its real argument with full precision.
the rational equivalent of its real argument with full precision
Lossless conversion from any representation of a rational to Rational
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.
Necessary when mixing NumericPrelude.Numeric Rationals with Prelude98 Rationals
Convert a decimal to a Rational
Convert an AlgReal to a Rational. If the AlgReal is exact, then you get a Left value. Otherwise, you get a Right value which is simply an approximation.
Returns a predicate and two integers, x and y. If the the predicate holds, then x / y is a rational representing the floating point number. Assumes the FP number is not one of the special ones that has no real representation.
Convert a floating point number to a rational, if possible.
Obtain a Rational representation of the ExchangeRate. This Rational is guaranteed to be a positive number.
Obtain the Rational representation of a Scale.
Convert a number to a Rational. Warning: This can use a lot of memory in the case of very large exponent parts.