Discussion in 'Java' started by KyoGaSuki, Apr 22, 2008.

1. ### KyoGaSukiGuest

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

KyoGaSuki, Apr 22, 2008

2. ### Joshua CranmerGuest

KyoGaSuki wrote:
> 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.

--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth

Joshua Cranmer, Apr 22, 2008

3. ### Patricia ShanahanGuest

KyoGaSuki wrote:
> 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

Patricia Shanahan, Apr 22, 2008
4. ### Thomas FritschGuest

KyoGaSuki wrote:
> 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

--
Thomas

Thomas Fritsch, Apr 23, 2008
5. ### Roedy GreenGuest

Roedy Green, Apr 23, 2008
6. ### Chase PreuningerGuest

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

Chase Preuninger, Apr 23, 2008
7. ### Patricia ShanahanGuest

Chase Preuninger wrote:
> 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

Patricia Shanahan, Apr 23, 2008
8. ### Roedy GreenGuest

On Tue, 22 Apr 2008 14:04:42 -0700 (PDT), KyoGaSuki
<> wrote, quoted or indirectly quoted
someone who 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?)

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

The Java Glossary
http://mindprod.com

Roedy Green, Apr 23, 2008
9. ### Mark SpaceGuest

Patricia Shanahan wrote:
> Chase Preuninger wrote:
>> lets take something easy
>>
>> 6%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.

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));

Mark Space, Apr 24, 2008