P
pemo
I'm sure it's obvious, but why is 2 the result of 22 % 5, rather than
4?
4?
pemo said:I'm sure it's obvious, but why is 2 the result of 22 % 5, rather than
4?
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
22 divided by 5 is 4 remainder 2
% is the remainder operator
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.
You might want to look up floating point to learn more about the
representation of numbers in computers.
pemo said:Well, Windows calc, given 22 / 5 says the result is 4.4
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
I'm speechless. http://en.wikipedia.org/wiki/Remainder.pemo said: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???
Please, rather than suggest I read someone else's explanation, please
point out why, something that seems so simple is so weird!
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 address 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"
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'!
pemo said:[snip]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).
THANK YOU Keith Thompson - at last, a plain speaking, descriptive and
down to earth rationale! MANY, many thanks!
I'm sure it's obvious, but why is 2 the result of 22 % 5, rather than
4?
pemo said:
As I was to your 'contribution' [and your arrogance] - such a valid
contribution (to your ego)!
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!
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???
Please, rather than suggest I read someone else's explanation, please
point out why, something that seems so simple is so weird!
pemo said: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???
Please, rather than suggest I read someone else's explanation, please
point out why, something that seems so simple is so weird!
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???
Please, rather than suggest I read someone else's explanation, please
point out why, something that seems so simple is so weird!
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.