question regarding % operator.

Discussion in 'C Programming' started by somenath, Mar 12, 2012.

  1. somenath

    somenath Guest

    Hello All,

    In the section 2.5 Arithmetic Operator of K&R2 says that "... the
    sign of result for % are machine-dependent for negative operand, as
    the action taken on overflow or underflow."

    I have failed to understand why the sign of -2%3 or -2%-3 or 2%-3 has
    to be machine dependent?
    Could you please explain how the % operator behaves in C ? I
    understand that in all the above case the result will be 2 if both
    the number is positive. In case of negative number how it should work?

    Please provide some inputs.
     
    somenath, Mar 12, 2012
    #1
    1. Advertising

  2. somenath

    Eric Sosman Guest

    On 3/11/2012 11:17 PM, somenath wrote:
    > Hello All,
    >
    > In the section 2.5 Arithmetic Operator of K&R2 says that "... the
    > sign of result for % are machine-dependent for negative operand, as
    > the action taken on overflow or underflow."
    >
    > I have failed to understand why the sign of -2%3 or -2%-3 or 2%-3 has
    > to be machine dependent?
    > Could you please explain how the % operator behaves in C ? I
    > understand that in all the above case the result will be 2 if both
    > the number is positive. In case of negative number how it should work?
    >
    > Please provide some inputs.


    K&R II is based on the original ANSI C Standard of two decades
    ago, and there have been some changes since. But let's begin at
    the beginning ...

    First, % is often called the "modulo" operator, but it might
    have been better to call it the "remainder" operator. When an
    integer division is inexact there is a non-zero remainder, and
    this is the quantity % delivers. 6 / 3 is 2, exactly, so 6 % 3
    is zero. 7 / 3 is 2, INexactly, so 7 % 3 is 1. For positive N,
    Q = N / 3 is the quotient (exact or inexact) and R = N % 3 is the
    amount of N "left over," not accounted for in the quotient. You
    can always recover N from Q and R: N = Q * 3 + R.

    But what if N is negative? If the division is exact there's
    no problem: -6 / 3 is -2, exactly, so the remainder -6 % 3 is zero.
    But -7 / 3 could (under ANSI C rules) be either -2 ("round toward
    zero") or -3 ("round toward minus infinity"), and implementations
    were allowed to produce either result. Whichever they chose to
    deliver, though, they had to make N = Q * 3 + R work correctly.
    So if -7 / 3 gave Q = -2, then -7 % 3 had to deliver R = -1. If
    -7 / 3 yielded Q = -3, then -7 % 3 had to produce R = +2. Similar
    considerations apply for 7 / -3 and 7 % -3: The implementation had
    a choice of two possible quotients, but had to deliver a remainder
    that made 7 = Q * -3 + R work.

    The original ANSI C Standard has been revised several times,
    and the C99 revision tightened the rules a bit. Specifically, it
    took away the implementation's choice of which quotient to produce:
    Division must now "round toward zero," always. Nowadays, -7 / 3
    must produce -2 (never -3), so the remainder must be -7 % 3 = -1,
    (never +2). As before, N = Q * 3 + R always holds -- but now there's
    no choice about what Q and R must be.


    --
    Eric Sosman
    d
     
    Eric Sosman, Mar 12, 2012
    #2
    1. Advertising

  3. somenath

    somenath Guest

    On Mar 12, 5:23 pm, Eric Sosman <> wrote:
    > On 3/11/2012 11:17 PM, somenath wrote:
    >
    > > Hello All,

    >
    > > In the  section 2.5 Arithmetic Operator  of K&R2  says that "... the
    > > sign of result for % are machine-dependent for negative operand, as
    > > the action taken on overflow or underflow."

    >
    > > I have failed to understand why the sign of -2%3 or -2%-3 or 2%-3 has
    > > to be machine dependent?
    > >   Could you please explain how the % operator behaves in C ? I
    > > understand that in all the above case the result will be  2 if both
    > > the number is positive. In case of negative number how it should work?

    >
    > > Please provide some inputs.

    >
    >      K&R II is based on the original ANSI C Standard of two decades
    > ago, and there have been some changes since.  But let's begin at
    > the beginning ...
    >
    >      First, % is often called the "modulo" operator, but it might
    > have been better to call it the "remainder" operator.  When an
    > integer division is inexact there is a non-zero remainder, and
    > this is the quantity % delivers.  6 / 3 is 2, exactly, so 6 % 3
    > is zero. 7 / 3 is 2, INexactly, so 7 % 3 is 1.  For positive N,
    > Q = N / 3 is the quotient (exact or inexact) and R = N % 3 is the
    > amount of N "left over," not accounted for in the quotient.  You
    > can always recover N from Q and R: N = Q * 3 + R.
    >
    >      But what if N is negative?  If the division is exact there's
    > no problem: -6 / 3 is -2, exactly, so the remainder -6 % 3 is zero.
    > But -7 / 3 could (under ANSI C rules) be either -2 ("round toward
    > zero") or -3 ("round toward minus infinity"), and implementations
    > were allowed to produce either result.  Whichever they chose to
    > deliver, though, they had to make N = Q * 3 + R work correctly.
    > So if -7 / 3 gave Q = -2, then -7 % 3 had to deliver R = -1.  If
    > -7 / 3 yielded Q = -3, then -7 % 3 had to produce R = +2.  Similar
    > considerations apply for 7 / -3 and 7 % -3: The implementation had
    > a choice of two possible quotients, but had to deliver a remainder
    > that made 7 = Q * -3 + R work.
    >
    >      The original ANSI C Standard has been revised several times,
    > and the C99 revision tightened the rules a bit.  Specifically, it
    > took away the implementation's choice of which quotient to produce:
    > Division must now "round toward zero," always.  Nowadays, -7 / 3
    > must produce -2 (never -3), so the remainder must be -7 % 3 = -1,
    > (never +2).  As before, N = Q * 3 + R always holds -- but now there's
    > no choice about what Q and R must be.


    Many thanks for the great response.
    So in case of 7/3 there also exist choices. It can be either 2 or 3.
    But in C it is 2. So does it mean the choice was made to round toward
    zero?
     
    somenath, Mar 12, 2012
    #3
  4. somenath

    James Kuyper Guest

    On 03/12/2012 10:02 AM, somenath wrote:
    > On Mar 12, 5:23 pm, Eric Sosman <> wrote:

    ....
    >> Division must now "round toward zero," always. Nowadays, -7 / 3

    ....
    > So in case of 7/3 there also exist choices. It can be either 2 or 3.
    > But in C it is 2. So does it mean the choice was made to round toward
    > zero?


    Well, yes - that is precisely what he said.
     
    James Kuyper, Mar 12, 2012
    #4
  5. somenath

    Fred K Guest

    On Monday, March 12, 2012 7:15:48 AM UTC-7, James Kuyper wrote:
    > On 03/12/2012 10:02 AM, somenath wrote:
    > > On Mar 12, 5:23 pm, Eric Sosman <> wrote:

    > ...
    > >> Division must now "round toward zero," always. Nowadays, -7 / 3

    > ...
    > > So in case of 7/3 there also exist choices. It can be either 2 or 3.
    > > But in C it is 2. So does it mean the choice was made to round toward
    > > zero?

    >
    > Well, yes - that is precisely what he said.


    Not necessarily - for positive numbers, "round toward zero" and "round toward negative infinity" always produce the same result.


    On Monday, March 12, 2012 7:15:48 AM UTC-7, James Kuyper wrote:
    > On 03/12/2012 10:02 AM, somenath wrote:
    > > On Mar 12, 5:23 pm, Eric Sosman <> wrote:

    > ...
    > >> Division must now "round toward zero," always. Nowadays, -7 / 3

    > ...
    > > So in case of 7/3 there also exist choices. It can be either 2 or 3.
    > > But in C it is 2. So does it mean the choice was made to round toward
    > > zero?

    >
    > Well, yes - that is precisely what he said.




    On Monday, March 12, 2012 7:15:48 AM UTC-7, James Kuyper wrote:
    > On 03/12/2012 10:02 AM, somenath wrote:
    > > On Mar 12, 5:23 pm, Eric Sosman <> wrote:

    > ...
    > >> Division must now "round toward zero," always. Nowadays, -7 / 3

    > ...
    > > So in case of 7/3 there also exist choices. It can be either 2 or 3.
    > > But in C it is 2. So does it mean the choice was made to round toward
    > > zero?

    >
    > Well, yes - that is precisely what he said.




    On Monday, March 12, 2012 7:15:48 AM UTC-7, James Kuyper wrote:
    > On 03/12/2012 10:02 AM, somenath wrote:
    > > On Mar 12, 5:23 pm, Eric Sosman <> wrote:

    > ...
    > >> Division must now "round toward zero," always. Nowadays, -7 / 3

    > ...
    > > So in case of 7/3 there also exist choices. It can be either 2 or 3.
    > > But in C it is 2. So does it mean the choice was made to round toward
    > > zero?

    >
    > Well, yes - that is precisely what he said.




    On Monday, March 12, 2012 7:15:48 AM UTC-7, James Kuyper wrote:
    > On 03/12/2012 10:02 AM, somenath wrote:
    > > On Mar 12, 5:23 pm, Eric Sosman <> wrote:

    > ...
    > >> Division must now "round toward zero," always. Nowadays, -7 / 3

    > ...
    > > So in case of 7/3 there also exist choices. It can be either 2 or 3.
    > > But in C it is 2. So does it mean the choice was made to round toward
    > > zero?

    >
    > Well, yes - that is precisely what he said.
     
    Fred K, Mar 12, 2012
    #5
  6. somenath

    Fred K Guest

    On Monday, March 12, 2012 7:15:48 AM UTC-7, James Kuyper wrote:
    > On 03/12/2012 10:02 AM, somenath wrote:
    > > On Mar 12, 5:23 pm, Eric Sosman <> wrote:

    > ...
    > >> Division must now "round toward zero," always. Nowadays, -7 / 3

    > ...
    > > So in case of 7/3 there also exist choices. It can be either 2 or 3.
    > > But in C it is 2. So does it mean the choice was made to round toward
    > > zero?

    >
    > Well, yes - that is precisely what he said.



    One can't tell what choice was made for positive numbers - for positive numbers, "round toward zero" and "round toward negative infinity" always produce the same result.
     
    Fred K, Mar 12, 2012
    #6
  7. somenath

    James Kuyper Guest

    On 03/12/2012 10:27 AM, Fred K wrote:
    > On Monday, March 12, 2012 7:15:48 AM UTC-7, James Kuyper wrote:
    >> On 03/12/2012 10:02 AM, somenath wrote:
    >>> On Mar 12, 5:23 pm, Eric Sosman <> wrote:

    >> ...
    >>>> Division must now "round toward zero," always. Nowadays, -7 / 3

    >> ...
    >>> So in case of 7/3 there also exist choices. It can be either 2 or 3.
    >>> But in C it is 2. So does it mean the choice was made to round toward
    >>> zero?

    >>
    >> Well, yes - that is precisely what he said.

    >
    >
    > One can't tell what choice was made for positive numbers - for positive numbers, "round toward zero" and "round toward negative infinity" always produce the same result.


    Yes, you can tell what choice was made. Just read what it says in the
    standard: "the result of the / operator is the algebraic quotient with
    any fractional part discarded.105" (6.5.5p6). The 105 refers to footnote
    105, which explains that "This is often called ‘‘truncation toward
    zero’’.". Alternatively, you could simply read the paraphrase of
    footnote 105 that was written by Eric Sosman and quoted by me above.

    I can understand questioning the accuracy of his paraphrase; but after
    being told "A is true, always", to ask "Is A true in this particular
    case?" seems a bit odd, unless you have some particular reason to doubt
    the "always" part; somenath provided no such reason.
     
    James Kuyper, Mar 12, 2012
    #7
  8. somenath

    James Kuyper Guest

    On 03/12/2012 10:24 AM, Fred K wrote:
    > On Monday, March 12, 2012 7:15:48 AM UTC-7, James Kuyper wrote:

    ....
    >
    > Not necessarily - for positive numbers, "round toward zero" and "round toward negative infinity" always produce the same result.
    >
    >
    > On Monday, March 12, 2012 7:15:48 AM UTC-7, James Kuyper wrote:

    ....
    >
    > On Monday, March 12, 2012 7:15:48 AM UTC-7, James Kuyper wrote:

    ....
    >
    > On Monday, March 12, 2012 7:15:48 AM UTC-7, James Kuyper wrote:

    ....
    >
    > On Monday, March 12, 2012 7:15:48 AM UTC-7, James Kuyper wrote:

    ....

    Whatever method you're using to insert quotes from the message you're
    responding to, it seems to have developed a stutter. Is this another
    Google Groups bug?
     
    James Kuyper, Mar 12, 2012
    #8
  9. somenath

    somenath Guest

    On Mar 12, 8:04 pm, James Kuyper <> wrote:
    > On 03/12/2012 10:27 AM, Fred K wrote:
    >
    > > On Monday, March 12, 2012 7:15:48 AM UTC-7, James Kuyper wrote:
    > >> On 03/12/2012 10:02 AM, somenath wrote:
    > >>> On Mar 12, 5:23 pm, Eric Sosman <> wrote:
    > >> ...
    > >>>> Division must now "round toward zero," always.  Nowadays, -7 / 3
    > >> ...
    > >>> So in case of 7/3 there also exist choices. It can be either 2 or 3.
    > >>> But in C it is 2. So does it mean the choice was made to round toward
    > >>> zero?

    >
    > >> Well, yes - that is precisely what he said.

    >
    > > One can't tell what choice was made for positive numbers - for positivenumbers, "round toward zero" and "round toward negative infinity" always produce the same result.

    >
    > Yes, you can tell what choice was made. Just read what it says in the
    > standard: "the result of the / operator is the algebraic quotient with
    > any fractional part discarded.105" (6.5.5p6). The 105 refers to footnote
    > 105, which explains that "This is often called ‘‘truncation toward
    > zero’’.". Alternatively, you could simply read the paraphrase of
    > footnote 105 that was written by Eric Sosman and quoted by me above.
    >
    > I can understand questioning the accuracy of his paraphrase; but after
    > being told "A is true, always", to ask "Is A true in this particular
    > case?" seems a bit odd, unless you have some particular reason to doubt
    > the "always" part; somenath provided no such reason.


    I was not expressing my doubt rather I was trying to be assured that I
    understood correctly. Also when I said the options regarding positive
    number I meant truncation toward zero and truncation toward positive
    infinity not toward negative infinity.
     
    somenath, Mar 12, 2012
    #9
  10. somenath

    Eric Sosman Guest

    On 3/12/2012 10:02 AM, somenath wrote:
    >
    > Many thanks for the great response.
    > So in case of 7/3 there also exist choices. It can be either 2 or 3.
    > But in C it is 2. So does it mean the choice was made to round toward
    > zero?


    Yes, even before C was standardized. The original K&R (now
    sometimes called "K&R I" to distinguish it from the later work)
    says

    When positive integers are divided truncation is toward
    zero, but the form of truncation is machine-dependent if
    either operand is negative. [...] It is always true
    that (a/b)*b + a%b is equal to a (if b is not 0).

    This particular choice was made for C in Ancient Times. One
    supposes that it was inspired by the behavior of the machines
    known to R, and that the leeway for negatives represented R's
    caution about machines where C might someday be implemented.
    The freedom was preserved in the original ANSI Standard, but
    by the time C99 rolled around the Committee apparently decided
    that "round toward minus infinity" machines were either non-
    existent or so rare as to be negligible.

    --
    Eric Sosman
    d
     
    Eric Sosman, Mar 13, 2012
    #10
  11. somenath

    Tim Rentsch Guest

    Eric Sosman <> writes:

    > On 3/12/2012 10:02 AM, somenath wrote:
    >>
    >> Many thanks for the great response.
    >> So in case of 7/3 there also exist choices. It can be either 2 or 3.
    >> But in C it is 2. So does it mean the choice was made to round toward
    >> zero?

    >
    > Yes, even before C was standardized. The original K&R (now
    > sometimes called "K&R I" to distinguish it from the later work)
    > says
    >
    > When positive integers are divided truncation is toward
    > zero, but the form of truncation is machine-dependent if
    > either operand is negative. [...] It is always true
    > that (a/b)*b + a%b is equal to a (if b is not 0).
    >
    > This particular choice was made for C in Ancient Times. One
    > supposes that it was inspired by the behavior of the machines
    > known to R, and that the leeway for negatives represented R's
    > caution about machines where C might someday be implemented.
    > The freedom was preserved in the original ANSI Standard, but
    > by the time C99 rolled around the Committee apparently decided
    > that "round toward minus infinity" machines were either non-
    > existent or so rare as to be negligible.


    I believe there was also some pressure to converge
    on a single definition that would conform to other
    standard languages, regardless of whether those
    machines were negligibly rare or not.
     
    Tim Rentsch, Mar 13, 2012
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. velthuijsen
    Replies:
    4
    Views:
    375
    velthuijsen
    May 10, 2004
  2. Juri
    Replies:
    5
    Views:
    388
    Rob Williscroft
    Jun 15, 2004
  3. somenath
    Replies:
    4
    Views:
    318
  4. somenath
    Replies:
    0
    Views:
    275
    somenath
    Dec 14, 2007
  5. Goran
    Replies:
    4
    Views:
    368
    Frank Birbacher
    May 23, 2008
Loading...

Share This Page