# % question

Discussion in 'C Programming' started by pemo, May 30, 2010.

1. ### pemoGuest

I'm sure it's obvious, but why is 2 the result of 22 % 5, rather than
4?

pemo, May 30, 2010

2. ### WillemGuest

pemo wrote:
) I'm sure it's obvious, but why is 2 the result of 22 % 5, rather than
) 4?

If you divide 22 by 5, the remainder is 2.

Why would you expect the result to be 4 ?

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Willem, May 30, 2010

3. ### Ben PfaffGuest

pemo <> writes:

> I'm sure it's obvious, but why is 2 the result of 22 % 5, rather than
> 4?

Because 22 == 4 * 5 + 2.

If you want 4, use / instead of %.
--
"C has its problems, but a language designed from scratch would have some too,
and we know C's problems."
--Bjarne Stroustrup

Ben Pfaff, May 30, 2010
4. ### pemoGuest

On May 30, 5:53 pm, Willem <> wrote:
> pemo wrote:
>
> ) I'm sure it's obvious, but  why is 2 the result of 22 % 5, rather than
> ) 4?
>
> If you divide 22 by 5, the remainder is 2.
>
> Why would you expect the result to be 4 ?
>
> SaSW, Willem
> --
> Disclaimer: I am in no way responsible for any of the statements
>             made in the above text. For all I know I might be
>             drugged or something..
>             No I'm not paranoid. You all think I'm paranoid, don't you !
> #EOT

Well, Windows calc, given 22 / 5 says the result is 4.4

pemo, May 30, 2010
5. ### Nick KeighleyGuest

On 30 May, 18:42, pemo <> wrote:
> On May 30, 5:53 pm, Willem <> wrote:
> > pemo wrote:

> > ) I'm sure it's obvious, but  why is 2 the result of 22 % 5, rather than
> > ) 4?

22 divided by 5 is 4 remainder 2

% is the remainder operator

> > If you divide 22 by 5, the remainder is 2.

>
> > Why would you expect the result to be 4 ?

>
> Well, Windows calc, given 22 / 5 says the result is 4.4

well / is division not remainder. Also

22 / 5 = 4

because C's / operator applied to integers yeilds an integral answer.
Microsoft calc (Calculator?) is dealing with floating variables
("real" numbers) rather than integers.

representation of numbers in computers.

Nick Keighley, May 30, 2010
6. ### pemoGuest

On May 30, 7:00 pm, Nick Keighley <>
wrote:
> On 30 May, 18:42, pemo <> wrote:
>
> > On May 30, 5:53 pm, Willem <> wrote:
> > > pemo wrote:
> > > ) I'm sure it's obvious, but  why is 2 the result of 22 % 5, rather than
> > > ) 4?

>
> 22 divided by 5 is 4 remainder 2
>
> % is the remainder operator
>
> > > If you divide 22 by 5, the remainder is 2.

>
> > > Why would you expect the result to be 4 ?

>
> > Well, Windows calc, given 22 / 5 says the result is 4.4

>
> well / is division not remainder. Also
>
> 22 / 5 = 4
>
> because C's / operator applied to integers yeilds an integral answer.
> Microsoft calc (Calculator?) is dealing with floating variables
> ("real" numbers) rather than integers.
>
> representation of numbers in computers.

I'm quite happy(ish!) to reveal my ignorance, but as a trusted
calculator says that 22/5 is 4.4, why isn't 22 mod 5 == 4 --- surely,
4 *is* the remainder?

I mean, try it yourself - I have, on a variety of calculators; in my
head; on paper etc --- 22/5 IS 4.4, so why isn't 22 mod 5 == 4???

point out why, something that seems so simple is so weird!

pemo, May 30, 2010
7. ### Keith ThompsonGuest

pemo <> writes:
> On May 30, 5:53Â pm, Willem <> wrote:
>> pemo wrote:
>> ) I'm sure it's obvious, but Â why is 2 the result of 22 % 5, rather than
>> ) 4?
>>
>> If you divide 22 by 5, the remainder is 2.
>>
>> Why would you expect the result to be 4 ?

