Thread overview
Prefer Signed or Unsigned in D?
Sep 01, 2015
John Carter
Sep 02, 2015
BBasile
Sep 02, 2015
ponce
Sep 02, 2015
John Carter
Sep 02, 2015
ponce
Sep 03, 2015
John Carter
September 01, 2015
C/C++ discussion here....

   http://blog.robertelder.org/signed-or-unsigned-part-2/

D rules here...

   http://dlang.org/type.html#integer-promotions
September 02, 2015
On Tuesday, 1 September 2015 at 23:06:50 UTC, John Carter wrote:
> C/C++ discussion here....
>
>    http://blog.robertelder.org/signed-or-unsigned-part-2/
>
> D rules here...
>
>    http://dlang.org/type.html#integer-promotions

It depends on the context.

You should take care of blending signed and unsigned:
comparison error, a is > b but...
---
uint a = 1;
int b = -1;
assert(a < b); // does not throw
---

You should take care to the index type in a loop:
loop that doesn't run at all because of an infered unsigned index...
---
auto array = new int[](8);
for (auto i = array.length - 1; i > -1; i--)
    array[i] = 8;
assert(array[0] == 0); // does not throw
---
September 02, 2015
On Wednesday, 2 September 2015 at 09:47:16 UTC, BBasile wrote:
> On Tuesday, 1 September 2015 at 23:06:50 UTC, John Carter wrote:
>> C/C++ discussion here....
>>
>>    http://blog.robertelder.org/signed-or-unsigned-part-2/
>>
>> D rules here...
>>
>>    http://dlang.org/type.html#integer-promotions
>
> It depends on the context.
>
> You should take care of blending signed and unsigned:
> comparison error, a is > b but...
> ---
> uint a = 1;
> int b = -1;
> assert(a < b); // does not throw
> ---
>
> You should take care to the index type in a loop:
> loop that doesn't run at all because of an infered unsigned index...
> ---
> auto array = new int[](8);
> for (auto i = array.length - 1; i > -1; i--)
>     array[i] = 8;
> assert(array[0] == 0); // does not throw
> ---

I wish the following would be the standard D behaviour (for T == U or if floats are involved neither C nor D have a problem)):

int opCmp(T, U)(const(T) a, const(U) b) pure @safe @nogc nothrow
   if(isIntegral!T && isIntegral!U && !is(Unqual!T == Unqual!U))
{
   alias C = CommonType!(T, U);
   static if(isSigned!T && isUnsigned!U && T.sizeof <= U.sizeof)
      return (a < 0) ? -1 : opCmp(cast(U)a, b);
   else static if(isUnsigned!T && isSigned!U && T.sizeof >= U.sizeof)
      return (b < 0) ? 1 : opCmp(a, cast(T)b);
   else return opCmp(cast(C)a, cast(C)b);
}

this is really almost equally fast, but always correct.

September 02, 2015
On Tuesday, 1 September 2015 at 23:06:50 UTC, John Carter wrote:
> C/C++ discussion here....
>
>    http://blog.robertelder.org/signed-or-unsigned-part-2/
>
> D rules here...
>
>    http://dlang.org/type.html#integer-promotions

Everything Bjarne said still applies equally to D code, since integer promotion is identical with C from what I understand.
September 02, 2015
On Wednesday, 2 September 2015 at 11:03:00 UTC, ponce wrote:
> On Tuesday, 1 September 2015 at 23:06:50 UTC, John Carter wrote:
>> C/C++ discussion here....
>>
>>    http://blog.robertelder.org/signed-or-unsigned-part-2/
>>
>> D rules here...
>>
>>    http://dlang.org/type.html#integer-promotions
>
> Everything Bjarne said still applies equally to D code, since integer promotion is identical with C from what I understand.

Hmm. What Robert Elder says also applies still. And in my world his argument about undefined behavior carries weight.

Bugs that emerge from different optimizations / different versions of the compiler / on different CPU's are nightmares in my domain.

Here is a bug that existed in the JDK for 9 years (and probably in many other places....)
http://googleresearch.blogspot.co.nz/2006/06/extra-extra-read-all-about-it-nearly.html

So given the advice on mixing unsigned and signed, and the complexity of Dominikus's code to get signed and unsigned code to compare sanely....

Maybe if his code becomes standard in D...

Yup, mixing signed and unsigned is A Bad Thing, and you best go with whatever your base libraries give you.

The whole problem....
   http://www.di.unipi.it/~ruggieri/Papers/semisum.pdf
...makes me feel vaguely ill and long for a Numerical Tower.

I really must get around to benchmarking BigInt....



September 02, 2015
On Wednesday, 2 September 2015 at 21:22:59 UTC, John Carter wrote:
> On Wednesday, 2 September 2015 at 11:03:00 UTC, ponce wrote:
>>
>> Everything Bjarne said still applies equally to D code, since integer promotion is identical with C from what I understand.
>
> Hmm. What Robert Elder says also applies still. And in my world his argument about undefined behavior carries weight.

I'd like to point out that while Robert Elder has remarkably documented articles, his conclusions are still contrary to most C++ experts (Bjarne, Meyers, Andrei...).

Additionally, I was said weeks ago on this NG that and signed overflow in D is not actually Undefined Behaviour.


September 03, 2015
On Wednesday, 2 September 2015 at 21:43:17 UTC, ponce wrote:

> Additionally, I was said weeks ago on this NG that and signed overflow in D is not actually Undefined Behaviour.

Interesting.. The reference is fairly terse on exactly what happens, is it more formally specified anywhere? In which case Elder's objection melts away for D and becomes explicitly "prefer signed for D, but don't mix".

> For integral operands, the *, /, and % correspond to multiply, divide, and modulus operations. For multiply, overflows are ignored and simply chopped to fit into the integral type.