Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!mips!news.cs.indiana.edu!ux1.cso.uiuc.edu!symcom!chappell From: chappell@symcom (Glenn Chappell) Newsgroups: comp.misc Subject: Re: Signed integer division - implementation questions Message-ID: <1991Jun29.172953.20536@ux1.cso.uiuc.edu> Date: 29 Jun 91 17:29:53 GMT References: Sender: usenet@ux1.cso.uiuc.edu (News) Reply-To: chappell@math.uiuc.edu (Glenn Chappell) Organization: Math Dept., University of Illinois at Urbana/Champaign Lines: 42 In article lxa221@snulbug.mtview.ca.us (The Lab Rat) writes: >When designing an integer arithmetic package, some interesting >qustions arise. In particular, what does one do in the case of signed >division. >One way to perform the calculation could be to take the absolute values >of the divisor and dividend, perform the division as if unsigned, >correct the sign of the quotient, and match the sign of the remainder >to the sign of the dividend. Examples follow: > -11 / 4 = -2, remainder -3 > 11 / -4 = -2, remainder 3 > -11 / -4 = 2, remainder -3 > >Another way to perform the division is to divide the dividend by the >absolute value of the divisor, with result rounded down (to -infinity). >The sign of the result is then corrected, and the sign of the remainder >is always positive. Examples follow: > -11 / 4 = -3, remainder 1 > 11 / -4 = -2, remainder 3 > -11 / -4 = 3, remainder 1 > >Yet another way is to have the remainder always have the same sign as >the divisor. Examples follow: > -11 / 4 = -3, remainder 1 > 11 / -4 = -3, remainder -1 > -11 / -4 = 2, remainder -3 >Which system do you prefer to use? Do you have any good reasons why? I would prefer it if everyone would use the second system (remainder always non-negative), since it conforms most closely to the usual way mathematicians use modular arithmetic. On the other hand, many integer math systems are already in use, and sometimes a less-than-perfect standard is better than no standard at all. I just tested it out (in "C") on a Sun, an Iris and a Cray, and all three use the first method. If this is typical of most integer math systems (does anyone know if it is?), then that is the method I would recommend. GGC <><