>
> Well, Windows calc, given 22 / 5 says the result is 4.4

Windows calc is showing you the result of a real division, not an
integer division. In C terms, it's computing 22.0 / 5.0, not 22 / 5.

If I understand you correctly, you're assuming that the result of
"/" should be the 4 that appears before the decimal point (which
is correct), and the result of "%" should be the 4 that appears
after the decimal point (which is wrong).

The "4" after the decimal point represents the real value 0.4,
or 4/10. Since you're not dividing by 10, but rather by 5, that's
not what "%" is going to give you. Using real values rather than
integers, 22 / 5 = 4 + 2/5; the 2 is the remainder, and therefore
the result of 22 % 5.

Consider 22 / 7. If you divide them as real numbers, the result is
going to be approximately 3.142857. Do you expect 22 % 7 to yield
142857? Try to figure out what 22/7 and 22%7 should be *before*
writing a C program to to compute them.

Here's what the standard says (C99 6.5.5p6):

When integers are divided, the result of the / operator is
the algebraic quotient with any fractional part discarded.
[footnote: This is often called "truncation toward zero".]
If the quotient a/b is representable, the expression
(a/b)*b + a%b shall equal a.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, May 30, 2010
8. ### pemoGuest

On May 30, 7:33 pm, Geoff <> wrote:
> On Sun, 30 May 2010 11:19:47 -0700 (PDT), pemo <>
> wrote:
>
> >I'm quite happy(ish!) to reveal my ignorance, but as a trusted
> >calculator says that 22/5 is 4.4, why isn't 22 mod 5 == 4 --- surely,
> >4 *is* the remainder?

>
> 4 is the Quotient, not the remainder.
>
> Go back to elementary school. Do the division longhand. 5 x 4 == 20.
> 20 + 2 = 22.
>
> 22 / 5 = 4 (quotient) remainder 2.
>
> The result of the % operator is the remainder of the division, not the
> quotient.
>
> In your calculator the result of % would be two steps:
>
> Step 1.      22 / 5 = 4.4
>
> Step 2.      0.4 * 5 = 2

Nope, still don't get it (sorry!) - plus, using words like 'quotient'
doesn't help ... keep it simple (are there any teachers in this
group!)

What I'm getting from this is that I'm sure toooo dumb to ask what to
most of you is, um, a 'stupid dumb ****' question!

PLEASE, 22 / 5 is 4.4 --- right????

So, if % gives the *remainder*, after the whole division; why is it -
the remainder = '2' rather than '4'!

Simple question, from a simple mind, that's hoping for a simple(ton)

So, TRY and come down to my level and spell it out. Please!

pemo, May 30, 2010
9. ### James LothianGuest

pemo wrote:

> I'm quite happy(ish!) to reveal my ignorance, but as a trusted
> calculator says that 22/5 is 4.4, why isn't 22 mod 5 == 4 --- surely,
> 4 *is* the remainder?
>
> I mean, try it yourself - I have, on a variety of calculators; in my
> head; on paper etc --- 22/5 IS 4.4, so why isn't 22 mod 5 == 4???
>
> point out why, something that seems so simple is so weird!
>
>

I'm speechless. http://en.wikipedia.org/wiki/Remainder.

James

James Lothian, May 30, 2010
10. ### pemoGuest

On May 30, 7:43 pm, Keith Thompson <> wrote:
> pemo <> writes:
> > On May 30, 5:53 pm, Willem <> wrote:
> >> pemo wrote:
> >> ) I'm sure it's obvious, but  why is 2 the result of 22 % 5, rather than
> >> ) 4?

>
> >> If you divide 22 by 5, the remainder is 2.

>
> >> Why would you expect the result to be 4 ?

>
> > Well, Windows calc, given 22 / 5 says the result is 4.4

