Skip to content

Commit

Permalink
fix bigint remainder division
Browse files Browse the repository at this point in the history
See #405
  • Loading branch information
andrewrk committed Jan 16, 2018
1 parent 84d8584 commit 6a95b88
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/bigint.cpp
Expand Up @@ -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]);
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions test/cases/math.zig
Expand Up @@ -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 {
Expand Down

0 comments on commit 6a95b88

Please sign in to comment.