Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   the dreaded % (http://www.velocityreviews.com/forums/t607088-the-dreaded.html)

KyoGaSuki 04-22-2008 09:04 PM

the dreaded %
 
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?)

Joshua Cranmer 04-22-2008 09:12 PM

Re: the dreaded %
 
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

Patricia Shanahan 04-22-2008 09:53 PM

Re: the dreaded %
 
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/t....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

Thomas Fritsch 04-22-2008 11:12 PM

Re: the dreaded %
 
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

Roedy Green 04-23-2008 01:56 AM

Re: the dreaded %
 

On Tue, 22 Apr 2008 14:04:42 -0700 (PDT), KyoGaSuki
<jrockgadaisukidayou@yahoo.co.jp> wrote, quoted or indirectly quoted
someone who said :

> can do the 5/2 =
>2.5 ...but what is 12%5?


see http://mindprod.com/jgloss/modulus.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Chase Preuninger 04-23-2008 02:08 AM

Re: the dreaded %
 
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

Patricia Shanahan 04-23-2008 02:52 AM

Re: the dreaded %
 
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


Roedy Green 04-23-2008 10:40 AM

Re: the dreaded %
 
On Tue, 22 Apr 2008 14:04:42 -0700 (PDT), KyoGaSuki
<jrockgadaisukidayou@yahoo.co.jp> 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.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Mark Space 04-24-2008 12:37 AM

Re: the dreaded %
 
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));



All times are GMT. The time now is 11:15 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.