* Added div, mod, div-and-mod, div0, mod0, div0-and-mod0.

This commit is contained in:
Abdulaziz Ghuloum 2007-09-12 02:37:03 -04:00
parent 9d8ceef99f
commit ed45b486a3
5 changed files with 68 additions and 6 deletions

Binary file not shown.

View File

@ -2371,3 +2371,57 @@
(go x eps)]
[else (error who "~s is not a number" eps)])]
[else (error who "~s is not a number" x)])))
(library (ikarus r6rs-fu div/mod)
(export div mod div-and-mod div0 mod0 div0-and-mod0)
(import
(except (ikarus)
div mod div-and-mod div0 mod0 div0-and-mod0))
(define (div-and-mod x y)
(define who 'div-and-mod)
(unless (integer? x)
(error who "~s is not an integer" x))
(unless (and (integer? y) (not (= y 0)))
(error who "~s is not an integer" y))
(if (> x 0)
(quotient+remainder x y)
(if (> y 0)
(let-values ([(q r) (quotient+remainder (- x y) y)])
(values q (+ r y)))
(let-values ([(q r) (quotient+remainder (+ x y) y)])
(values q (- r y))))))
(define (div x y)
(let-values ([(n m) (div-and-mod x y)])
n))
(define (mod x y)
(let-values ([(n m) (div-and-mod x y)])
m))
(define (div0-and-mod0 x y)
(define who 'div0-and-mod0)
(unless (integer? x)
(error who "~s is not an integer" x))
(unless (and (integer? y) (not (= y 0)))
(error who "~s is not an integer" y))
(let-values ([(d m) (div-and-mod x y)])
(if (> y 0)
(if (< m (/ y 2))
(values d m)
(values (+ d 1) (- m y)))
(if (> m (/ y -2))
(values (- d 1) (+ m y))
(values d m)))))
(define (div0 x y)
(let-values ([(n m) (div0-and-mod0 x y)])
n))
(define (mod0 x y)
(let-values ([(n m) (div0-and-mod0 x y)])
m)))

View File

@ -530,6 +530,12 @@
[modulo i r]
[remainder i r]
[quotient+remainder i r]
[div i]
[mod i]
[div-and-mod i]
[div0 i]
[mod0 i]
[div0-and-mod0 i]
[number->string i r]
[string->number i r]
[flonum->string i]

View File

@ -4,6 +4,7 @@
(tests reader)
(tests bytevectors)
(tests strings)
(tests numbers)
(tests bignum-to-flonum)
(tests string-to-number))
@ -26,4 +27,5 @@
(test-exact-integer-sqrt)
(test-bignum-to-flonum)
(test-string-to-number)
(test-div-and-mod)
(printf "Happy Happy Joy Joy\n")

View File

@ -134,10 +134,12 @@
[cons C ba se]
[cos C ba se]
[denominator C ba se]
[div S ba]
[div-and-mod S ba]
[div0 S ba]
[div0-and-mod0 S ba]
[div C ba]
[mod C ba]
[div-and-mod C ba]
[div0 C ba]
[mod0 C ba]
[div0-and-mod0 C ba]
[dynamic-wind C ba se]
[eq? C ba se]
[equal? S ba se]
@ -177,8 +179,6 @@
[map C ba se]
[max C ba se]
[min C ba se]
[mod S ba]
[mod0 S ba]
[nan? S ba]
[negative? C ba se]
[not C ba se]