D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20924 - std.numeric.gcd cannot be used with const BigInt
Summary: std.numeric.gcd cannot be used with const BigInt
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P1 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2020-06-12 22:50 UTC by hsteoh
Modified: 2020-09-18 21:05 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description hsteoh 2020-06-12 22:50:24 UTC
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
Comment 1 Dlang Bot 2020-06-15 06:40:58 UTC
@Biotronic created dlang/phobos pull request #7531 "Fix issue 20924 - std.numeric.gcd cannot be used with const BigInt" fixing this issue:

- Fix issue 20924

https://github.com/dlang/phobos/pull/7531
Comment 2 Dlang Bot 2020-09-18 21:05:40 UTC
dlang/phobos pull request #7531 "Fix issue 20924 - std.numeric.gcd cannot be used with const BigInt" was merged into master:

- a10a3a43efe2eb24e49fccb1b06b56f87e234901 by Simen Kjærås:
  Fix issue 20924

https://github.com/dlang/phobos/pull/7531