>
> Windows calc is showing you the result of a real division, not an
> integer division.  In C terms, it's computing 22.0 / 5.0, not 22 / 5.
>
> If I understand you correctly, you're assuming that the result of
> "/" should be the 4 that appears before the decimal point (which
> is correct), and the result of "%" should be the 4 that appears
> after the decimal point (which is wrong).
>
> The "4" after the decimal point represents the real value 0.4,
> or 4/10.  Since you're not dividing by 10, but rather by 5, that's
> not what "%" is going to give you.  Using real values rather than
> integers, 22 / 5 = 4 + 2/5; the 2 is the remainder, and therefore
> the result of 22 % 5.
>
> Consider 22 / 7.  If you divide them as real numbers, the result is
> going to be approximately 3.142857.  Do you expect 22 % 7 to yield
> 142857?  Try to figure out what 22/7 and 22%7 should be *before*
> writing a C program to to compute them.
>
> Here's what the standard says (C99 6.5.5p6):
>
>     When integers are divided, the result of the / operator is
>     the algebraic quotient with any fractional part discarded.
>     [footnote: This is often called "truncation toward zero".]
>     If the quotient a/b is representable, the expression
>     (a/b)*b + a%b shall equal a.
>
> --
> Keith Thompson (The_Other_Keith)  <http://www.ghoti.net/~kst>
> Nokia
> "We must do something.  This is something.  Therefore, we must do this."
>     -- Antony Jay and Jonathan Lynn, "Yes Minister"

THANK YOU Keith Thompson - at last, a plain speaking, descriptive and
down to earth rationale! MANY, many thanks!

pemo, May 30, 2010
11. ### bart.cGuest

pemo wrote:

> PLEASE, 22 / 5 is 4.4 --- right????
>
> So, if % gives the *remainder*, after the whole division; why is it -
> the remainder = '2' rather than '4'!

The remainder is 2 (fifths), or 2/5 as a fraction, or 0.4 in decimal.

The % operator gives the first value.

--
Bartc

bart.c, May 30, 2010
12. ### Keith ThompsonGuest

pemo <> writes:
> On May 30, 7:43Â pm, Keith Thompson <> wrote:
>> pemo <> writes:
>> > On May 30, 5:53Â pm, Willem <> wrote:
>> >> pemo wrote:
>> >> ) I'm sure it's obvious, but Â why is 2 the result of 22 % 5, rather than
>> >> ) 4?

>>
>> >> If you divide 22 by 5, the remainder is 2.

>>
>> >> Why would you expect the result to be 4 ?

>>
>> > Well, Windows calc, given 22 / 5 says the result is 4.4

>>
>> Windows calc is showing you the result of a real division, not an
>> integer division. Â In C terms, it's computing 22.0 / 5.0, not 22 / 5.
>>
>> If I understand you correctly, you're assuming that the result of
>> "/" should be the 4 that appears before the decimal point (which
>> is correct), and the result of "%" should be the 4 that appears
>> after the decimal point (which is wrong).

[snip]
>
> THANK YOU Keith Thompson - at last, a plain speaking, descriptive and
> down to earth rationale! MANY, many thanks!

You're welcome.

I should point out that I think the reason most people here didn't
understand your problem is that you chose a poor example, and didn't
as a result clearly explain what your misconception was. Don't be
too hard on the people who didn't understand what you were asking.
(I was mostly lucky myself.)

Your example was 22 / 5 = 4.4. You told us (several times) that you
thought 22 / 5 should be 4. Most of us thought you were referring
to the 4 *before* the decimal point; the 4 after the decimal point
is so obviously (to most of us) a fraction that it's hard to think
of it as an integer.

If you had tried 23 / 5 = 4.6 and told us that you thought 23 % 5
should be 6, you might have gotten an explanation sooner. Or if
you had tried 22 / 7 = 3.142857 (approximately), you might have
seen the flaw in your own reasoning, if not the solution.

I'm not suggesting that you should have thought of this yourself;
that probably would have required that you understand the situation,
and then you wouldn't have needed to ask in the first place. But if
you had shown us, say, 2 or 3 different examples and/or tried to
explain more clearly *why* you thought the result should be 4,
that would have helped us to understand your question.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, May 30, 2010
13. ### pemoGuest

