Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #18566 > unrolled thread

question regarding % operator.

Started bysomenath <pal_somenath@emc.com>
First post2012-03-11 20:17 -0700
Last post2012-03-13 16:35 -0700
Articles 13 — 7 participants

Back to article view | Back to comp.lang.c


Contents

  question regarding % operator. somenath <pal_somenath@emc.com> - 2012-03-11 20:17 -0700
    Re: question regarding % operator. gordonb.ddi19@burditt.org (Gordon Burditt) - 2012-03-12 02:27 -0500
    Re: question regarding % operator. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-03-12 08:23 -0400
      Re: question regarding % operator. somenath <pal_somenath@emc.com> - 2012-03-12 07:02 -0700
        Re: question regarding % operator. James Kuyper <jameskuyper@verizon.net> - 2012-03-12 10:15 -0400
          Re: question regarding % operator. Fred K <fred.l.kleinschmidt@gmail.com> - 2012-03-12 07:27 -0700
            Re: question regarding % operator. James Kuyper <jameskuyper@verizon.net> - 2012-03-12 11:04 -0400
              Re: question regarding % operator. somenath <pal_somenath@emc.com> - 2012-03-12 08:43 -0700
                Re: question regarding % operator. Robert Wessel <robertwessel2@yahoo.com> - 2012-03-12 11:04 -0500
          Re: question regarding % operator. Fred K <fred.l.kleinschmidt@gmail.com> - 2012-03-12 07:24 -0700
            Re: question regarding % operator. James Kuyper <jameskuyper@verizon.net> - 2012-03-12 11:24 -0400
        Re: question regarding % operator. Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-03-12 20:17 -0400
          Re: question regarding % operator. Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-13 16:35 -0700

#18566 — question regarding % operator.

Fromsomenath <pal_somenath@emc.com>
Date2012-03-11 20:17 -0700
Subjectquestion regarding % operator.
Message-ID<5cd1384f-dadd-4f6f-abcb-dafe20b3498a@qb4g2000pbb.googlegroups.com>
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.

[toc] | [next] | [standalone]


#18570

Fromgordonb.ddi19@burditt.org (Gordon Burditt)
Date2012-03-12 02:27 -0500
Message-ID<soqdnSAMLpTkOMDSnZ2dnUVZ_qWdnZ2d@posted.internetamerica>
In reply to#18566
> 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?

This isn't true for C99 or probably C11 (which I haven't seen).

1.  Because the C90 standard says so.  
2.  Because some of the hardware involved rounds integer division towards
    negative infinity, and some of the hardware rounds it towards zero.
    Property (A) below then determines the operation of %.
    The rounds-towards-zero crowd seems to have eventually won in C99.
If you want defined behavior, use div() or ldiv().

There are three properties that are considered desirable for / and % operators:

A.  a == ((a%b) + b*(a/b))	You can put the quotient and remainder back together.
	This is specifically required in C89, C99, and probably C11.
B.  -((a)/b) == ((-a)/b)
C.  a%b has a range of 0 to (b-1), to correspond with the mathematical
	definition used for modular arithmetic.

Unfortunately, you can't have all three at the same time.  Mathematically, there
are two operations, modulo and remainder, which are different, but in computer
science and programming languages, what is called modulo is often really remainder.

Some languages, such as Ada, provide mod and rem operators that provide both behaviors.

> Could you please explain how the % operator behaves in C ? I

In C99 the sign of a%b, if a%b is not zero, is the same as the sign of a.

> 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?

In C90, there is not a single answer to that question, because it is
implementation-defined.

[toc] | [prev] | [next] | [standalone]


#18576

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-03-12 08:23 -0400
Message-ID<jjkpsv$b5i$1@dont-email.me>
In reply to#18566
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
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#18580

Fromsomenath <pal_somenath@emc.com>
Date2012-03-12 07:02 -0700
Message-ID<0abfe2cd-dfdf-4dd8-b17f-f34686ee29b8@y27g2000yqy.googlegroups.com>
In reply to#18576
On Mar 12, 5:23 pm, Eric Sosman <esos...@ieee-dot-org.invalid> 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?

[toc] | [prev] | [next] | [standalone]


#18582

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-03-12 10:15 -0400
Message-ID<4F5E0514.5030505@verizon.net>
In reply to#18580
On 03/12/2012 10:02 AM, somenath wrote:
> On Mar 12, 5:23 pm, Eric Sosman <esos...@ieee-dot-org.invalid> 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.

[toc] | [prev] | [next] | [standalone]


#18584

FromFred K <fred.l.kleinschmidt@gmail.com>
Date2012-03-12 07:27 -0700
Message-ID<25841733.5359.1331562467047.JavaMail.geo-discussion-forums@ynje13>
In reply to#18582
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 <esos...@ieee-dot-org.invalid> 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.

[toc] | [prev] | [next] | [standalone]


#18585

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-03-12 11:04 -0400
Message-ID<4F5E1098.1090105@verizon.net>
In reply to#18584
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 <esos...@ieee-dot-org.invalid> 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.

[toc] | [prev] | [next] | [standalone]


#18589

Fromsomenath <pal_somenath@emc.com>
Date2012-03-12 08:43 -0700
Message-ID<8fee42ef-6a9b-4a2c-8a8b-0020225a2682@lf20g2000pbb.googlegroups.com>
In reply to#18585
On Mar 12, 8:04 pm, James Kuyper <jameskuy...@verizon.net> 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 <esos...@ieee-dot-org.invalid> 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.

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.

[toc] | [prev] | [next] | [standalone]


#18590

FromRobert Wessel <robertwessel2@yahoo.com>
Date2012-03-12 11:04 -0500
Message-ID<9b7sl7913krh5apehkftnmrcuhu34vilae@4ax.com>
In reply to#18589
On Mon, 12 Mar 2012 08:43:34 -0700 (PDT), somenath
<pal_somenath@emc.com> wrote:

>On Mar 12, 8:04 pm, James Kuyper <jameskuy...@verizon.net> 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 <esos...@ieee-dot-org.invalid> 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.
>
>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.


Truncations towards positive infinity would be absurd as a general
approach, so the decision was undoubtedly made, but was trivial.

[toc] | [prev] | [next] | [standalone]


#18586

FromFred K <fred.l.kleinschmidt@gmail.com>
Date2012-03-12 07:24 -0700
Message-ID<2487285.341.1331562256280.JavaMail.geo-discussion-forums@yner4>
In reply to#18582
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 <esos...@ieee-dot-org.invalid> 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 <esos...@ieee-dot-org.invalid> 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 <esos...@ieee-dot-org.invalid> 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 <esos...@ieee-dot-org.invalid> 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 <esos...@ieee-dot-org.invalid> 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.

[toc] | [prev] | [next] | [standalone]


#18588

FromJames Kuyper <jameskuyper@verizon.net>
Date2012-03-12 11:24 -0400
Message-ID<4F5E151F.5020405@verizon.net>
In reply to#18586
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?

[toc] | [prev] | [next] | [standalone]


#18621

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2012-03-12 20:17 -0400
Message-ID<jjm3nd$k7i$1@dont-email.me>
In reply to#18580
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
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#18736

FromTim Rentsch <txr@alumni.caltech.edu>
Date2012-03-13 16:35 -0700
Message-ID<kfnwr6okras.fsf@x-alumni2.alumni.caltech.edu>
In reply to#18621
Eric Sosman <esosman@ieee-dot-org.invalid> 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web