#1062 continued fractions: sign of sqrt terms ignored

open
nobody
Lisp Core (472)
5
2012-11-18
2007-01-14
Robert Dodier
No

cflength : 4;
cf(sqrt(2) - 1);
=> [0, 2, 2, 2, 2] /* OK */
cf(sqrt(2) - 2);
=> [0, - 1, - 1, - 2, - 2, - 2] /* OK (but not canonicalized) */
cf(1 - sqrt(2));
=> [2, 2, 2, 2, 2] /* WRONG */
cf(1 + sqrt(2));
=> [2, 2, 2, 2, 2] /* OK */
cf(2 - sqrt(2));
=> [3, 2, 2, 2, 2] /* WRONG */
cf(2 + sqrt(2));
=> [3, 2, 2, 2, 2] /* OK */

cf ignores the minus sign on sqrt (i.e. treats -sqrt(foo) the same as +sqrt(foo)), as shown in the above examples. cf thinks minus signs are OK if not attached to sqrt.

Discussion

  • This is not only about the sign in the input . E.g.,

    (%i27) cflength:3$
    (%i28) cf((16*(119+48*sqrt(6)))/337);
    (%o28) [11, 4, 3, 4, 4, 5, 1, 27, 1, 6]
    period not visible ! And I claim the "5" is incorrect, should be "4"
    Though one can check that in all intermediate steps the sign of the sqrt(6) term is positive.
    Indeed :
    (%i29) float((16*(119+48*sqrt(6)))/337);
    (%o29) 11.23207157999252
    (%i30) float(cfdisrep([11, 4, 3, 4, 4]));
    (%o30) 11.23206751054852
    This term is below the true value, so next should be above :
    (%i31) float(cfdisrep([11, 4, 3, 4, 4, 4]));
    (%o31) 11.23207171314741
    Correct _ is above
    (%i32) float(cfdisrep([11, 4, 3, 4, 4, 5]));
    (%o32) 11.232070910556
    Wrong : is below.

    JF Mertens