Commit 6a95b88d1b

Andrew Kelley <superjoe30@gmail.com>
2018-01-16 09:09:44
fix bigint remainder division
See #405
1 parent 84d8584
Changed files (2)
src
test
cases
src/bigint.cpp
@@ -1015,21 +1015,29 @@ static void bigint_unsigned_division(const BigInt *op1, const BigInt *op2, BigIn
 
     // If the caller wants the quotient
     if (Quotient) {
-        Quotient->digit_count = lhsWords;
-        Quotient->data.digits = allocate<uint64_t>(lhsWords);
         Quotient->is_negative = false;
-        for (size_t i = 0; i < lhsWords; i += 1) {
-            Quotient->data.digits[i] = Make_64(Q[i*2+1], Q[i*2]);
+        Quotient->digit_count = lhsWords;
+        if (lhsWords == 1) {
+            Quotient->data.digit = Make_64(Q[1], Q[0]);
+        } else {
+            Quotient->data.digits = allocate<uint64_t>(lhsWords);
+            for (size_t i = 0; i < lhsWords; i += 1) {
+                Quotient->data.digits[i] = Make_64(Q[i*2+1], Q[i*2]);
+            }
         }
     }
 
     // If the caller wants the remainder
     if (Remainder) {
-        Remainder->digit_count = rhsWords;
-        Remainder->data.digits = allocate<uint64_t>(rhsWords);
         Remainder->is_negative = false;
-        for (size_t i = 0; i < rhsWords; i += 1) {
-            Remainder->data.digits[i] = Make_64(R[i*2+1], R[i*2]);
+        Remainder->digit_count = rhsWords;
+        if (rhsWords == 1) {
+            Remainder->data.digit = Make_64(R[1], R[0]);
+        } else {
+            Remainder->data.digits = allocate<uint64_t>(rhsWords);
+            for (size_t i = 0; i < rhsWords; i += 1) {
+                Remainder->data.digits[i] = Make_64(R[i*2+1], R[i*2]);
+            }
         }
     }
 }
test/cases/math.zig
@@ -47,6 +47,7 @@ fn testDivision() {
         assert(@divTrunc(-1194735857077236777412821811143690633098347576,
             -508740759824825164163191790951174292733114988) ==
             2);
+        assert(4126227191251978491697987544882340798050766755606969681711 % 10 == 1);
     }
 }
 fn div(comptime T: type, a: T, b: T) -> T {