Bug 13929 – nothrow @nogc gcd with signed integers

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2015-01-03T12:16:00Z
Last change time
2018-03-26T23:59:47Z
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2015-01-03T12:16:00Z
void main() nothrow @nogc { import std.numeric: gcd; auto z = gcd(10, 20); } dmd 2.067alpha gives: test.d(3,17): Error: @nogc function 'D main' cannot call non-@nogc function 'std.numeric.gcd!int.gcd' test.d(3,17): Error: 'std.numeric.gcd!int.gcd' is not nothrow test.d(1,6): Error: function 'D main' is nothrow yet may throw Perhaps it's better to replace the enforce() with an assert in a pre-condition. Currently gcd is implemented like this: T gcd(T)(T a, T b) { static if (is(T == const) || is(T == immutable)) { return gcd!(Unqual!T)(a, b); } else { static if (T.min < 0) { enforce(a >= 0 && b >=0); } while (b) { auto t = b; b = a % b; a = t; } return a; } }
Comment #1 by r9shackleford — 2015-04-01T05:13:18Z
gcd(a,b) = gcd(|a|,|b|) = gcd(|a|,b) = gcd(a,|b|) - is there a technical reason we reject negative instead of using abs on possible negative(i.e, T.min < 0)?
Comment #2 by greensunny12 — 2018-03-26T23:59:47Z
This works since 2.072.2: https://run.dlang.io/is/1CXrc8 > gcd(a,b) = gcd(|a|,|b|) = gcd(|a|,b) = gcd(a,|b|) - is there a technical reason we reject negative instead of using abs on possible negative(i.e, T.min < 0)? Please open a separate issue or thread on NG.learn for this.