Results 1 to 9 of 9
  1. #1
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default I need clarity on Modulus\remainder operator

    I was busy with some mock exam & came across a modulus operator. I've always understood it when the numerator is bigger than the denominator, like 5%2 will give me 1.... but now I was faced with another one, the numerator was bigger... it was 1%3 and it gave me 1. In my mind I've made my own theories to understand this, but I think it is best to ask if you don't know.

    how did 1%3 become 1?

    Your help will be highly appreciated... I do not want to assume things

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,979
    Rep Power
    5

    Default Re: I need clarity on Modulus\remainder operator

    Ah the wonders of integer math. See it like this:

    5/2 = 2, because 2 fits twice inside 5.
    5%2 = 1, because 2 fits twice inside 5 and that has a remainder of 1 (5 - 2 -2).

    13/5 = 2 because 5 fits twice inside 13
    13%5 = 3, because 5 fits twice inside 13 (total 10) and then there is 3 left over

    1%3 = 1 because 3 fits zero times inside 1, which leaves a remainder of 1.

    Note: done from memory, so I may have goofed somewhere with one of the numbers.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: I need clarity on Modulus\remainder operator

    The modulus or modulo operator is simply to come up with the remainder of a division. So 1/3 gives you a quotient of 0, and the remainder of 1. Wikipedia at Modulo operation - Wikipedia, the free encyclopedia has a pretty robust description of this.

  4. #4
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: I need clarity on Modulus\remainder operator

    Thanx alot guys... and to add to your answers I've just found thisWhat is 1 mod 5? and 1 mod 3? - Yahoo Answers

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,597
    Rep Power
    5

    Default Re: I need clarity on Modulus\remainder operator

    Just be careful with the 1 mod 5 notation. That is very similar to number theory notation where the modulus can be many different values. In the JLS, % is actually referred to as the remainder operator. If you want the complete details, check it out here. Remainder Operator %

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: I need clarity on Modulus\remainder operator

    The % operator is defined as: a%b == a-b*(a/b), where / is the integer division operator (this is also defined for negative numbers).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: I need clarity on Modulus\remainder operator

    Isn't it funny how we ignore the JLS I'm looking at it & I'm not sure if I'll be able to grasp all there is to know about remainder operator in one moment
    I'm busy preparing for the OCAJP 7 exam which I plan to take on the 24th of this month

    Thanks again to all of you

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,513
    Blog Entries
    7
    Rep Power
    20

    Default Re: I need clarity on Modulus\remainder operator

    Quote Originally Posted by Kagiso View Post
    Isn't it funny how we ignore the JLS
    How strange, because I took my definition (see reply #6) straight from the JLS (see 15.17.3 Remainder Operator %).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: I need clarity on Modulus\remainder operator

    Quote Originally Posted by JosAH View Post
    How strange, because I took my definition (see reply #6) straight from the JLS (see 15.17.3 Remainder Operator %).

    kind regards,

    Jos
    What I was trying to say is... I didn't think of looking at the JLS until both you & Jim led me to it... English is not my language & I didn't mean to offend

Similar Threads

  1. Unary operator and binary operator
    By suhaas.mohandos@gmail.com in forum New To Java
    Replies: 10
    Last Post: 08-27-2013, 04:39 PM
  2. Question about how Modulus works
    By calistoknight in forum New To Java
    Replies: 2
    Last Post: 09-24-2012, 12:06 PM
  3. Replies: 5
    Last Post: 01-21-2012, 11:02 PM
  4. modulus value keeps returning a zero value
    By leoshiner in forum New To Java
    Replies: 3
    Last Post: 10-24-2010, 04:11 PM
  5. [SOLVED] Modulus/Remainder results
    By antgaudi in forum New To Java
    Replies: 9
    Last Post: 10-13-2008, 03:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •