Fix border case in bignum division when the estimate gives a zero

remainder immediately after having overshot the previous estimate.
Fixes issue #303.
This commit is contained in:
Alex Shinn 2016-02-01 22:06:26 +09:00
parent 3e9092cfcc
commit 47381d8802
2 changed files with 10 additions and 1 deletions

View file

@ -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);
}

View file

@ -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))))