At 12:40 PM +1000 2000/02/18, Stephen Best wrote:
> > Should the naked constant 10 be treated as signed or unsigned? Short or
> > long integer? Those are the silent promotions in the above expression,
> > and in the original example.
>
>Ok, what about:
>
> result = x +
> (unsigned long) ((signed long) (((signed long) (y - z)) * 10L));
>
>which (still!) gives the same result?
This issue has nothing to do with the size of the variable -- we're talking about an
undefined behavior between various compilers for sign promotion in mixed-sign
expressions. In this and all previous examples 10 and 10L are promoted to unsigned
integers, which I believe is the reason the compiler is incorrectly generating an
unsigned multiply opcode. It just doesn't know what to do.
Should the compiler be smarter about this? Of course. But is it always proper to
promote an unsigned integer to a signed integer? Definitely not.
A defensive C programmer should always be more explicit when mixing signed and
unsigned arithmetic. For example:
unsigned long test (unsigned long x, signed short y, signed short z)
{
signed long temp = (y - z) * 10;
if (temp < 0)
return ???; /* the result is technically undefined -- what do you want to return
in this case? */
else
return x + temp;
}
Regards,
Jim Schram
3Com/Palm Computing
Partner Engineering
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palm.com/devzone/mailinglists.html