bridge/class-master/depend/pari/src/basemath/arith1.c
/* continued fraction of a/b. If y != NULL, stop when partial quotients
 * differ from y */
continued fraction of a/b. If y != NULL, stop when partial quotients
differ from y
continued fraction of a / b . If y ! = NULL stop when partial quotients differ from y
static GEN
Qsfcont(GEN a, GEN b, GEN y, ulong k)
{
  GEN  z, c;
  ulong i, l, ly = lgefint(b);
  l = (ulong)(3 + bit_accuracy_mul(ly, 1.44042009041256));
  if (k > 0 && k+1 > 0 && l > k+1) l = k+1; 
  if (l > LGBITS) l = LGBITS;
  z = cgetg(l,t_VEC);
  l--;
  if (y) {
    pari_sp av = avma;
    if (l >= (ulong)lg(y)) l = lg(y)-1;
    for (i = 1; i <= l; i++)
    {
      GEN q = gel(y,i);
      gel(z,i) = q;
      c = b; if (!gequal1(q)) c = mulii(q, b);
      c = subii(a, c);
      if (signe(c) < 0)
      { 
        c = addii(c, b);
        if (signe(c) >= 0) i++; 
        break;
      }
      if (cmpii(c, b) >= 0)
      { 
        c = subii(c, b);
        if (cmpii(c, b) < 0) {
          if (i < l && equali1(gel(y,i+1))) gel(z,i) = addiu(q,1);
          i++;
        }
        break;
      }
      if ((i & 0xff) == 0) gerepileall(av, 2, &b, &c);
      a = b; b = c;
    }
  } else {
    a = icopy_lg(a, ly);
    b = icopy(b);
    for (i = 1; i <= l; i++)
    {
      gel(z,i) = truedvmdii(a,b,&c);
      if (c == gen_0) { i++; break; }
      affii(c, a); cgiv(c); c = a;
      a = b; b = c;
    }
  }
  i--;
  if (i > 1 && gequal1(gel(z,i)))
  {
    cgiv(gel(z,i)); --i;
    gel(z,i) = addui(1, gel(z,i)); 
  }
  setlg(z,i+1); return z;
}
static GEN Qsfcont ( GEN a , GEN b , GEN y , ulong k ) { GEN z , c ; ulong i , l , ly = lgefint ( b ) ; l = ( ulong ) ( 3 + bit_accuracy_mul ( ly , 1.44042009041256 ) ) ; if ( k > 0 && k + 1 > 0 && l > k + 1 ) l = k + 1 ; if ( l > LGBITS ) l = LGBITS ; z = cgetg ( l , t_VEC ) ; l -- ; if ( y ) { pari_sp av = avma ; if ( l >= ( ulong ) lg ( y ) ) l = lg ( y ) - 1 ; for ( i = 1 ; i <= l ; i ++ ) { GEN q = gel ( y , i ) ; gel ( z , i ) = q ; c = b ; if ( ! gequal1 ( q ) ) c = mulii ( q , b ) ; c = subii ( a , c ) ; if ( signe ( c ) < 0 ) { c = addii ( c , b ) ; if ( signe ( c ) >= 0 ) i ++ ; break ; } if ( cmpii ( c , b ) >= 0 ) { c = subii ( c , b ) ; if ( cmpii ( c , b ) < 0 ) { if ( i < l && equali1 ( gel ( y , i + 1 ) ) ) gel ( z , i ) = addiu ( q , 1 ) ; i ++ ; } break ; } if ( ( i & 0xff ) == 0 ) gerepileall ( av , 2 , & b , & c ) ; a = b ; b = c ; } } else { a = icopy_lg ( a , ly ) ; b = icopy ( b ) ; for ( i = 1 ; i <= l ; i ++ ) { gel ( z , i ) = truedvmdii ( a , b , & c ) ; if ( c == gen_0 ) { i ++ ; break ; } affii ( c , a ) ; cgiv ( c ) ; c = a ; a = b ; b = c ; } } i -- ; if ( i > 1 && gequal1 ( gel ( z , i ) ) ) { cgiv ( gel ( z , i ) ) ; -- i ; gel ( z , i ) = addui ( 1 , gel ( z , i ) ) ; } setlg ( z , i + 1 ) ; return z ; }
continued fraction of a/b.
continued fraction of a / b .
/* times 1 / log2( (1+sqrt(5)) / 2 )  */ /* beware overflow */ /* partial quotient too large */ /* by 1 */ /* partial quotient too small */ /* by 1. If next quotient is 1 in y, add 1 */ /* unclean: leave old z[i] on stack */