Re: Sign conventions for remainder

From: mike.staniforth (mike.staniforth_at_btinternet.com)
Date: 06/18/04


Date: Fri, 18 Jun 2004 23:34:49 +0100


"Michael Stemper" <mstemper@siemens-emis.com> wrote in message
news:200406171732.i5HHWRG37280@mickey.empros.com...
> Just for fun, I've been implementing a package to perform arithmetic on
> integers of arbitrary size. Addition, subtraction, and multiplication
> were all pretty straight-forward. (Subtraction was the easiest!) But,
> when I prepared to implement division, I realized that I'm not aware of
> the conventions for remainders.
>
There is no universally agreed convention. I use Visual Basic in Excel for a
lot of my mathematical programming, and Microsoft manage to use two
different conventions in the same package. Visual Basic has an integer
divide function and a mod (remainder) function. The results obtained for
different sign combinations are:
39/5 = 7 rem 4
39/(-5) = -7 rem 4
(-39)/5 = -7 rem -4
(-39)/(-5) = 7 rem -4
Excel also has a MOD function. This returns the following results:
39/5 = x rem 4
39/(-5) = x rem -1
(-39)/5 = x rem -1
(-39)/(-5) = x rem -4
Excel does not have an integer divide function, so the value of x in the
above list cannot be generated directly. Instead, it is necessary to perform
the division using normal floating point division (giving the answer 7.8
or -7.8 as appropriate) and then convert it to an integer using either the
INT or TRUNC function. INT rounds to the integer below, so INT(-7.8) = -8,
whereas TRUNC discards the fractional part, so TRUNC(-7.8) = -7. It is
necessary to choose the INT function for consistency with the results
returned by MOD (eg (-39)/5 must be -8 rem 1).



Relevant Pages

  • Re: Calling hot shot coders!
    ... > int a,modulus; ... if (rem>= divisor) ... rem = dividend - q*divisor; ...
    (sci.crypt)
  • Calling hot shot coders!
    ... Here are two routines that compute a modular inverse of a single precision ... int a,modulus; ... rem = modulus - a*q; ... register int n1,n2, preg, m1, m2; ...
    (sci.crypt)
  • Re: Portversion question
    ... Rem P Roberti writes: ... > This morning I ran portversion -l and found that there was a package ... > portupgrade picked up the non-current package. ...
    (freebsd-questions)
  • Re: Skype permissions (kdump output)
    ... Rem P Roberti writes: ... Well, Boris just emailed me about that, but I'm having a bit of a problem. ... But if I try to install it as a package pkg_add tells me the package ...
    (freebsd-questions)

Loading