Skip to content

Commit

Permalink
Fix issue 18434: gcd(BigInt(0), BigInt(1)) fails
Browse files Browse the repository at this point in the history
The special cases of `gcd(0,a)` and `gcd(a,0)` are not handled correctly
when `BigInt`s are involved.
  • Loading branch information
H. S. Teoh committed Feb 13, 2018
1 parent 8d52541 commit fb66d07
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions std/numeric.d
Expand Up @@ -2684,6 +2684,12 @@ T gcd(T)(T a, T b)

assert(a >= 0 && b >= 0);

// Special cases.
if (a == 0)
return b;
if (b == 0)
return a;

static if (canUseBinaryGcd)
{
uint shift = 0;
Expand Down Expand Up @@ -2726,6 +2732,9 @@ T gcd(T)(T a, T b)
assert(gcd(BigInt("71_000_000_000_000_000_000"),
BigInt("31_000_000_000_000_000_000")) ==
BigInt("1_000_000_000_000_000_000"));

assert(gcd(BigInt(0), BigInt(1234567)) == BigInt(1234567));
assert(gcd(BigInt(1234567), BigInt(0)) == BigInt(1234567));
}

@safe pure nothrow unittest
Expand Down

0 comments on commit fb66d07

Please sign in to comment.