Re: Sign conventions for remainder

From: Keith A. Lewis (lewis_at_PROBE.mitre.org)
Date: 06/17/04


Date: Thu, 17 Jun 2004 18:09:26 +0000 (UTC)

mstemper@siemens-emis.com (Michael Stemper) writes in article <200406171732.i5HHWRG37280@mickey.empros.com> dated Thu, 17 Jun 2004 12:32:27 -0500:
>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.
>
>If the divisor and the dividend are both positive, I know that my result
>should be d = n*q+r, with 0<=r<n. What if one of them is negative? Should
>the remainder still be in that range? Should it be in the negative of that
>range? What about if both the divisor and the dividend are negative?

My opinion: The remainder is a piece of the dividend; therefore it should
have the same sign, regardless of the sign of the divisor.

This has the result of always rounding your quotient toward 0 rather than
away, which seems intuitive. You wouldn't want -1/100 = -1 rem. 99, would
you?

--Keith Lewis klewis {at} mitre.org
The above may not (yet) represent the opinions of my employer.



Relevant Pages

  • Re: Dividing 512 bit number by 128 bit number in C program
    ... calculating the quotient and the remainder before returning. ... In one of the inner loops, the divisor is positioned so that its most ... A bit-wise subtraction of divisor from dividend is performed ... bool borrow = false; ...
    (sci.crypt)
  • Re: Sign conventions for remainder
    ... What about if both the divisor and the dividend are negative? ... > If the divisor is positive, the remainder should still be in the range ... options in your package for the several possible sensible behaviours. ...
    (sci.math)
  • [SUMMARY] Long Division (#180)
    ... is done repeatedly dividing the divisor into each digit of the ... dividend combined with the remainder of the previous division step. ... And so our quotient is 0372, usually written without leading zeros as ...
    (comp.lang.ruby)
  • Re: FORMULA THAT CALCULATES HH:MM ON THIRD SHIFT(BETWEEN TWO DAYS)
    ... it's damn handy for figuring the difference between two times spanning ... The result has the same sign as divisor", ... If 11 is divided by 3, the remainder is 2. ... when both divisor and dividend are positive, 2. ...
    (microsoft.public.excel.misc)
  • Re: relationship between divisor and dividend in CRC codes
    ... between divisor and dividend in CRC codes ... Dividend and divisor are ... The remainder is the polynomial, ...
    (sci.crypt)