Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!apple!snulbug!lxa221 From: lxa221@snulbug.mtview.ca.us (The Lab Rat) Newsgroups: comp.misc Subject: Signed integer division - implementation questions Message-ID: Date: 29 Jun 91 06:32:00 GMT Lines: 44 When designing an integer arithmetic package, some interesting qustions arise. In particular, what does one do in the case of signed division. The case for positive / positive is easy - quotient is the truncated integer result of the division, and the remainder is the difference between the dividend and (quotient * divisor). Example follows: 11 / 4 = 2, with remainder 3 In the case of negative dividend and/or divisor, things get a lot murkier. 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 There are probably other variations, but I can't think of any that make sense. Which system do you prefer to use? Do you have any good reasons why? Thanks in advance! --- The Lab Rat lxa221@snulbug.mtview.ca.us