# Potential bug : missing terms in ContinuedFraction

If I use 0.23 instead of 23/100, the last continued fraction coefficient (7) is not given:

target = 23/100;
ContinuedFraction[target]        (* {0,4,2,1,7} *)
ContinuedFraction[N[target]]     (* {0,4,2,1} *)
FromContinuedFraction[%]         (* 3/13 *)
FromContinuedFraction[%%%]       (* 23/100 *)
% - %%                           (* -1/1300 *)


If I use something like 0.23001, the 7 will be there, and more coefficients after it; if I use something like 0.22999, there will be a 6 instead of the 7, and more stuff after the 6; but for plain 0.23, the 7 is just omitted, and the error of 1/1300 is whopping. It's not meant to be like that, is it?

EDIT

Ok, it's not a bug. Only the certain coefficients are being given, and in our case, the 7 is not among them, if N[] is used - see Chip Hurst's answer.

So I'd like to extend the question: If what I want is a result, such that the absolute difference between target and FromContinuedFraction[result] is smaller than a suitable epsilon, how do I make Mathematica give me that result, even if the last coefficient is uncertain?

(The issue appears to arise if and only if the input to ContinuedFraction is of the form N[rational number with smallish denominator] - in other cases, it seems, the "relevant" and the "certain" appear to coincide.)

• Would Rationalize help? Jun 11 '19 at 12:07
• sometimes - if the denominator is sufficiently small. For 0.23, it works fine. On the other hand, Rationalize[N[1352343/2010653]] doesn't perform as one would like (returns Head[]==Real). My bestGuessCF from my answer gives the coefficients of the exact fraction Jun 11 '19 at 13:15
• You could also use Rationalize[x, 0] or SetPrecision[x, Infinity]. Jun 11 '19 at 13:19
• Rationalize[SetPrecision[N[1352343/2010653], Infinity]] gives 6058142733606543/9007199254740992 Jun 11 '19 at 13:20
• Right -- it won't faithfully give back the original rational, but it will give an exact number very close to it. This means ContinuedFraction will give more terms. Jun 11 '19 at 13:22

Per the docs:

ContinuedFraction stops when it runs out of precision

The numbers just left of 0.23 will have 6 in the continued fraction, and the ones to the right will have 7. This means we can't know for sure which one to use, as 0.23 is not an exact number, and so it's dropped.

ContinuedFraction[23/100 - 1/10^20]

{0, 4, 2, 1, 6, 1, 9999999999999999, 7, 1, 2, 4}

ContinuedFraction[23/100 + 1/10^20]

{0, 4, 2, 1, 7, 9999999999999999, 1, 6, 1, 2, 4}

bestGuessCF = Function[target,
With[{cf = ContinuedFraction[target], k = Unique["k"]},
With[{cfx = Append[cf, k]},
With[{ext = FromContinuedFraction[cfx]},
With[{sol = Quiet[Solve[ext == target]]},
If[Length[sol] > 0, cfx /. k -> Round[k /. sol[]], cf]
]]]]];

• this solution adds exactly one term to Mathematica's solution of the continued fraction, if the first term omitted by Mathematica is an "uncertain cliff hanger" thing, and leaves Mathematica solution unchanged otherwise. Jun 16 '19 at 9:54
ContinuedFraction[Rationalize[0.23, 10^-12]]  (* {0,4,2,1,7} *)


credit @ Chip Hurst / see discussion in question.

• Hi Chip, I'll accept this own answer here (tomorrow), unless you want to have a go at summing up the discussion. Jun 11 '19 at 13:53