diff --git a/src/bigint.cpp b/src/bigint.cpp index 3e47a11900..a68dd3a4b8 100644 --- a/src/bigint.cpp +++ b/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(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(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(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(rhsWords); + for (size_t i = 0; i < rhsWords; i += 1) { + Remainder->data.digits[i] = Make_64(R[i*2+1], R[i*2]); + } } } } diff --git a/test/cases/math.zig b/test/cases/math.zig index 9b949ffd16..090e2b9dfd 100644 --- a/test/cases/math.zig +++ b/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 {