[Issue 19514] New: gcd(BigInt(2), BigInt(1)) fails

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Dec 26 10:09:12 UTC 2018


https://issues.dlang.org/show_bug.cgi?id=19514

          Issue ID: 19514
           Summary: gcd(BigInt(2), BigInt(1)) fails
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: hos at hos.ac

The following code fails:
----
import std.bigint, std.numeric, std.stdio;
void main() {
  writeln(gcd(BigInt(2), BigInt(1)));
}
----

Output:
----
core.exception.AssertError@/dlang/dmd/linux/bin64/../../src/phobos/std/numeric.d(2714):
Assertion failure
----------------
??:? _d_assertp [0xad2af111]
/dlang/dmd/linux/bin64/../../src/phobos/std/numeric.d:2714 pure nothrow
std.bigint.BigInt std.numeric.gcd!(std.bigint.BigInt).gcd(std.bigint.BigInt,
std.bigint.BigInt) [0xad29f564]
onlineapp.d:3 _Dmain [0xad29c683]
----

In general, gcd(a, b) for BigInts will fail when a has more zero bits at the
end than b:

----
    static if (canUseBinaryGcd)
    {
        uint shift = 0;
        while ((a & 1) == 0 && (b & 1) == 0)
        {
            a >>= 1;
            b >>= 1;
            shift++;
        }

        do
        {
            assert((a & 1) != 0);
----

--


More information about the Digitalmars-d-bugs mailing list