From 9f7e0aa6a70c1770f0a80962826bc877b811b674 Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Sun, 21 Jul 2013 10:08:39 -0600 Subject: [PATCH] r6rs: fix `div' and `mod' Closes PR 13925 --- pkgs/r6rs-pkgs/r6rs-lib/rnrs/base-6.rkt | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/pkgs/r6rs-pkgs/r6rs-lib/rnrs/base-6.rkt b/pkgs/r6rs-pkgs/r6rs-lib/rnrs/base-6.rkt index 275c181909..0b5f4dc049 100644 --- a/pkgs/r6rs-pkgs/r6rs-lib/rnrs/base-6.rkt +++ b/pkgs/r6rs-pkgs/r6rs-lib/rnrs/base-6.rkt @@ -219,19 +219,17 @@ (raise-type-error 'nan? "real" n))) ;; Someone needs to look more closely at div and mod. -;; I started with the code from Enger04, and poked it -;; until the results matched the examples in R6RS. (define (div x y) (cond [(rational? y) - (let ([n (* (numerator x) - (denominator y))] - [d (* (denominator x) - (numerator y))]) - (if (negative? n) - (- (quotient (- (abs d) n 1) d)) - (quotient n d)))] + (cond + [(x . >= . 0) + (truncate (/ x y))] + [(y . > . 0) + (floor (/ x y))] + [else + (ceiling (/ x y))])] [(real? y) ;; infinity or nan (if (equal? y +nan.0)