comp.lang.ada
 help / color / mirror / Atom feed
From: ncohen@watson.ibm.com (Norman H. Cohen)
Subject: Re: Modulus and Remainder operations (Was Re: Help with a bit of C code)
Date: 29 Sep 1994 14:34:05 GMT
Date: 1994-09-29T14:34:05+00:00	[thread overview]
Message-ID: <36ej8t$1bsl@watnews1.watson.ibm.com> (raw)
In-Reply-To: 36bsmq$5p@gnat.cs.nyu.edu

In article <36bsmq$5p@gnat.cs.nyu.edu>, dewar@cs.nyu.edu (Robert Dewar) writes: 

|> In traditional C, the result of applying / or % to negative integers is
|> implementation dependent and hence undefined at the language level. Likely
|> it will be remainder type semantics rather than modulus type semantics
|> because most machines do things that way by default, but on the ICL1900
|> series C gave modular semantics, since that's the way the machine was.
|>
|> I think this is unchanged in the ANSI C standard, can someone confirm this?

Yes, it's implementation-defined, within certain constraints, of course.
Section 3.3.5 of the ANSI C standard contains the following paragraph
(page 47, lines 25-30): 

      When integers are divided and the division is inexact, if both
   operands are positive the result of the / operator is the largest
   integer less than the algebraic quotient and the result of the %
   operator is positive.  If either operand is negative, whether the
   result of the / operator is the largest integer less than or equal to
   the algebraic quotient or the smallest integer greater than or equal
   to the algebraic quotient is implementation-defined, as is the sign of
   the result of the % operator.  If the quotient a/b is representable,
   the expression (a/b)*b + a%b shall equal a.

(Boy, the Ada RM with its paragraph numbers sure is easier to cite, and
it's nice that the citations are not bound by page number and line
number to one particular printed version!)

Thus, it is implementation-defined in C whether (-11)/5 and 11/(-5) round
towards zero as in Ada, yielding -2, or towards negative infinity,
yielding -3.  More surprisingly, even though 11/5 must yield 2, it is
implementation-defined in C whether (-11)/(-5) yields 2 or 3.  The last
sentence of the paragraph in effect defines a%b to be a - (a/b)*b, the
same formula given in RM83-4.5.5(3) for a rem b, so we have: 

       | Rounding towards 0 | Rounding away from 0 |
       +------------+-------+-----------+----------+
 a | b |    a/b     |  a%b  |    a/b    |   a%b    | a rem b | a mod b |
---+---+------------+-------+-----------+----------+---------+---------+
 11|  5|     2      |   1   |      not allowed     |    1    |    1    |
---+---+------------+-------+-----------+----------+---------+---------+
-11|  5|    -2      |  -1   |     -3    |    4     |   -1    |    4    |
---+---+------------+-------+-----------+----------+---------+---------+
 11| -5|    -2      |   1   |     -3    |   -4     |    1    |   -4    |
---+---+------------+-------+-----------+----------+---------+---------+
-11| -5|     2      |  -1   |      3    |    4     |   -1    |   -1    |
---+---+------------+-------+-----------+----------+---------+---------+

When negative quotients a/b are rounded toward zero a%b = a rem b
and when negative quotients a/b are rounded towards negative infinity
a%b = a mod b.  In the case where a and b are both negative, if the
quotient is rounded towards zero we get a%b = a rem b = a mod b.  If the
quotient is rounded away from zero, we get a%b = a rem b + b =
a mod b + b.

--
Norman H. Cohen    ncohen@watson.ibm.com



  parent reply	other threads:[~1994-09-29 14:34 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-09-27 14:40 Modulus and Remainder operations (Was Re: Help with a bit of C code) David A. Cobb
1994-09-28 13:56 ` Robert Dewar
1994-09-29  9:04   ` Christopher Costello
1994-09-29 14:34   ` Norman H. Cohen [this message]
     [not found]   ` <1994Oct7.225248.6208@nosc.mil>
     [not found]     ` <1994Oct10.084630.19894@sei.cmu.edu>
     [not found]       ` <37bof4$ljl@gnat.cs.nyu.edu>
     [not found]         ` <37cigq$6e0@felix.seas.gwu.edu>
1994-10-11 14:42           ` Norman H. Cohen
     [not found]     ` <hbakerCxFK2p.4wp@netcom.com>
     [not found]       ` <1994Oct11.161048.1058@nosc.mil>
1994-10-11 20:06         ` Norman H. Cohen
1994-10-13  1:51           ` Henry G. Baker
1994-10-13  8:27             ` Magnus Kempe
1994-10-13 12:30               ` Robert Dewar
1994-10-14 15:45               ` Henry G. Baker
1994-10-14 22:11                 ` Robert Dewar
1994-10-15 17:35                 ` Tucker Taft
1994-10-13 10:38             ` Tucker Taft
1994-10-13 21:34             ` Norman H. Cohen
1994-10-14 15:39               ` Henry G. Baker
1994-10-14 22:56                 ` David Weller
1994-10-16  1:25                   ` Henry G. Baker
1994-10-13 18:13           ` Charles H. Sampson
1994-10-13 16:56             ` Robert I. Eachus
1994-10-13 20:59             ` Robert Dewar
1994-10-13 23:44             ` Bob Duff
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox