[Bug middle-end/22072] bizarre code for int*int/2

pluto at agmk dot net gcc-bugzilla@gcc.gnu.org
Fri Jun 17 09:33:00 GMT 2005


------- Additional Comments From pluto at agmk dot net  2005-06-17 09:33 -------
(In reply to comment #9)
> (In reply to comment #8)
> > did i miss something in my math? 
> 
> Yes in C, % (remainder and not mod) is negative iff one of the operands is
negative.
> so it is -2*2 + -1 = -5. , integer divide in C is truncated.

[ cite ]

"The New ISO Standard for C (C9X)"

25).
The integer division and modulus operators are defined to perform
truncation *towards zero*. (In C89, it was implementation-defined whether
truncation was done towards zero or -infinity. This is (obviously)
important only if one or both operands are negative. Consider:

-22 / 7 = -3     	truncation towards zero
-22 % 7 = -1

-22 / 7 = -4    	truncation towards -infinity
-22 % 7 =  6

Both satisfy the required equation (a/b)*b + a%b == a.
The second has the advantage that the modulus is always positive
-- but they decided on the other (more Fortran-like, less Pascal-like) variant...)

[ /cite ]

so it's all clear now.


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=22072



More information about the Gcc-bugs mailing list