'int' is enough for 'length' to migrate code from x86 to x64
Marco Leise via Digitalmars-d
digitalmars-d at puremagic.com
Fri Nov 21 13:55:10 PST 2014
Am Wed, 19 Nov 2014 18:20:24 +0000
schrieb "Marc Schütz" <schuetzm at gmx.net>:
> I'd say length being unsigned is fine. The real mistake is that
> the difference between two unsigned values isn't signed, which
> would be the most "correct" behaviour.
Now take my position where I explicitly write code relying
on the fact that `bigger - smaller` yields correct results.
uint bigger = uint.max;
uint smaller = 2;
if (bigger > smaller)
{
auto added = bigger - smaller;
// Now 'added' is an int with the value -3 !
}
else
{
auto removed = smaller - bigger;
}
In fact checking which value is larger is the only way to
handle the full result range of subtracting two machine
integers which is ~2 times larger than what the original type
can handle:
T.min - T.max .. T.max - T.min
This is one reason why I'd like to just keep working with
the original unsigned type, but split the range around the
positive/negative pivot with an if-else.
Implicit conversion of unsigned subtractions to signed values
would make the above code unnecessarily hard.
> Let people cast the result
> if they want wrapping (or better, use a helper function to
> document the intentiion).
--
Marco
More information about the Digitalmars-d
mailing list