the dreaded %

K

KyoGaSuki

So, as I am looking over previous tests of mine, I am finding one
thing that is always marked wrong -- things that include % (mod) I
simply don't understand how you solve it (of course, our prof. does
things like 5/2+12%5 and expects us to solve it. I can do the 5/2 =
2.5 ...but what is 12%5? I doubt it would be solved like 12/5 ...that
seems too easy...can someone help at least explain what i am missing?)
 
J

Joshua Cranmer

KyoGaSuki said:
So, as I am looking over previous tests of mine, I am finding one
thing that is always marked wrong -- things that include % (mod) I
simply don't understand how you solve it (of course, our prof. does
things like 5/2+12%5 and expects us to solve it. I can do the 5/2 =
2.5 ...but what is 12%5? I doubt it would be solved like 12/5 ...that
seems too easy...can someone help at least explain what i am missing?)

a % b is roughly equivalent to (in integer arithmetic) a - a / b * b (I
say roughly because I don't recall off the top of my had what happens
when negative numbers enter the mix), or the remainder one would get
when dividing a by b. Therefore, 12 % 5 is 2.
 
P

Patricia Shanahan

KyoGaSuki said:
So, as I am looking over previous tests of mine, I am finding one
thing that is always marked wrong -- things that include % (mod) I
simply don't understand how you solve it (of course, our prof. does
things like 5/2+12%5 and expects us to solve it. I can do the 5/2 =
2.5 ...but what is 12%5? I doubt it would be solved like 12/5 ...that
seems too easy...can someone help at least explain what i am missing?)

Be careful about the differences between real number arithmetic and Java
arithmetic. 5/2 in real number arithmetic is 2.5. As a Java expression,
both 5 and 2 are int literals, so the "/" is integer division, and the
result is 2.

If you can solve 5/2 you can solve 12%5. Although some people call it
"modulo", % in Java is actually defined as the integer division
remainder: "(a/b)*b+(a%b) is equal to a"
[http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.17.3]

Suppose a is 12 and b is 5. Then, in integer division, a/b is 2. (a/b)*b
is 10. You would need to add 2 to that to get 12, so 12%5 is 2.

To deal with negative numbers, note that integer division in Java is
always rounded towards zero.

Patricia
 
T

Thomas Fritsch

KyoGaSuki said:
So, as I am looking over previous tests of mine, I am finding one
thing that is always marked wrong -- things that include % (mod) I
simply don't understand how you solve it (of course, our prof. does
things like 5/2+12%5 and expects us to solve it. I can do the 5/2 =
2.5 ...but what is 12%5? I doubt it would be solved like 12/5 ...that
seems too easy...can someone help at least explain what i am missing?)
This is how I memorize the modulo thing.

When we were roughly 10 years old, we learned to divide with digits
behind the decimal point:
12 / 5 = 2.4

Even earlier in school, when we were 6 years old, we learned the integer
division with remainder:
12 / 5 = 2 remainder 2
The remainder 2 here is the modulo result.
12 % 5 = 2
 
C

Chase Preuninger

lets take something easy

6%5

so now, what is 6/5
it is 6/5 = 1.2
now truncate the decimal and you are left with 1.
now subtract 1 from 6 thereby giving you the remainder.

6%5=5
 
P

Patricia Shanahan

Chase said:
lets take something easy

6%5

so now, what is 6/5
it is 6/5 = 1.2
now truncate the decimal and you are left with 1.
now subtract 1 from 6 thereby giving you the remainder.

6%5=5

This is not correct. You need to multiply the 1 by 5 before the
subtraction. 6%5 is 6-(5*1) which is 1.

public class RemainderTest {
public static void main(String[] args) {
System.out.println(6%5);
}
}

indeed prints 1.

Patricia
 
R

Roedy Green

So, as I am looking over previous tests of mine, I am finding one
thing that is always marked wrong -- things that include % (mod) I
simply don't understand how you solve it (of course, our prof. does
things like 5/2+12%5 and expects us to solve it. I can do the 5/2 =
2.5 ...but what is 12%5? I doubt it would be solved like 12/5 ...that
seems too easy...can someone help at least explain what i am missing?)

There is nothing to dread, except the behaviour when one or both of
the operands are negative. Then there are many different ways it could
be implemented. Every language picks a different way to handle it.
Forth got it right in my opinion. I would suspect Java does it the way
Sun hardware does.

99% of the time both operands are positive; then it is simply the
remainder.

It bugs be that % code, if it works differently from hardware needs to
check both operands before every % operation (even if at compile
time). It is too bad somebody like Gauss did not nail down the
definition long before the age of computers.
 
M

Mark Space

This is not correct. You need to multiply the 1 by 5 before the
subtraction. 6%5 is 6-(5*1) which is 1.

Wouldn't it be slightly more correct to say 6%5 is 6-(5*(6/5)) where /
is the integer math division operator?

This seems to work for all cases, for the few cases I submitted to testing.

int a = 6;
int b = 5;
int m = a%b;
int r = a-(b*(a/b));
a = -a;
m = a%b;
r = a-(b*(a/b));
b = -b;
m = a%b;
r = a-(b*(a/b));
a = -a;
m = a%b;
r = a-(b*(a/b));
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top