authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-16 03:09:44-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-01-16 03:09:44-05:00
log6a95b88d1b3a619532feedffc674fc4b5bf7517b
tree0a77195ea28e98f1868c942258176de36d7a48f9
parent84d8584c5b315c24a00fa444d758a6b78f80f4a0

fix bigint remainder division

See #405

2 files changed, 17 insertions(+), 8 deletions(-)

src/bigint.cpp+16-8
...@@ -1015,21 +1015,29 @@ static void bigint_unsigned_division(const BigInt *op1, const BigInt *op2, BigIn...@@ -1015,21 +1015,29 @@ static void bigint_unsigned_division(const BigInt *op1, const BigInt *op2, BigIn
10151015
1016 // If the caller wants the quotient1016 // If the caller wants the quotient
1017 if (Quotient) {1017 if (Quotient) {
1018 Quotient->digit_count = lhsWords;
1019 Quotient->data.digits = allocate<uint64_t>(lhsWords);
1020 Quotient->is_negative = false;1018 Quotient->is_negative = false;
1021 for (size_t i = 0; i < lhsWords; i += 1) {1019 Quotient->digit_count = lhsWords;
1022 Quotient->data.digits[i] = Make_64(Q[i*2+1], Q[i*2]);1020 if (lhsWords == 1) {
1021 Quotient->data.digit = Make_64(Q[1], Q[0]);
1022 } else {
1023 Quotient->data.digits = allocate<uint64_t>(lhsWords);
1024 for (size_t i = 0; i < lhsWords; i += 1) {
1025 Quotient->data.digits[i] = Make_64(Q[i*2+1], Q[i*2]);
1026 }
1023 }1027 }
1024 }1028 }
10251029
1026 // If the caller wants the remainder1030 // If the caller wants the remainder
1027 if (Remainder) {1031 if (Remainder) {
1028 Remainder->digit_count = rhsWords;
1029 Remainder->data.digits = allocate<uint64_t>(rhsWords);
1030 Remainder->is_negative = false;1032 Remainder->is_negative = false;
1031 for (size_t i = 0; i < rhsWords; i += 1) {1033 Remainder->digit_count = rhsWords;
1032 Remainder->data.digits[i] = Make_64(R[i*2+1], R[i*2]);1034 if (rhsWords == 1) {
1035 Remainder->data.digit = Make_64(R[1], R[0]);
1036 } else {
1037 Remainder->data.digits = allocate<uint64_t>(rhsWords);
1038 for (size_t i = 0; i < rhsWords; i += 1) {
1039 Remainder->data.digits[i] = Make_64(R[i*2+1], R[i*2]);
1040 }
1033 }1041 }
1034 }1042 }
1035}1043}
test/cases/math.zig+1
...@@ -47,6 +47,7 @@ fn testDivision() {...@@ -47,6 +47,7 @@ fn testDivision() {
47 assert(@divTrunc(-1194735857077236777412821811143690633098347576,47 assert(@divTrunc(-1194735857077236777412821811143690633098347576,
48 -508740759824825164163191790951174292733114988) ==48 -508740759824825164163191790951174292733114988) ==
49 2);49 2);
50 assert(4126227191251978491697987544882340798050766755606969681711 % 10 == 1);
50 }51 }
51}52}
52fn div(comptime T: type, a: T, b: T) -> T {53fn div(comptime T: type, a: T, b: T) -> T {