Next Continued Fraction Arithmetic 45

Comparing continued fractions is easy

        /* Return -1 if a < b
         *         0 if a == b
         *        +1 if a > b
         */
        int compare_fractions(CFract a, CFract b) {
          int term = +1;
          for (;;) {
            if (null(a) && null(b)) 
              return 0;
            else if (null(a))
              return term * +1;
            else if (null(b))
              return term * -1;
            else {
              unsigned a0 = first(a), b0 = first(b);
              if      (a0 < b0) return -1 * term;
              else if (a0 > b0) return +1 * term;
              else { term = -term; a = rest(a); b = rest(b); }
            }
          }
        }

Next   Back