www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 20924] New: std.numeric.gcd cannot be used with const BigInt

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

          Issue ID: 20924
           Summary: std.numeric.gcd cannot be used with const BigInt
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: hsteoh quickfur.ath.cx

Code:
----------
import std.bigint;
import std.numeric;
import std.stdio;
void main() {
        const a = BigInt("123143238472389492934020");
        const b = BigInt("902380489324729338420924");
        writeln(gcd(a, b));
}
----------

Compiler output:
----------
/usr/src/d/phobos/std/numeric.d(3044): Error: template
std.bigint.BigInt.opAssign cannot deduce function from argument types
!()(BigInt) const, candidates are:
/usr/src/d/phobos/std/bigint.d(178):        opAssign(T)(T x)
/usr/src/d/phobos/std/bigint.d(194):        opAssign(T : BigInt)(T x)
/usr/src/d/phobos/std/numeric.d(3045): Error: template
std.bigint.BigInt.opAssign cannot deduce function from argument types
!()(const(BigInt)) const, candidates are:
/usr/src/d/phobos/std/bigint.d(178):        opAssign(T)(T x)
/usr/src/d/phobos/std/bigint.d(194):        opAssign(T : BigInt)(T x)
test.d(7): Error: template instance std.numeric.gcd!(const(BigInt)) error
instantiating
----------

There's no reason why const BigInt shouldn't work with gcd, since gcd doesn't
(shouldn't!) modify its arguments, and the following does work:

----------
import std.bigint;
//import std.numeric; // NG
import std.stdio;

// Copy-n-pasted code from std.numeric.gcd
BigInt gcd(in BigInt a, in BigInt b) {
        BigInt ua = a;
        BigInt ub = b;
        while (ub)
        {
            auto t = ub;
            ub = ua % ub;
            ua = t;
        }
        return ua;
}

void main() {
        const a = BigInt("123143238472389492934020");
        const b = BigInt("902380489324729338420924");
        writeln(gcd(a, b));
}
----------

Program output: 12

--
Jun 12