On May 30, 7:51 pm, James Lothian
<> wrote:
> pemo wrote:
> > I'm quite happy(ish!) to reveal my ignorance, but as a trusted
> > calculator says that 22/5 is 4.4, why isn't 22 mod 5 == 4 --- surely,
> > 4 *is* the remainder?

>
> > I mean, try it yourself - I have, on a variety of calculators; in my
> > head; on paper etc --- 22/5 IS 4.4, so why isn't 22 mod 5 == 4???

>
> > point out why, something that seems so simple is so weird!

>
> I'm speechless.http://en.wikipedia.org/wiki/Remainder.
>
> James

>>> I'm speechless

As I was to your 'contribution' [and your arrogance] - such a valid

You know, one can always 'link' to this/that, or 'exclaim' with an 'oh
dear' - things are only obvious to some - the rest of us would like to
learn, sans smarmie know-it-all comments like yours!

c.f. your 'oh dear' with Keith's contribution - then 'relect' for a
moment!

pemo, May 30, 2010
14. ### Denis McMahonGuest

On 30/05/10 17:26, pemo wrote:
> I'm sure it's obvious, but why is 2 the result of 22 % 5, rather than
> 4?

22 % 5 = 4 remainder 2, the % operator gives the remainder of the
integer division.

22 / 5 as ints will give the 4 that you expect.

/******* code below *******/

#include <stdio.h>

void main(int argc, char * argv[]) {

printf("22 / 5 = %d remainder %d\n",22/5,22%5);

}

/******* code above *******/

Rgds

Denis McMahon

Denis McMahon, May 30, 2010
15. ### Keith ThompsonGuest

pemo <> writes:
> On May 30, 7:51Â pm, James Lothian
> <> wrote:
>> pemo wrote:
>> > I'm quite happy(ish!) to reveal my ignorance, but as a trusted
>> > calculator says that 22/5 is 4.4, why isn't 22 mod 5 == 4 --- surely,
>> > 4 *is* the remainder?

>>
>> > I mean, try it yourself - I have, on a variety of calculators; in my
>> > head; on paper etc --- 22/5 IS 4.4, so why isn't 22 mod 5 == 4???

>>
>> > point out why, something that seems so simple is so weird!

>>
>> I'm speechless.http://en.wikipedia.org/wiki/Remainder.
>>
>> James

>
>>>> I'm speechless

>
> As I was to your 'contribution' [and your arrogance] - such a valid
>
> You know, one can always 'link' to this/that, or 'exclaim' with an 'oh
> dear' - things are only obvious to some - the rest of us would like to
> learn, sans smarmie know-it-all comments like yours!
>
> c.f. your 'oh dear' with Keith's contribution - then 'relect' for a
> moment!

If you're going to be participating here, I suggest you grow
a thicker skin. Given the information you gave us, it really
wasn't easy to figure out just what your misunderstanding was.
I think that's why some people reacted as if you has asked why
2 + 2 isn't 22.

In any case, the article at <http://en.wikipedia.org/wiki/Remainder>
who try to help you, you're not going to get much help.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Keith Thompson, May 30, 2010
16. ### spinoza1111Guest

On May 31, 2:19 am, pemo <> wrote:
> On May 30, 7:00 pm, Nick Keighley <>
> wrote:
>
>
>
>
>
> > On 30 May, 18:42, pemo <> wrote:

>
> > > On May 30, 5:53 pm, Willem <> wrote:
> > > > pemo wrote:
> > > > ) I'm sure it's obvious, but  why is 2 the result of 22 % 5, rather than
> > > > ) 4?

>
> > 22 divided by 5 is 4 remainder 2

>
> > % is the remainder operator

>
> > > > If you divide 22 by 5, the remainder is 2.

>
> > > > Why would you expect the result to be 4 ?

>
> > > Well, Windows calc, given 22 / 5 says the result is 4.4

>
> > well / is division not remainder. Also

>
> > 22 / 5 = 4

