Change / operation to return exact quotient of two exact integers if
authorChris Hanson <org/chris-hanson/cph>
Thu, 12 Aug 1993 06:56:37 +0000 (06:56 +0000)
committerChris Hanson <org/chris-hanson/cph>
Thu, 12 Aug 1993 06:56:37 +0000 (06:56 +0000)
the numerator is a multiple of the denominator.

v7/src/6001/arith.scm

index 001288c087de3e9a8f6d86cc360bd88ee20c296f..5cf4a95cd7e5aae716b8e040b70907129a38f6fe 100644 (file)
@@ -1,8 +1,8 @@
 #| -*-Scheme-*-
 
-$Id: arith.scm,v 1.3 1992/09/26 15:40:02 cph Exp $
+$Id: arith.scm,v 1.4 1993/08/12 06:56:37 cph Exp $
 
-Copyright (c) 1989-92 Massachusetts Institute of Technology
+Copyright (c) 1989-93 Massachusetts Institute of Technology
 
 This material was developed by the Scheme project at the Massachusetts
 Institute of Technology, Department of Electrical Engineering and
@@ -127,10 +127,15 @@ MIT in each case. |#
        (else (int:* x y))))
 
 (define (real:/ x y)
-  (let ((y (if (flonum? y) y (int:->flonum y))))
-    (cond ((flonum? x) (flo:/ x y))
-         ((int:zero? x) x)
-         (else (flo:/ (int:->flonum x) y)))))
+  (cond ((flonum? x) (flo:/ x (if (flonum? y) y (int:->flonum y))))
+       ((flonum? y) (if (int:zero? x) x (flo:/ (int:->flonum x) y)))
+       ((int:= (int:remainder x y) 0) (int:quotient x y))
+       (else (flo:/ (int:->flonum x) (int:->flonum y)))))
+
+(define (real:invert x)
+  (cond ((flonum? x) (flo:/ 1. x))
+       ((int:= 1 x) x)
+       (else (flo:/ 1. (int:->flonum x)))))
 \f
 (define (real:= x y)
   (if (flonum? x)
@@ -307,7 +312,9 @@ MIT in each case. |#
                    (else 1.))))
        (if (flonum? y)
            (general-case (int:->flonum x) y)
-           (int:expt x y)))))
+           (if (int:negative? y)
+               (real:invert (int:expt x (int:negate y)))
+               (int:expt x y))))))
 
 (define number? rational?)
 (define complex? rational?)
@@ -381,7 +388,7 @@ MIT in each case. |#
 
 (define (/ z1 . zs)
   (cond ((null? zs)
-        (flo:/ 1. (if (flonum? z1) z1 (int:->flonum z1))))
+        (real:invert z1))
        ((null? (cdr zs))
         (real:/ z1 (car zs)))
        (else