tech-toolchain archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
Re: minus operator applied to unsigned type
On Mon, Nov 29, 2010 at 23:32:37 -0500, der Mouse wrote:
> 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.
That l_val comes from stroul, that returns unsinged(!) long. It
returns negation of the input number if the number has leading '-'
sign.
l_val is unsinged long, so the type of unary minus applied to it is
also unsinged long. For unsinged types the modulo arithmetic is
defined by the standard (unlike for singed!). The inner unary minus
undoes negation applied by stroul and gives us an unsinged long that
has correct absolute value (but, obviously, wrong sign). The outer
unary minus operates on the singed, positive double and gives the
desired result - negative double.
-uwe
Home |
Main Index |
Thread Index |
Old Index