>
> > because C's / operator applied to integers yeilds an integral answer.
> > Microsoft calc (Calculator?) is dealing with floating variables
> > ("real" numbers) rather than integers.

>
> > representation of numbers in computers.

>
> I'm quite happy(ish!) to reveal my ignorance, but as a trusted
> calculator says that 22/5 is 4.4, why isn't 22 mod 5 == 4 --- surely,
> 4 *is* the remainder?
>
> I mean, try it yourself - I have, on a variety of calculators; in my
> head; on paper etc --- 22/5 IS 4.4, so why isn't 22 mod 5 == 4???
>
> point out why, something that seems so simple is so weird!

"Do not worry about your problems with mathematics, I assure you mine
are far greater" - Einstein

Keith has explained this very well. Terminology:

integer: a counting number: 0, 1, 2 and so forth: also -1, -2 and so
forth

real number: a measuring number: 0, .25, -.001 and so on

complex number: a multiple of the "nonexistent" square root of -1
times a real number: not used in everyday arithmetic, used in modern
physics. All integers are reals, all reals are complex, and all
equations have a solution if there are complex numbers

dividend: the number being divided

divisor: the number the dividend is being divided by

remainder: the difference between the integer result and the real
result

Lancelot Hogben, "Mathematics for the Million".

Many adults and more teens have in fact not been able to learn
mathematics. Nonetheless, they need to use and even program computers
on the job.

One model of education is that they must learn one thing at a time as
in traditional schools. However, this model often fails to work.

While learning C, it might be an excellent idea to learn basic
mathematics. This integrated approach works, and is recommended by
educator (and political radical) Bill Ayers (cf his book To Teach: the
Journey of a Teacher, Teachers College Press, 2000). Ayers teaches
"basic skills" in K-12 in the context of useful projects such as
mapping one's neighborhood, saving money or doing a survey.

Brazilian educator Paulo Freire also points out that in adult
education, the teacher must respect, use and learn from adult students
who have been deprived of the so-called basics in a conversation. In
this ng, there are a lot of posters who have been in fact mistreated
by traditional educational models: diagnosed and labeled as "not good
at math" or "dyslexic". They've tracked themselves or been tracked in
programming to discover that they are good with computers because
computers "make it real", whereas in traditional classrooms, the
teacher (who's usually herself strikingly ignorant about math) tells
the "slow" children they are "stupid".

I learned finite mathematics by using a computer.

OP, don't be discouraged by some of the replies. Stand up for your
rights. There are a lot of people here with one or more deficiencies
in basic skills including reading comprehension and writing complex
difficulties while attacking others for theirs.

Keith Thompson, who has answered your question well, tells you to suck
up the sort of abuse that goes on here. I say, don't.

spinoza1111, May 31, 2010
17. ### Charlton WilburGuest

>>>>> "p" == pemo <> writes:

p> What I'm getting from this is that I'm sure toooo dumb to ask
p> what to most of you is, um, a 'stupid dumb ****' question!

p> PLEASE, 22 / 5 is 4.4 --- right????

Only if you're working with real or rational numbers. If you're working
with integer math, the value 4.4 is impossible, so 22/5 is 4.

p> So, if % gives the *remainder*, after the whole division; why is
p> it - the remainder = '2' rather than '4'!

Because the remainder is not the decimal portion of the quotient.

Go get yourself 22 things. When I was in second grade we used dried
beans, but anything you have 22 of will be handy. Divide them into five
even groups, and note that you can't split them into partial beans.
What happens?

Well, you find that you have 4 groups of 5, and 2 beans left over.

So, when you work with C integers, 22 / 5 is 4. (*NOT* 4.4, because 4.4
is not an integer.) And 22 % 5 is 2, because that's the *remainder*.

Charlton

--
Charlton Wilbur

Charlton Wilbur, May 31, 2010
18. ### Charles RichmondGuest

pemo wrote:
> On May 30, 7:00 pm, Nick Keighley <>
> wrote:
>> On 30 May, 18:42, pemo <> wrote:
>>
>>> On May 30, 5:53 pm, Willem <> wrote:
>>>> pemo wrote:
>>>> ) I'm sure it's obvious, but why is 2 the result of 22 % 5, rather than
>>>> ) 4?

