This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Warning flags for unsigned operations (unsafe)


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]