Warning flags for unsigned operations (unsafe)
Luca Masini
luca.masini@t-online.de
Wed Sep 22 20:50:00 GMT 2004
On Wed, 22 Sep 2004 14:36:36 +0100, Dave Korn <dk@artimi.com> wrote:
>> I have been googling around and I couldn't find out if
>> gcc had a warning flag for unsigned operation. For example, even the
>> linear
>> interpolation on [a,b] can be tricky to code:
>>
>> 1. c = a + t * (b - a); //unsafe
>> 2. c = (1.0 - t) * a + t * b; //safe
>>
>> Number 1 will fail when both a and b are unsigned and let say
>> b - a = -1
>> (math speaking). Is there something in gcc that could warn me
>> for this kind of operation ?
>
>
> Your code has a design flaw and is not valid. If you want to do maths
> that involves negative quantities, you HAVE to use a signed variable,
> not an unsigned one. If you want to do subtraction with unsigned
> quantities and
> have it work, you have to ensure (by a test) to always subtract the
> smaller from the larger.
I'm curious too, and interested on the original question.
That is:
GCC has the warning for that or not ?
Luca.
More information about the Gcc
mailing list