Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #19740: Incorrect result of BigInt * BigInt. #6969

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ obj/
.dub/
libphobos_*
source/
/.project
20 changes: 20 additions & 0 deletions std/bigint.d
Original file line number Diff line number Diff line change
Expand Up @@ -1900,3 +1900,23 @@ void divMod(const BigInt dividend, const BigInt divisor, out BigInt quotient, ou
assert(r == -24);
assert(q * d + r == -c);
}

// Issue 19740
@system unittest
{
BigInt a = BigInt(
"241127122100380210001001124020210001001100000200003101000062221012075223052000021042250111300200000000000" ~
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
BigInt b = BigInt(
"700200000000500418321000401140010110000022007221432000000141020011323301104104060202100200457210001600142" ~
"000001012245300100001110215200000000120000000000000000000000000000000000000000000000000000000000000000000" ~
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");

BigInt c = a * b;
assert (c == BigInt(
"1688372108948068874722901180228375682334987075822938736581472847151834613694489486296103575639363261807341" ~
"3910091006778604956808730652275328822700182498926542563654351871390166691461743896850906716336187966456064" ~
"2702007176328110013356024000000000000000000000000000000000000000000000000000000000000000000000000000000000" ~
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ~
"0000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
}
3 changes: 2 additions & 1 deletion std/internal/math/biguintcore.d
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,8 @@ void mulInternal(BigDigit[] result, const(BigDigit)[] x, const(BigDigit)[] y)
mulKaratsuba(result[0 .. half + y.length], y, x[0 .. half], scratchbuff);
partial[] = result[half .. half + y.length];
mulKaratsuba(result[half .. $], y, x[half .. $], scratchbuff);
addAssignSimple(result[half .. half + y.length], partial);
BigDigit c = addAssignSimple(result[half .. half + y.length], partial);
if (c) multibyteIncrementAssign!('+')(result[half + y.length..$], c);
() @trusted { GC.free(scratchbuff.ptr); } ();
}
else
Expand Down