[comp.lang.modula2] perpetual calendar

ags@j.cc.purdue.edu (Dave Seaman) (02/12/88)

In article <8802101656.AA08424@decwrl.dec.com> fulton@navion.dec.com (10-Feb-1988 0957) writes:

 [ description of perpetual calendar formula omitted ]

>        I have been intrigued by the above formula, and I coded it up in
>        Logitech Modula-2 to try it out.  It appears that there are some
>        cases where it doesn't work quite right.  For instance:

 [ example leading to negative numbers omitted ]

>        Then you have -1 MOD 7  =>  -1,  which does not equate to any of
>        the numbers assigned to Sunday thru Saturday, namely 0 thru 6. 

 [ illustration of adding 7 to make result positive ]
 
>        I guess that  this  is  an  after-the-fact  kludge;  perhaps the
>        original formula could be  changed  so  that  the result doesn't
>        require "fixing" if it comes out negative.

I don't have my copy of the Modula-2 standard available at the moment, but
I believe that the definition of the MOD operator is similar to Pascal,
where the standard says:

	A term of the form i mod j shall be an error if j is zero or
	negative, otherwise the value of i mod j shall be that value of
	(i-(k*j)) for integral k such that 0 <= i mod j < j.

Therefore the correct value of (-1 MOD 7) is 6, not -1.  In fact, I believe
that in Modula-2 the result of the MOD operator is type CARDINAL and
therefore cannot be negative.  It sounds as if you may be stuck with a
flawed implementation of Modula-2.

-- 
Dave Seaman	  					
ags@j.cc.purdue.edu

firth@sei.cmu.edu (Robert Firth) (02/12/88)

In article <6413@j.cc.purdue.edu] ags@j.cc.purdue.edu.UUCP (Dave Seaman) writes:

]I don't have my copy of the Modula-2 standard available at the moment, but
]I believe that the definition of the MOD operator is similar to Pascal,
]where the standard says:
]
]	A term of the form i mod j shall be an error if j is zero or
]	negative, otherwise the value of i mod j shall be that value of
]	(i-(k*j)) for integral k such that 0 <= i mod j < j.
]
]Therefore the correct value of (-1 MOD 7) is 6, not -1.  In fact, I believe
]that in Modula-2 the result of the MOD operator is type CARDINAL and
]therefore cannot be negative.  It sounds as if you may be stuck with a
]flawed implementation of Modula-2.

Well, I DO have my copy of the reference document [PIM-2, 3rd ed], and it
says no such thing.

Section 8.2 says, in particular

(a) the result of MOD is CARDINAL iff both operands are CARDINAL, and
    otherwise is INTEGER

(b) x MOD y is the remainder after DIV, ie y*(x DIV y) + x MOD y = y

(c) x DIV y is equal to the truncated quotient of x/y

Therefore, the correct value of -1 MOD 7, at least according to Wirth, is -1.