]> gitweb.factorcode.org Git - factor.git/commitdiff
fix division and modulo giving incorrect results
authorSlava Pestov <slava@factorcode.org>
Fri, 27 Aug 2004 03:05:48 +0000 (03:05 +0000)
committerSlava Pestov <slava@factorcode.org>
Fri, 27 Aug 2004 03:05:48 +0000 (03:05 +0000)
library/test/math/bignum.factor
native/bignum.c
native/s48_bignum.c
native/s48_bignum.h
native/s48_bignumint.h

index 1e17337b8e123f556d9b9070987f50681a2180b1..28cf2fb1da2528bdcf7c3faf80437ee64294d74b 100644 (file)
@@ -23,3 +23,4 @@ unit-test
 [ 1/268435456 ] [ -1 -268435456 >fixnum / ] unit-test
 [ 0 ] [ -1 -268435456 >fixnum /i ] unit-test
 [ 0 -1 ] [ -1 -268435456 >fixnum /mod ] unit-test
+[ 14355 ] [ 1591517158873146351817850880000000 32769 mod ] unit-test
index 7129ff861cb9545baea2195d32559d6143b277fc..369e0dbe610bedbdea99e5db53db80b85511d1ec 100644 (file)
@@ -120,10 +120,7 @@ CELL divide_bignum(ARRAY* x, ARRAY* y)
 
 CELL divint_bignum(ARRAY* x, ARRAY* y)
 {
-       ARRAY* q = s48_bignum_quotient(x,y);
-       if(q == NULL)
-               raise(SIGFPE);
-       return tag_object(q);
+       return tag_object(s48_bignum_quotient(x,y));
 }
 
 CELL divfloat_bignum(ARRAY* x, ARRAY* y)
@@ -135,20 +132,15 @@ CELL divfloat_bignum(ARRAY* x, ARRAY* y)
 
 CELL divmod_bignum(ARRAY* x, ARRAY* y)
 {
-       ARRAY* q;
-       ARRAY* r;
-       if(s48_bignum_divide(x,y,&q,&r))
-               raise(SIGFPE);
+       ARRAY *q, *r;
+       s48_bignum_divide(x,y,&q,&r);
        dpush(tag_object(q));
        return tag_object(r);
 }
 
 CELL mod_bignum(ARRAY* x, ARRAY* y)
 {
-       ARRAY* r = s48_bignum_remainder(x,y);
-       if(r == NULL)
-               raise(SIGFPE);
-       return tag_object(r);
+       return tag_object(s48_bignum_remainder(x,y));
 }
 
 CELL and_bignum(ARRAY* x, ARRAY* y)
index 4f795a9315997792f3eb5caa6f08e783368de4bd..656d18d33335bc9b89d639b09d57990dcde2b19a 100644 (file)
@@ -177,12 +177,15 @@ s48_bignum_multiply(bignum_type x, bignum_type y)
   return (bignum_multiply_unsigned (x, y, negative_p));
 }
 
-int
-bignum_divide(bignum_type numerator, bignum_type denominator,
+void
+s48_bignum_divide(bignum_type numerator, bignum_type denominator,
                  bignum_type * quotient, bignum_type * remainder)
 {
   if (BIGNUM_ZERO_P (denominator))
-    return (1);
+    {
+      raise(SIGFPE);
+      return;
+    }
   if (BIGNUM_ZERO_P (numerator))
     {
       (*quotient) = (BIGNUM_MAYBE_COPY (numerator));
@@ -244,15 +247,6 @@ bignum_divide(bignum_type numerator, bignum_type denominator,
          }
        }
     }
-  return (0);
-}
-
-int
-s48_bignum_divide(bignum_type numerator, bignum_type denominator,
-                 void* quotient, void * remainder)
-{
-  return bignum_divide(numerator, denominator,
-                      (bignum_type *)quotient, (bignum_type *)remainder);
 }
 
 bignum_type
index 416d0122174d67219460ba80e8649b5789324bdf..91fcb5bdd457461194c288e27870f3bdce71a770 100644 (file)
@@ -60,8 +60,6 @@ bignum_type s48_bignum_add(bignum_type, bignum_type);
 bignum_type s48_bignum_subtract(bignum_type, bignum_type);
 bignum_type s48_bignum_negate(bignum_type);
 bignum_type s48_bignum_multiply(bignum_type, bignum_type);
-int s48_bignum_divide(bignum_type numerator, bignum_type denominator,
-                            void * quotient, void * remainder);
 bignum_type s48_bignum_quotient(bignum_type, bignum_type);
 bignum_type s48_bignum_remainder(bignum_type, bignum_type);
 bignum_type s48_long_to_bignum(long);
index 2d7f89dde23a10de64bece2fa5611bcef4e7f021..4284782ca9f3d4a83e56cfec983f159da2fdbf77 100644 (file)
@@ -42,8 +42,8 @@ MIT in each case. */
    definition is `CHAR_BIT', which is defined in the Ansi C header
    file "limits.h". */
 
-typedef CELL bignum_digit_type;
-typedef CELL bignum_length_type;
+typedef long bignum_digit_type;
+typedef long bignum_length_type;
 
 /* BIGNUM_ALLOCATE allocates a (length + 1)-element array of
    `bignum_digit_type'; deallocation is the responsibility of the