BufferedFile bug?
Oskar Linde
oskar.lindeREM at OVEgmail.com
Thu Mar 6 06:37:38 PST 2008
Janice Caron wrote:
> On 06/03/2008, Oskar Linde <oskar.lindeREM at ovegmail.com> wrote:
>> The problem is that:
>>
>> uint x = 1;
>> long y = -x;
>>
>> yields y == 4294967295
>
> (I changed the variable names because lowercase L looks like one to me).
>
> Huh?
>
> Why doesn't y equal minus one?
>
> I wouldn't call that a bug in BufferedStream.flush(), I'd call it a
> bug in D's arithmetic generally.
It is hard to classify something as a bug when it behaves exactly as
specified. See "Usual Arithmetic Conversion" under:
http://www.digitalmars.com/d/1.0/type.html
This is exactly how it works in C too.
> The way I see it, there are two possible fixes:
>
> (1) Disallow unary minus completely for all unsigned types. Thus
Unary minus is not the only affected operator. All integer arithmetic is
affected.
> (2), when unary minus is applied to an unsigned type, promote ubyte to
> short, ushort to int, uint to long, ulong to cent (...best make that
> one illegal for now). That would mean
>
> uint x = 1;
> long y = -x; /* OK */
>
> but
>
> uint x = 1;
> int y = -x; /* Error */
>
> Either would work, but the status quo /can't/ be right!!!???
There has been some previous discussions along those lines. For example:
http://www.digitalmars.com/d/archives/digitalmars/D/unsigned_policy_47929.html#N47954
Today, all arithmetic operations are promoted to int/uint, and by the
magic of the 2's-complement representation, things actually work out
most of the time.
for example:
int a = (rand() % 3) - 1;
works fine, while
double b = (rand() % 3) - 1;
doesn't.
--
Oskar
More information about the Digitalmars-d
mailing list