Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > the dreaded %

Reply
Thread Tools

the dreaded %

 
 
KyoGaSuki
Guest
Posts: n/a
 
      04-22-2008
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?)
 
Reply With Quote
 
 
 
 
Joshua Cranmer
Guest
Posts: n/a
 
      04-22-2008
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
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-22-2008
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
 
Reply With Quote
 
Thomas Fritsch
Guest
Posts: n/a
 
      04-22-2008
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-23-2008

On Tue, 22 Apr 2008 14:04:42 -0700 (PDT), KyoGaSuki
<(E-Mail Removed)> 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
 
Reply With Quote
 
Chase Preuninger
Guest
Posts: n/a
 
      04-23-2008
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
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      04-23-2008
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

 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-23-2008
On Tue, 22 Apr 2008 14:04:42 -0700 (PDT), KyoGaSuki
<(E-Mail Removed)> 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
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      04-24-2008
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));

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Latest MyDoom Virus May Signal Dreaded 'Zero Day' Attack AeoN Computer Support 0 11-13-2004 09:43 PM
TB, Sunbird, and the dreaded Pocket PC - OpenSource help needed! Kneewax Firefox 1 10-13-2004 06:48 PM
The dreaded asp.clipboard =?Utf-8?B?QmlsbCBCZWxsaXZlYXU=?= ASP .Net 3 05-02-2004 04:05 PM
dreaded OE losing emails? asifi'dtell Computer Support 3 04-23-2004 12:44 PM
The dreaded Rex X Golden Oldie Computer Support 6 09-30-2003 02:14 AM



Advertisments