[comp.lang.misc] MOD/DIV in standard Pascal

reagan@hiyall.enet.dec.com (John R. Reagan) (03/01/90)

There was a recent discussion about DIV and MOD in Pascal.  There was
also some comment about how the standards committee got it "wrong".
I asked someone on the Pascal committee at the time to recollect the
events leading upto the current definition of DIV/MOD.  Enjoy.

			-John Reagan, X3J9 Secretary



To:  John Reagan          From:  David Joslin          February 26, 1990

Here are my recollections of the Pascal DIV/MOD debate (late seventies or
early eighties, when we were standardizing classic Pascal).  You can pass
them on to Dave Jones & Charles Lindsey (I seem to remember that name) if
you wish.

1. Jensen & Wirth (at the time - it's probably different now, after being
   edited by Jim Miner) did not consider or define DIV & MOD for anything
   other than positive operands;  but it did call DIV "division with
   truncation" and "divide and truncate (i.e value is not rounded)".

2. The original working drafts defined DIV & MOD only for positive divisors
   and non-negative dividends - anything else was an error.

3. It was then considered essential to extend at least DIV to negative
   operands.  There were two schools of thought:

   GROUP A (of which I was a member) were mainly concerned with extending
           DIV.  They regarded it as essential that, for any I and J:
                 (-I) div J  =  I div (-J)  =  -(I div J)                  (i)
           and this, together with the name "division with truncation",
           inevitably led to  14 div 3 = 4 ,     (-14) div 3 = -4 ,
                              14 div (-3) = -4 , (-14) div (-3) = 4 .
           This definition was also compatible with other programming
           languages, notably Fortran, and they did not want to run the
           risk of programmer error by having the Pascal definition
           subtly different.   (It is something like blank-padding or
           non-padding string comparison:  the definition which is more
           aesthetically pleasing to some is outweighed by the compatibility
           advantages of the other.)
           They would have preferred to extend MOD by keeping
                 (I div J) * J + I mod J  =  I                            (ii)
           for any I and J;  or alternatively defining an additional
           operator REM such that, for any I and J,
                 (I div J) * J + I rem J  =  I .                         (iii)
           This would give  14 rem 3 = 2 ,    (-14) rem 3 = -2 ,
                            14 rem (-3) = 2 , (-14) rem (-3) = -2 ,
           and have satisfied, for any I and J,
                 (-I) rem J = -(I rem J) ,  I rem (-J) = I rem J          (iv)

   GROUP B were mainly concerned with MOD, and maintained that since there
           was a mathematical operator called "modulo", the Pascal MOD
           should have the same definition, to avoid confusion.
           This would give  14 mod 3 = 2 ,  (-14) mod 3 = 1 ,
                            undefined for non-positive divisors.
           If DIV were then defined via equation (ii), as Jones and Lindsey
           presumably wished, equation (i) would no longer hold (and Pascal
           would be incompatible with Fortran etc).

   THE COMPROMISE was to define DIV according to the wishes of group A (for
           the reasons stated above), and MOD according to the wishes of
           group B (for compatibility with "modulo").  This had the result
           that equation (ii) only holds for positive operands - but then,
           MOD is not "remainder", so why should (ii) hold anyway?

    A BETTER COMPROMISE would have been to define DIV & MOD as per ISO 7185,
           but also introduce the operator REM as per equation (iii);  we
           would then have had (i), (iii) & (iv) true for all operands, as
           well as (ii) true for positive operands.   However the remit of
           ISO 7185 was not to extend Wirth's Pascal unless absolutely
           necessary, so this was not done.   Someone could have suggested
           incorporating REM into Extended Pascal, but I forgot.


As to Sun-3 4.3 Pascal getting DIV & MOD wrong (i.e different from the
standard):  presumably it will not pass the Pascal Validation Suite.