Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Modulo Operator Confusion, I need help to understand what it does exactly
sonesay
post Jan 11 2008, 09:27 AM
Post #1


|||[ n00b King ]|||
*********

Group: [HOSTED]
Posts: 715
Joined: 20-June 07
From: Auckland
Member No.: 45,102
myCENT:20.20



Ok I've been doing some revision and came across this operator again. I've seen it before but havent fully understood what it does or its surpose to do.

Taken from "Objects first in java 2006"

QUOTE

The modulo operator

The last method in the NumberDisplay class increments the display value by 1. It takes care that the value resets to zero when the limit is reached:

public void increment()
{
value = (value + 1) % limit;
}

This method uses the modulo operator (%). The modulo operator calculates the remainder of an integer division. For example, the result of the division

27 / 4

can be expressed in integer numbers as

result = 6 remainder = 3

The modulo operator returns just the remainder of such a division. Thus the result of the expression ( 27 % 4) would be 3.


The example it gives (27 / 4) and the increment function to me dont seem to relate and this is where I'm confused as to why or how modulo is suppose to work. The increment function will increase 'value' by 1 everytime its run untill it reaches an int 'limit' then resets to zero. Well thats how it works but I'm still confused.

For another reason the example is (27 / 4) where as the function would be ((lower number + 1) % limit ). A lower number on the left side % by limit which is a higher number.

/sigh someone help explain this to me so I can figure this out.

This post has been edited by sonesay: Jan 11 2008, 09:31 AM
Go to the top of the page
 
+Quote Post
rvalkass
post Jan 11 2008, 03:03 PM
Post #2


apt-get moo
Group Icon

Group: [MODERATOR]
Posts: 2,237
Joined: 28-May 05
From: Devon, England
Member No.: 7,593
Spam Patrol
myCENT:45.20



QUOTE(sonesay @ Jan 11 2008, 09:27 AM) *
The example it gives (27 / 4) and the increment function to me dont seem to relate and this is where I'm confused as to why or how modulo is suppose to work.


Modulo is what most people know as "child's division". When you are first taught to divide, you are told to give an answer like "6 remainder 2". Modulo returns the remainder. As far as I can tell, you understand this operation, and what is actually outputted when you use modulo? What you don't understand is how it is being used in the counter.

QUOTE(sonesay @ Jan 11 2008, 09:27 AM) *
The increment function will increase 'value' by 1 everytime its run untill it reaches an int 'limit' then resets to zero. Well thats how it works but I'm still confused.


Lets go for an example. We'll start with value set to 0 and limit set to 7, OK? Good. Below I'll list what the code does and what output you'll get. Perhaps you'll spot the pattern and why it works:
  • value = (0+1)%7 = 1%7 1/7 = 0 remainder 1 OUTPUT: 1
  • value = (1+1)%7 = 2%7 2/7 = 0 remainder 2 OUTPUT: 2
  • value = (2+1)%7 = 3%7 3/7 = 0 remainder 3 OUTPUT: 3
  • value = (3+1)%7 = 4%7 4/7 = 0 remainder 4 OUTPUT: 4
  • value = (4+1)%7 = 5%7 5/7 = 0 remainder 5 OUTPUT: 5
  • value = (5+1)%7 = 6%7 6/7 = 0 remainder 6 OUTPUT: 6
  • value = (6+1)%7 = 7%7 7/7 = 1 remainder 0 OUTPUT: 0
  • value = (0+1)%7 = 1%7 8/7 = 0 remainder 1 OUTPUT: 1
  • Continuing in an infinite loop...
As value is increased each time, the number you are dividing by 7 also increases. As this happens, the remainder you get also increases. When you reach a number that is in the 7 times table, there is no remainder (as it divides exactly by 7) so the counter appears to reset to 0. This also resets 'value' to 0, and the sequence begins again.

QUOTE(sonesay @ Jan 11 2008, 09:27 AM) *
For another reason the example is (27 / 4) where as the function would be ((lower number + 1) % limit ). A lower number on the left side % by limit which is a higher number.


The lower number divided by a large number is shown in my example above. The large number goes into the small number 0 times, leaving a remainder equal to the small number.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. How To Resize/host Images!(15)
  2. Girl I Can't Understand(39)
  3. Where Can I Get Uni.cc Domain Names?(3)
  4. Confusion - A Recalled Song Due To The Mistakes.(2)
  5. Confusion About Hosting Features(3)
  6. Css So Only Ie Will Understand It(5)
  7. Mmorpgs - Is There An Easy Way To Make Them?(9)
  8. Confusion About Adsense Cpm ?(1)
  9. Gnu And Confusion...(0)
  10. Nose Hair(9)
  11. A Question About Dinosaurs And Christians(4)
  12. Did Amd Buy Ati ?(4)
  13. Confusion In The Matrix Revolutions(3)
  14. Mascot Phobia(11)
  15. I Don't Understand Hosting Credits(8)
  1. My Confusion About Dead/non-existence(12)
  2. I Just Want To Understand Better(2)
  3. I Dont Understand On How To Use This Forum?(4)
  4. A Little Help With My Cat?(4)
  5. Why Must We Learn Object Oriented Programming(8)
  6. Java Exercise Help(2)
  7. The Right Web Album/image/photo Album Host?(9)
  8. Contact.pl Confusion(0)
  9. I Hate My Teacher!(11)
  10. Credits?(2)
  11. Muscle Confusion(1)
  12. I Really Do Not Understand(6)
  13. God With Heaven And Hell(0)


 



- Lo-Fi Version Time is now: 22nd November 2008 - 07:39 PM