'int' is enough for 'length' to migrate code from x86 to x64

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Thu Nov 20 01:47:56 PST 2014


On 11/20/14 12:18 AM, Don wrote:
> On Wednesday, 19 November 2014 at 17:55:26 UTC, Andrei Alexandrescu wrote:
>> On 11/19/14 6:04 AM, Don wrote:
>>> Almost everybody seems to think that unsigned means positive. It does
>>> not.
>>
>> That's an exaggeration. With only a bit of care one can use D's
>> unsigned types for positive numbers. Please let's not reduce the
>> matter to black and white.
>>
>> Andrei
>
> Even in the responses in this thread indicate that about half of the
> people here don't understand unsigned.
>
> "unsigned" means "I want to use modulo 2^^n arithmetic". It does not
> mean, "this is an integer which cannot be negative".
>
> Using modulo 2^^n arithmetic is *weird*. If you are using uint/ulong to
> represent a non-negative integer, you are using the incorrect type.
>
>> "With only a bit of care one can use D's unsigned types for positive
>> numbers."
>
> I do not believe that that statement to be true. I believe that bugs
> caused by unsigned calculations are subtle and require an extraordinary
> level of diligence. I showed an example at DConf, that I had found in
> production code.
>
> It's particularly challenging in D because of the widespread use of 'auto':
>
> auto x = foo();
> auto y = bar();
> auto z = baz();
>
> if (x - y > z) { ... }
>
>
> This might be a bug, if one of these functions returns an unsigned
> type.  Good luck finding that. Note that if all functions return
> unsigned, there isn't even any signed-unsigned mismatch.
>
> I believe the correct statement, is "With only a bit of care one can use
> D's unsigned types for positive numbers and believe that one's code is
> correct, even though it contains subtle bugs."

Well I'm sorry but I quite disagree. -- Andrei



More information about the Digitalmars-d mailing list