From 7d38ec4786c895cc4b324378e0917e0a909fa131 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Mon, 20 Jan 2014 22:51:36 +0900 Subject: [PATCH] Shortcut for general quotient/remainder with a denominator of 1. --- bignum.c | 2 ++ tests/numeric-tests.scm | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bignum.c b/bignum.c index a4724c69..fe22aaeb 100644 --- a/bignum.c +++ b/bignum.c @@ -1474,6 +1474,7 @@ sexp sexp_quotient (sexp ctx, sexp a, sexp b) { int at=sexp_number_type(a), bt=sexp_number_type(b); sexp r=SEXP_VOID; sexp_gc_var1(tmp); + if (b == SEXP_ONE) return a; sexp_gc_preserve1(ctx, tmp); switch ((at * SEXP_NUM_NUMBER_TYPES) + bt) { case SEXP_NUM_NOT_NOT: case SEXP_NUM_NOT_FIX: @@ -1548,6 +1549,7 @@ sexp sexp_remainder (sexp ctx, sexp a, sexp b) { int at=sexp_number_type(a), bt=sexp_number_type(b); sexp r=SEXP_VOID; sexp_gc_var1(tmp); + if (b == SEXP_ONE) return SEXP_ZERO; sexp_gc_preserve1(ctx, tmp); switch ((at * SEXP_NUM_NUMBER_TYPES) + bt) { case SEXP_NUM_NOT_NOT: case SEXP_NUM_NOT_FIX: diff --git a/tests/numeric-tests.scm b/tests/numeric-tests.scm index 1769c516..80362b9c 100644 --- a/tests/numeric-tests.scm +++ b/tests/numeric-tests.scm @@ -173,7 +173,12 @@ (test #t (< 1.0 3/2)) (test #t (< 1/2 1.5)) (test #t (< 1/2 2.0)) - (test 1.0 (max 1/2 1.0))) + (test 1.0 (max 1/2 1.0)) + (test 18446744073709551617 (numerator (/ 18446744073709551617 2))) + (test "18446744073709551617/2" (number->string (/ 18446744073709551617 2))) + (let ((r (/ (expt 2 61) 3))) + (test 0 (- r r)) + (test 2305843009213693952/3 r))) (else #f))