>> 22 divided by 5 is 4 remainder 2
>>
>> % is the remainder operator
>>
>>>> If you divide 22 by 5, the remainder is 2.
>>>> Why would you expect the result to be 4 ?
>>> Well, Windows calc, given 22 / 5 says the result is 4.4

>> well / is division not remainder. Also
>>
>> 22 / 5 = 4
>>
>> because C's / operator applied to integers yeilds an integral answer.
>> Microsoft calc (Calculator?) is dealing with floating variables
>> ("real" numbers) rather than integers.
>>
>> representation of numbers in computers.

>
> I'm quite happy(ish!) to reveal my ignorance, but as a trusted
> calculator says that 22/5 is 4.4, why isn't 22 mod 5 == 4 --- surely,
> 4 *is* the remainder?
>
> I mean, try it yourself - I have, on a variety of calculators; in my
> head; on paper etc --- 22/5 IS 4.4, so why isn't 22 mod 5 == 4???
>
> point out why, something that seems so simple is so weird!
>
>

The 0.4 is *not* the remainder. 5 * 0.4 = 2. Two is the remainder.

--
+----------------------------------------+
| Charles and Francis Richmond |
| |
| plano dot net at aquaporin4 dot com |
+----------------------------------------+

Charles Richmond, Jun 1, 2010
19. ### Nick KeighleyGuest

On 30 May, 19:19, pemo <> wrote:
> On May 30, 7:00 pm, Nick Keighley <>
> > On 30 May, 18:42, pemo <> wrote:
> > > On May 30, 5:53 pm, Willem <> wrote:
> > > > pemo wrote:

> > > > ) I'm sure it's obvious, but  why is 2 the result of 22 % 5, rather than
> > > > ) 4?

>
> > 22 divided by 5 is 4 remainder 2

>
> > % is the remainder operator

I was taught division in a day before calculators. On being first
taught division we were taught it as an operation on integers (no
fractions or decimals). Division was repeated subtraction and
subtraction was a big word meaning "to take away". You have 22 apples
to divide as best you can between 5 people.

22 take away 5 = 17 each person has 1 apple
17 take away 5 = 13 each person has 2 apples
12 take away 5 = 7 each person has 3 apples
7 take away 5 = 2 each person has 4 apples

So each person gets 4 apples and there 2 left over. That was they
called "the remainder" because it was what remained after you had done
as many take-aways as possible. Made sense to me when I was 6.

The remainder operator % is still sometimes useful.

> > > > If you divide 22 by 5, the remainder is 2.

>
> > > > Why would you expect the result to be 4 ?

>
> > > Well, Windows calc, given 22 / 5 says the result is 4.4

>
> > well / is division not remainder. Also

>
> > 22 / 5 = 4

>
> > because C's / operator applied to integers yeilds an integral answer.
> > Microsoft calc (Calculator?) is dealing with floating variables
> > ("real" numbers) rather than integers.

>
> > representation of numbers in computers.

really, you should do this

> I'm quite happy(ish!) to reveal my ignorance,

I've never seen in problem with admitting to ignorance. How else are
you going to learn things?

> but as a trusted
> calculator says that 22/5 is 4.4, why isn't 22 mod 5 == 4 --- surely,
> 4 *is* the remainder?
>
> I mean, try it yourself - I have, on a variety of calculators; in my
> head; on paper etc --- 22/5 IS 4.4, so why isn't 22 mod 5 == 4???

because you have a misapprehension as to the meaning of mod

> point out why, something that seems so simple is so weird!

please assume I have a reason for suggesting you learn the basics. It
took quite some time for other posters to disentangle what your
misapprehension was. Being a toutorial for infant school mathematics
seems a bit much for a newsgroup devoted to a programming language.

You might think of getting a better attitude if you want answers to
future questions.

Happy Programming.

Nick Keighley, Jun 1, 2010