(define %bignum-quotient (letrec ((bqn (lambda (u v nu nv) (if (bignum? nv) (bqn u v (quotient nu 2) (quotient nv 2)) (begin ; (writeln "norm" nu nv) (bqd u v nu nv))))) (bqd (lambda (u v nu nv) (let* ((tq (quotient nu nv)) (tr (- u (* tq v)))) ; (writeln "tq" tq "tr" tr) (if (or (negative? tr) (>= tr v)) (begin ; (writeln "tq needs adj.") (let* ((atr (abs tr)) (a (bqn atr v atr v))) ; (writeln a) (let* ((tq (if (negative? tr) (- tq (add1 (car a))) (+ tq (car a)))) (tr (- u (* tq v)))) ; (writeln "atq" tq "atr" tr) (if (or (negative? tr) (>= tr v)) (begin ; (writeln "tq needs further adjustment") (let* ((tq (if (negative? tr) (sub1 tq) (add1 tq))) (tr (- u (* tq v)))) ; (writeln "fatq" tq "fatr" tr) (if (or (negative? tr) (>= tr v)) (error '%bignum-quotient "internal error") (cons tq tr)))) (cons tq tr))))) (cons tq tr)))))) (lambda (u v w) ; (writeln "init" u v) (let* ((au (abs u)) (av (abs v)) (su (negative? u)) (sv (negative? v)) (result (bqn au av au av)) (q (car result)) (r (cdr result))) (if w (if (eq? su sv) q (- q)) (if su (- r) r))))))