From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.7 required=5.0 tests=BAYES_00,INVALID_DATE, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,18069d15345a10c8 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1994-09-29 17:46:55 PST Path: bga.com!news.sprintlink.net!howland.reston.ans.net!swiss.ans.net!newsgate.watson.ibm.com!watnews.watson.ibm.com!ncohen From: ncohen@watson.ibm.com (Norman H. Cohen) Newsgroups: comp.lang.ada Subject: Re: Modulus and Remainder operations (Was Re: Help with a bit of C code) Date: 29 Sep 1994 14:34:05 GMT Organization: IBM T.J. Watson Research Center Distribution: world Message-ID: <36ej8t$1bsl@watnews1.watson.ibm.com> References: <940927104030.20400d35@v22bz.npt.nuwc.navy.mil> <36bsmq$5p@gnat.cs.nyu.edu> Reply-To: ncohen@watson.ibm.com NNTP-Posting-Host: rios8.watson.ibm.com Date: 1994-09-29T14:34:05+00:00 List-Id: 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