I've found, empirically, that I can ensure Rationalize[ ] provides an exact rational conversion of whatever decimal I enter (call it "num") if I evaluate
Rationalize[num`z, 0] where:
$z \geq 2*\text{Length@RealDigits[num][[1]]} + 1$
E.g.,
Length@RealDigits[0.97646105481464028][[1]]
==>16
so:
Rationalize[0.97646105481464028`33, 0]
==>24411526370366007/25000000000000000
24411526370366007/25000000000000000==97646105481464028/100000000000000000
==>True
but
Rationalize[0.39984759300000068`32, 0]
==>5594893179322259/5729765822953714
5594893179322259/5729765822953714==97646105481464028/100000000000000000
==>False
I'd like to understand why that's the cutoff*, so I'll be posting my own question on that soon. [*That's the general cutoff; there are some decimals that are exactly converted with a lower value of z, but I've not found any that require a higher value for z.]
Alternately, you can also achieve an exact rational conversion using Round[num`y, 10^-y], where (again, empricially), I've found that:
$y \geq \text{Length@RealDigits[num][[1]]} + 1$
Thus:
Round[0.97646105481464028`17, 10^-17]
==>24411526370366007/25000000000000000
but
Round[0.39984759300000068`16, 10^-16]
==>5594893179322259/5729765822953714
Finally, one way to understand why SetPrecision[num`z, Infinity] won't generally give an exact rational conversion of the decimal you enter is that SetPrecision[ ] is restricted to converting "num" to that subset of rational base-10 numbers that can be represented in binary with a finite number of digits; it then pads that binary with an infinite number of zeros. And, while there are exceptions (e.g., 0.5), decimals typically can't be exactly represented with a finite binary.
Thus:
RealDigits[SetPrecision[0.97646105481464028`33, Infinity], 2, 300]
==>{1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1,
0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1,
1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1,
1, 1, 1, 1, 1, 0, 1, 0, 0, 0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0, 1, 1, 0, 0, 0, 1, 1,
1, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0,
0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1,
1, 0, 1, 1, 1, 0, 0, 0}
You might also want to take a look at the answer by george2079 here: Enter exact rational numbers easily with decimal notation
Note also that the part of the documentation you quoted with p,q,c
applies only to the single-argument Rationalize[x]
forms, as noted by sn6uv here: Rationalize error. For the dual-argument forms, Rationalize[x, dx]
(excepting where dx = 0, which is what I was using above), Daniel Lichtblau, at that same link, says: "I'd say the documentation could be worded better for the 2-argument case of Rationalize. There is an interplay between denominator of result, epsilon, and size of residual. If you take dx as replacing the default epsilon then I think the correct claim might be to the effect: ratPi = Rationalize[N[Pi], eps]; Abs[N[Pi] - ratPi] < N[eps]/Denominator[ratPi]^2.
"
p,q,c
. $\endgroup$Rationalize
is when you've numerically computed a rational number (or what you hope to be a rational number), and you want to obtain the true rational value from its numerical approximation. Exactly representing the specific floating-point approximation is the wrong answer to this problem. $\endgroup$