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!EUTWS1.WIN.TUE.NL!wsbusup From: wsbusup@EUTWS1.WIN.TUE.NL (Jan Stout) Newsgroups: comp.lang.forth Subject: division donuts Message-ID: <9102282011.AA14953@ucbvax.Berkeley.EDU> Date: 28 Feb 91 19:53:43 GMT Sender: daemon@ucbvax.BERKELEY.EDU Reply-To: Jan Stout Organization: The Internet Lines: 84 FE>From: "Frank C. Earl" FE>Subject: Division... Is both types of division in BASIS arithmatically FE> wrong??? FE>To: Multiple recipients of list FIGI-L FE> FE>To the net: FE> FE>This is an emprical proof of the fact that the forms of division used in FE>most of the current computer languages (Including the two used in the FE>BASIS...) are incorrect in the results given for the remainder for FE>operations in the third quadrant (both divisor and dividend are FE>negative). This is in response to a comment made to me by Mitch FE>Bradley over a statement that I made about this being a problem in both FE>types of division. FE> FE> FE>Passed to /MOD is the values minus four and minus three- FE> FE>Results: FE> FE>F-PC 1 -1 FE>MVP 1 -1 FE>TaskForth 1 -1 FE> FE>So, you might ask, what is wrong with this? FE> FE>The remainder is NEGATIVE. FE>This is incorrect, as you will soon see... FE> FE> FE>Consider, FE> -4 FE>-4 -3 /MOD is equivalent to -- in fractional expression terms. FE> -3 FE> FE> -4 -1 4 -1 FE>Given this, -- = -- * - . The -- = 1 because the signs cancel. FE> -3 -1 3 -1 FE> FE> 4 1 FE>Therefore, our calculation is really with - , which is 1 + - . FE> 3 3 FE> FE>This is what /MOD passes back as a result - the whole number resulting FE>from the division and the dividend of the fraction that's left over. FE> -1 FE>But, every Forth dialect that I have asked, tells me that it's 1 + -- , FE> 3 FE>which, from what I just said is *quite* wrong... (Not only is Forth off FE>here, but every language dialect I've tried does the same thing (ADA's the FE>only one I know of that does the right calculation for the remainder FE>portion)). If you think that this doesn't matter- think again... FE> FE> 1 4 -1 3 1 2 4 2 FE>1 + - = - and, 1 + -- = - - - = - and, - does not equal - . FE> 3 3 3 3 3 3 3 3 FE> FE> FE>That's sufficient enough an error to justify re-working it (What if you FE>don't check to see that this throws off your calculations- it could FE>prove catastrophic- might cause a horrible accident of some sort if it FE>was used in a critical application that only very rarely drifted off FE>into the third quadrant??) and looking at ways of doing it better... FE> FE>The next posting from me (time permitting) will be a description of the FE>algorithm I propose should be considered for standardization (I know it's FE>too late- but we could start considering it for the next standard...) and FE>some sample Forth-83 (F-PC) code that does the desired operation... FE> FE>Frank Sorry Frank, but in my opinion you're misinterpreting the meaning of the modulo result. My schoolbook tells me the definition of division is such that x r - = q + - y y because x = q * y + r , in your case -4 = 1 * -3 + -1, which is perfectly valid. Note that in the above equation the last term r / y is dependant on y, so it becomes quite questionable wether your step to remove the minus signs is justifiable... Jan Stout, wsbusup@eutws1.win.tue.nl