To: Kimberley Burchett Cc: DJGPP Mailing List Subject: Re: -1441992/4 = 1073381326 Date: Sun, 27 Nov 1994 16:01:09 EST From: David Goldschmidt >>>>> "Kimberley" == Kimberley Burchett writes: Kimberley> This program: Kimberley> int main() { Kimberley> long x = -1441992; unsigned y = 4; Kimberley> printf("%d / %d = %d\n",x,y,x/y); } Kimberley> Spits this out: Kimberley> -1441992 / 4 = 1073381326 Kimberley> The division is done at compile time if I compile with Kimberley> optimizations on. A more complex program where the division Kimberley> can't be done at compile time gets the same result doing the Kimberley> division at run time. Can anyone tell me why? As far as I Kimberley> know, even in the strange world of CPU math, overflows can't Kimberley> account for this result. The only thing I can think of is that Kimberley> the compiler is using DIV instead of IDIV (however, in this Kimberley> case, dividing by 4, it shouldn't use any DIV). The funny math Kimberley> only shows up when x is negative (never tried y negative...). Kimberley> It doesn't matter whether I'm using the C or the C++ compiler. Your problem is that the negative long is being "promoted" to an unsigned long because you called the divisor unsigned. When you promote a negative long to unsigned, you get a number between 2^31 and 2^32. If you change "unsigned" to "int" it works correctly. David Goldschmidt