www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1994/11/25/20:44:39

Date: Fri, 25 Nov 1994 19:16:07 -0500 (EST)
From: Kimberley Burchett <OKRA AT max DOT tiac DOT net>
Subject: -1441992/4 = 1073381326
To: DJGPP Mailing List <djgpp AT sun DOT soe DOT clarkson DOT edu>

  This program:
 
int main() {
 
  long x = -1441992;
  unsigned y = 4;
 
  printf("%d / %d = %d\n",x,y,x/y);
}
 
  Spits this out:

-1441992 / 4 = 1073381326


  The division is done at compile time if I compile with optimizations on. 
A more complex program where the division can't be done at compile time
gets the same result doing the division at run time.  Can anyone tell me
why?  As far as I know, even in the strange world of CPU math, overflows
can't account for this result.  The only thing I can think of is that the
compiler is using DIV instead of IDIV (however, in this case, dividing by
4, it shouldn't use any DIV).  The funny math only shows up when x is
negative (never tried y negative...).  It doesn't matter whether I'm using
the C or the C++ compiler.
  For Those Who Care: I found this little glitch when for some reason a
program I was trying to write kept making little "birds" zip off the
screen - I was using this to calculate delta x's and y's.  Two or three 
hours of debugging and now I'm a lot more comfortable with gdb.
							Kimberley

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019