Path: utzoo!censor!geac!torsqnt!lethe!yunexus!ists!helios.physics.utoronto.ca!news-server.csri.toronto.edu!cs.utexas.edu!helios!bcm!convex!linac!att!ucbvax!MITCH.ENG.SUN.COM!wmb From: wmb@MITCH.ENG.SUN.COM (Mitch Bradley) Newsgroups: comp.lang.forth Subject: Modulus Message-ID: <9102282010.AA14897@ucbvax.Berkeley.EDU> Date: 27 Feb 91 23:46:01 GMT Sender: daemon@ucbvax.BERKELEY.EDU Reply-To: Mitch Bradley Organization: The Internet Lines: 45 > This is an emprical proof of the fact that the forms of division used in > most of the current computer languages (Including the two used in the > BASIS...) are incorrect in the results given for the remainder for > operations in the third quadrant (both divisor and dividend are > negative). ... > Consider, > -4 > -4 -3 /MOD is equivalent to -- in fractional expression terms. > -3 Sorry, this isn't correct. The mathematical definition of signed integer division with remainder is the following equation holds: quotient*divisor + remainder = dividend There are several ways of arranging the signs to make this work out correctly; the mathematical "mod" function arranges the signs so that that, if the remainder is nonzero, its sign is the same as the divisor's. Reference: Knuth "The Art of Computer Programming", Vol. 1. There are other choices, but the Forth-83 choice is definitely mathematically correct, and there is considerable evidence to suggest that it is the best choice for many if not most numerical applications. Robert Berkey has written extensively on this topic, and rather than recap his arguments, I refer interested readers to the literature. Following up on the example presented, let's recast it in fractional form: remainder dividend quotient + --------- = -------- divisor divisor -1 -4 1 + --------- = -------- -3 -3 Thus we see that the error in the original argument stems from the fact that, which the signs of the dividend and divisor cancel out, the fractional representation implies that the remainder is also divided by the divisor. In order for the divisor's sign to be canceled in the remainder term, the remainder must be negative. Mitch