Velocity Reviews > % question

% question

pemo
Guest
Posts: n/a

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

Willem
Guest
Posts: n/a

 05-30-2010
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

Ben Pfaff
Guest
Posts: n/a

 05-30-2010
pemo <(E-Mail Removed)> 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

pemo
Guest
Posts: n/a

 05-30-2010
On May 30, 5:53*pm, Willem <(E-Mail Removed)> 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

Nick Keighley
Guest
Posts: n/a

 05-30-2010
On 30 May, 18:42, pemo <(E-Mail Removed)> wrote:
> On May 30, 5:53*pm, Willem <(E-Mail Removed)> 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.

pemo
Guest
Posts: n/a

 05-30-2010
On May 30, 7:00*pm, Nick Keighley <(E-Mail Removed)>
wrote:
> On 30 May, 18:42, pemo <(E-Mail Removed)> wrote:
>
> > On May 30, 5:53*pm, Willem <(E-Mail Removed)> 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!

Keith Thompson
Guest
Posts: n/a

 05-30-2010
pemo <(E-Mail Removed)> writes:
> On May 30, 5:53Â*pm, Willem <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"

pemo
Guest
Posts: n/a

 05-30-2010
On May 30, 7:33*pm, Geoff <(E-Mail Removed)> wrote:
> On Sun, 30 May 2010 11:19:47 -0700 (PDT), pemo <(E-Mail Removed)>
> 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!

James Lothian
Guest
Posts: n/a

 05-30-2010
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

pemo
Guest
Posts: n/a

 05-30-2010
On May 30, 7:43*pm, Keith Thompson <(E-Mail Removed)> wrote:
> pemo <(E-Mail Removed)> writes:
> > On May 30, 5:53*pm, Willem <(E-Mail Removed)> 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) (E-Mail Removed) *<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!