times-aux.lsp 670 Bytes
Newer Older
1 2 3 4 5
;-*- Mode:     Lisp -*-
;;;; Author:   Paul Dietz
;;;; Created:  Thu Aug 28 11:23:40 2003
;;;; Contains: Auxiliary functions for testing the multiplication operator *

6

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

(defun integer-times (x y)
  (assert (integerp x))
  (assert (integerp y))
  (let (neg)
    (when (< x 0)
      (setq neg t x (- x)))
    (let ((result (nat-times x y)))
      (if neg (- result) result))))

(defun nat-times (x y)
  ;; Assumes x >= 0
  (if (= x 0)
      0
    (let ((lo (if (oddp x) y 0))
sds's avatar
sds committed
22
          (hi (nat-times (ash x -1) y)))
23 24 25 26 27
      (+ lo (+ hi hi)))))

(defun rat-times (x y)
  (/ (integer-times (numerator x) (numerator y))
     (integer-times (denominator x) (denominator y))))