diff --git a/bignum.c b/bignum.c index c9f2d4e4..eaed306f 100644 --- a/bignum.c +++ b/bignum.c @@ -568,7 +568,8 @@ sexp sexp_bignum_quot_rem (sexp ctx, sexp *rem, sexp a, sexp b) { } } /* adjust signs */ - if (sign < 0) { + a1 = sexp_bignum_normalize(a1); + if (sign < 0 && a1 != SEXP_ZERO) { q = sexp_sub(ctx, q, SEXP_ONE); a1 = sexp_add(ctx, a1, b1); } diff --git a/lib/chibi/numeric-test.sld b/lib/chibi/numeric-test.sld index 79b24a9b..ef2184b5 100644 --- a/lib/chibi/numeric-test.sld +++ b/lib/chibi/numeric-test.sld @@ -337,5 +337,13 @@ (let ((r (/ (expt 2 61) 3))) (test 0 (- r r)) (test 2305843009213693952/3 r)) + (let ((x (+ (expt 2 32) +2)) + (y (+ (expt 2 32) -1))) + (test 0(remainder (* x y) y)) + (test 0(remainder (* x y) x))) + (let ((x (+ (expt 2 64) +2)) + (y (+ (expt 2 64) -1))) + (test 0(remainder (* x y) y)) + (test 0(remainder (* x y) x))) (test-end))))