tech-toolchain archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: minus operator applied to unsigned type



> My compiler is confused. What does this mean? 

>       d_val = str[0] == '-' ? -(double)-l_val : (double)l_val;

> I suppose:
> d_val = (str[0] == '-') ? (double)(0-l_val) : (double)l_val;
> or maybe if not in __LP64__
> d_val = (str[0] == '-') ? (double)(0-(double)l_val) : (double)l_val;

Neither.

Parenthesizing, it becomes

        d_val = ((str[0] == '-') ? (-(double)-l_val) : ((double)l_val));

That is, if str[0] is '-', then take l_val, negate it, convert to
double, negate the resulting double, and store that in d_val;
otherwise, just take l_val, convert to double, and store in d_val.

Your expansions are correct except that they negate only once.
Converting -l_val to (0-l_val) is correct, but failing to negate again
after converting to double is not.

__LP64__ has nothing to do wth this.  That affects (describes, really)
pointer and some integral types.  Whether l_val is 32 or 64 bits long
affects, at most, how it's converted to double.

I'm not sure why the -(double)-l_val construct.  I suspect the intent
is to convert an unsigned long holding a signed long's bit pattern to a
double with the same value as the signed long.  If so, I can't see why
not (double)(long)l_val; indeed, I think -(double)-l_val equals
(double)(long)l_val except when l_val holds the (bit pattern of the)
most negative long - or if signed longs don't use two's complement, I
suppose, which it seems to me it is unlikely the code is designed to
accommodate.

> There are several of these (for my compiler ambiguous) constructs in
> bmake.

You might want to get rid of it and find a C compiler, then; there is
nothing ambiguous about these constructs in C.

> I solved them just by adding a zero - hope that's what was originally
> meant.

In C, for unsigned long l_val, -l_val is by definition equal to
0-l_val.  (Not necessarily textually, because of operator precedence
rules, but semantically.)

/~\ The ASCII                             Mouse
\ / Ribbon Campaign
 X  Against HTML                mouse%rodents-montreal.org@localhost
/ \ Email!           7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index