Bug 20924 – std.numeric.gcd cannot be used with const BigInt
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-06-12T22:50:24Z
Last change time
2020-09-18T21:05:40Z
Keywords
pull
Assigned to
No Owner
Creator
hsteoh
Comments
Comment #0 by hsteoh — 2020-06-12T22:50:24Z
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 by dlang-bot — 2020-06-15T06:40:58Z
@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 by dlang-bot — 2020-09-18T21:05:40Z
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