www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: "Unsigned-related bugs never occur in real code."

reply bearophile <bearophileHUGS lycos.com> writes:
And abs() of an unsigned number probably needs a compilation warning.

Bye,
bearophile
Jan 21 2010
parent reply Don <nospam nospam.com> writes:
bearophile wrote:
 And abs() of an unsigned number probably needs a compilation warning.

Not a warning, it's always an error.
Jan 21 2010
next sibling parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
prev sibling parent reply Rainer Deyke <rainerd eldwood.com> writes:
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
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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
parent reply Rainer Deyke <rainerd eldwood.com> writes:
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
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
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