the dreaded %

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

  1. KyoGaSuki

    KyoGaSuki Guest

    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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. 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
    #3
  4. 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
    #4
  5. KyoGaSuki

    Roedy Green Guest

    Roedy Green, Apr 23, 2008
    #5
  6. 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
    #6
  7. 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
    #7
  8. KyoGaSuki

    Roedy Green Guest

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

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
     
    Roedy Green, Apr 23, 2008
    #8
  9. KyoGaSuki

    Mark Space Guest

    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
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QmlsbCBCZWxsaXZlYXU=?=

    The dreaded asp.clipboard

    =?Utf-8?B?QmlsbCBCZWxsaXZlYXU=?=, May 1, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    460
    Alvin Bruney [MVP]
    May 2, 2004
  2. caustik

    The dreaded question

    caustik, Jul 2, 2003, in forum: C++
    Replies:
    28
    Views:
    793
  3. Alexander Stippler

    virtual inheritance / dreaded diamond problem

    Alexander Stippler, Jul 14, 2003, in forum: C++
    Replies:
    0
    Views:
    1,892
    Alexander Stippler
    Jul 14, 2003
  4. Alexander Stippler

    virtual inheritance / dreaded diamond again

    Alexander Stippler, Aug 26, 2003, in forum: C++
    Replies:
    1
    Views:
    398
    Ron Natalie
    Aug 26, 2003
  5. John Hunter

    the dreaded double fork

    John Hunter, May 5, 2004, in forum: Python
    Replies:
    4
    Views:
    4,952
    Doug Holton
    May 6, 2004
Loading...

Share This Page