digitalmars.D - Re: "Unsigned-related bugs never occur in real code."
- bearophile <bearophileHUGS lycos.com> Jan 21 2010
- Don <nospam nospam.com> Jan 21 2010
- Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> Jan 21 2010
- Rainer Deyke <rainerd eldwood.com> Jan 21 2010
- Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> Jan 21 2010
- Rainer Deyke <rainerd eldwood.com> Jan 21 2010
And abs() of an unsigned number probably needs a compilation warning. Bye, bearophile
Jan 21 2010
bearophile wrote:And abs() of an unsigned number probably needs a compilation warning.
Not a warning, it's always an error.
Jan 21 2010
Don wrote:bearophile wrote:And abs() of an unsigned number probably needs a compilation warning.
Not a warning, it's always an error.
Ok, makes sense. I'll operate the change in Phobos. Andrei
Jan 21 2010
Don wrote:bearophile wrote:And abs() of an unsigned number probably needs a compilation warning.
Not a warning, it's always an error.
I can imagine abs(x) being useful in a generic function where x can be either signed or unsigned. -- Rainer Deyke - rainerd eldwood.com
Jan 21 2010
Rainer Deyke wrote:Don wrote:bearophile wrote:And abs() of an unsigned number probably needs a compilation warning.
I can imagine abs(x) being useful in a generic function where x can be either signed or unsigned.
std.traits has a Unsigned template. I plan to add two functions: signed(x) and unsigned(x), which transform the integral x into the signed/unsigned integral of the same size. Generic code could then call signed or unsigned wherever necessary. For abs, they'd have to call abs(signed(expr)) which I believe clarifies intent and averts bugs like mine. Alas, that's not fixing everything... Andrei
Jan 21 2010
Andrei Alexandrescu wrote:std.traits has a Unsigned template. I plan to add two functions: signed(x) and unsigned(x), which transform the integral x into the signed/unsigned integral of the same size. Generic code could then call signed or unsigned wherever necessary. For abs, they'd have to call abs(signed(expr)) which I believe clarifies intent and averts bugs like mine.
It would also be completely and utterly wrong. The correct replacement is this: T result; if (isSigned!T) { result = abs(v); } else { result = v; } ...which works, but is a lot less concise than simply saying 'v = abs(v)'. If I ever found myself writing the above in actual code, I'd be tempted to factor it out into its own function, call that function 'abs', and consistently use that function instead of the 'abs' in Phobos. -- Rainer Deyke - rainerd eldwood.com
Jan 21 2010
Rainer Deyke wrote:Andrei Alexandrescu wrote:std.traits has a Unsigned template. I plan to add two functions: signed(x) and unsigned(x), which transform the integral x into the signed/unsigned integral of the same size. Generic code could then call signed or unsigned wherever necessary. For abs, they'd have to call abs(signed(expr)) which I believe clarifies intent and averts bugs like mine.
It would also be completely and utterly wrong. The correct replacement is this: T result; if (isSigned!T) { result = abs(v); } else { result = v; } ....which works, but is a lot less concise than simply saying 'v = abs(v)'. If I ever found myself writing the above in actual code, I'd be tempted to factor it out into its own function, call that function 'abs', and consistently use that function instead of the 'abs' in Phobos.
Depends! In my case, your fix is wrong. Andrei
Jan 21 2010