mod of a negative number

Discussion in 'Java' started by gk, Nov 5, 2006.

  1. gk

    gk Guest

    Int a = -5; Int b = -2;
    System.out.println(a % b); // -1

    how this is working ?
     
    gk, Nov 5, 2006
    #1
    1. Advertisements

  2. gk

    Lars Enderin Guest

    gk skrev:
    (How does this work?)
    According to the definition of the % operator -1 is the rest after
    taking out a multiple (-4) of -2 from -5.
     
    Lars Enderin, Nov 5, 2006
    #2
    1. Advertisements

  3. Strictly speaking, there is no "mod" operator in Java. % is defined to
    be "remainder". It is the same as modulo for positive operands.

    For integer operands, % is designed to maintain the identity:
    (a/b)*b+(a%b) is equal to a.

    Java integer division rounds towards zero, so -5/-2 is 2.

    ( (-5)/(-2) ) * (-2) + (-1) is -5.

    See the JLS,
    http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#239829

    Patricia
     
    Patricia Shanahan, Nov 5, 2006
    #3
  4. gk

    gk Guest

    true.....but -2<-1 , so we can divide it further theoretically ...is
    not it ? hmm...but if we keep on dividing its going to be an infinite
    loop.
     
    gk, Nov 5, 2006
    #4
  5. A nitpick question: Is the division ( (8)/(3) ) also 2 in Java?
    If so, why is this be called "rounding"?
     
    Richard F.L.R.Snashall, Nov 5, 2006
    #5
  6. gk

    Lee Weiner Guest

    It is not called rounding. It is called integer division.
     
    Lee Weiner, Nov 5, 2006
    #6
  7. I usually use "rounding" in the sense in which it is used in e.g. the
    IEEE 754 standard, to mean modifying the infinitely precise result of a
    calculation to fit in the destination's format.

    In that usage, it includes rounding directed rounding, such as rounding
    towards zero, as well as the various flavors of round to nearest. Some
    people use "truncation" when the rounding is towards either zero or
    negative infinity.

    Patricia
     
    Patricia Shanahan, Nov 5, 2006
    #7
  8. gk

    gk Guest

    nice explanation!
     
    gk, Nov 5, 2006
    #8
  9. gk

    kstahmer

    Joined:
    May 13, 2010
    Messages:
    1
    Likes Received:
    0
    Illuminating post - thanks.

    The JLS example was also illuminating:

    5 % 3 produces 2
    5 % (-3) produces 2
    (-5) % 3 produces -2
    (-5) % (-3) produces -2

    This got me thinking...

    Suppose a and b are integers.

    We have two special cases:
    1. If b == 0, then a % b is NaN (JLS specification).
    2. If nonzero b divides a, then a % b == 0 (In particular, every nonzero b divides 0, so 0 % b == 0).
    Which leaves us with nonzero b does not divide a.

    Then

    a % b > 0 if a > 0

    and

    a % b < 0 if a < 0.

    Hence, if nonzero b does not divide a, then the sign of a % b equals the sign of a.

    You can see this formally by noting:

    (a / b) * b + a % b == a

    implies

    a % b == a - (a / b) * b

    and if nonzero b does not divide a, then

    Math.abs((a / b) * b) < Math.abs(a),

    due integer division rounding towards zero.
     
    kstahmer, May 13, 2010
    #9
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.