mod, div (was Re: [mercury-users] The Mercury random module

Fergus Henderson fjh at cs.mu.oz.au
Wed Aug 6 21:16:35 AEST 1997


Peter Moulder, you wrote:
> Fergus Henderson <fjh at cs.mu.OZ.AU> writes:
> 
> > The semantics of `mod' have been tightened up (it always
> > returns a non-negative number), and there are new functions `div' and
> > `rem'.  With this change, you can now choose between `//' (round-to-zero)
> > and `div' (round-to-minus-infinity).  You call also choose between the
> > corresponding remainders: `rem' is remainder with respect to `//', and
> > `mod' is remainder with respect to `div'.
> 
> I join with the others in warmly welcoming this change.  (See rant below.)
> 
> I'd just point out mod should give a negative answer if the second
> argument is negative.

You are correct.  The revised Mercury `mod' does exactly that.
My summary above was misleading.

> I don't know why GNU chose to *guarantee* the wrong behaviour wrt `%'
> and `/' in C.

Perhaps because that that behaviour is what most machines
implement in hardware, which in turn is because that behaviour
for division is what was specified by the FORTRAN (66?) standard.
This behaviour is also going to be enshrined in C9X, the next revision
of the C standard.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the users mailing list