Skip
# Help me understand modulus operators

(adsbygoogle = window.adsbygoogle || []).push({});

You're half way there. Lets work through one and maybe that will help:

8%3 = ?

Say we had little bags of 3 items, and we wanted to put those bags into a bucket so there were 8 items in the bucket. We could put in two whole bags, but then we would have to open up a bag and take 2 items out of a bag and put them in the bucket. So 8%3 is 2, the number of items we need to take out of a bag.

This is what the modulus operator is after - how many items must be taken out of a set.

1%2 = 1 because we have bags of two items, so we can't put in any whole bags, and so must open up a bag and take 1 out to get 1.

12%100 is 12 because we can put zero bags of 100 into a bucket sized to hold 12 items, but instead we must open the bag and put take out 12 individual items.

9%3 is 0 because we can just put 3 bags of 3 into the bucket to get 9, and never open the bags and take anything out.

Once you have the idea, you can then perform the math by taking the divisor ( 8/3=2.66 ) and truncating it (2). That is the whole number of bags we can put in. If we take that number (2) and multiply by the bag size (3) we get 6 items. So we can put in 6 items without opening a bag. We want 8 items, so 8-6 =2 is the number of items which must be removed from a bag.

As a side note, I never realized how hard it is to explain the modulus operator... I hope this helped.

Cheers!

posted by PJensen at 6:38 AM on August 8, 2006

(adsbygoogle = window.adsbygoogle || []).push({});

Post

# Help me understand modulus operators

August 8, 2006 6:17 AM Subscribe

My maths skills are rubbish. Help me understand modulus remainders

I'm working my way through some JavaScript stuff and have seen the modulus operator used quite a bit. All well and good, but it doesn't make sense. It's supposed to return the remainder after a division, so I can see why 9%3 (9÷3) is 0. But, if 12%100 is 12 (12÷100 = 0.12), why is 1%2 1 and not 5 (1÷2 = 0.5) and why does 8%3 = 2 and not 66 (8÷3 = 2.66)?

I'm working my way through some JavaScript stuff and have seen the modulus operator used quite a bit. All well and good, but it doesn't make sense. It's supposed to return the remainder after a division, so I can see why 9%3 (9÷3) is 0. But, if 12%100 is 12 (12÷100 = 0.12), why is 1%2 1 and not 5 (1÷2 = 0.5) and why does 8%3 = 2 and not 66 (8÷3 = 2.66)?

One way to think of it is "how many are left over after you've subtracted the second value from the first as many times as possible?"

So, 8%3 would be:

8-3=5. 5-3=2. 2-3 is negative, so you can't subtract anymore. You're left with 2.

1-2 is negative, so you can't subtract any. you're left with 1.

And so on.

posted by ducksauce at 6:22 AM on August 8, 2006

So, 8%3 would be:

8-3=5. 5-3=2. 2-3 is negative, so you can't subtract anymore. You're left with 2.

1-2 is negative, so you can't subtract any. you're left with 1.

And so on.

posted by ducksauce at 6:22 AM on August 8, 2006

It's not what you're dividing, it's what you have left.

8%3 = 2 because 8 = 3 + 3 + 2 = 2(3) + 2. Once you take out the 3's, you have 2 left.

9%3 = 0 because 9 = 3 + 3 + 3 + 0 = 3(3) + 0. Once you take out the 3's, you have 0 left.

1%2 = 1 because 1 = 1 + 0(2). There are no 2's, so you're just left with 1.

12%100 = 12 because 12 = 12 + 0(100). Take out your multiples of 100 and you're left with 12.

posted by Blazecock Pileon at 6:22 AM on August 8, 2006

8%3 = 2 because 8 = 3 + 3 + 2 = 2(3) + 2. Once you take out the 3's, you have 2 left.

9%3 = 0 because 9 = 3 + 3 + 3 + 0 = 3(3) + 0. Once you take out the 3's, you have 0 left.

1%2 = 1 because 1 = 1 + 0(2). There are no 2's, so you're just left with 1.

12%100 = 12 because 12 = 12 + 0(100). Take out your multiples of 100 and you're left with 12.

posted by Blazecock Pileon at 6:22 AM on August 8, 2006

To me, thinking back to the early days of learning division helps: "3 goes into 8 two times with a remainder of 2," so 8%3 = 2. Modulus arithmetic deals in integers, never decimals or fractions.

posted by davcoo at 6:25 AM on August 8, 2006

posted by davcoo at 6:25 AM on August 8, 2006

Ok I'll take a stab, avoiding the division, as you seem to be having trouble with decimal fractions.

modulo basically means "keep taking the second number away from the first number. When you can't do it any more without going into negative numbers, whatever's left is the answer".

9%3:

9 - 3 = 6

6 - 3 = 3

3 - 3 = 0

0 - 3 drops below 0, so remainder = 0

12%100:

12 - 100 drops below 0, so remainder = 12

1%2:

1 - 2 drops below 0, so remainder = 1

8%3:

8 - 3 = 5

5 - 3 = 2

2 - 3 drops below 0, so remainder = 2

Does that help any?

posted by Leon at 6:26 AM on August 8, 2006

modulo basically means "keep taking the second number away from the first number. When you can't do it any more without going into negative numbers, whatever's left is the answer".

9%3:

9 - 3 = 6

6 - 3 = 3

3 - 3 = 0

0 - 3 drops below 0, so remainder = 0

12%100:

12 - 100 drops below 0, so remainder = 12

1%2:

1 - 2 drops below 0, so remainder = 1

8%3:

8 - 3 = 5

5 - 3 = 2

2 - 3 drops below 0, so remainder = 2

Does that help any?

posted by Leon at 6:26 AM on August 8, 2006

The remainder is NOT "the part after the decimal place" (or even "the part after the decimal place times the number of non-zero decimal places" as in your examples).

GET THAT OUT OF YOUR HEAD.

Ok, good, now that it's out: Division is really repeated subtraction. When we divide 9 by 3, we keep taking away threes from nine until we have less than three left. As it happens, with nine, three "divides evenly" into none, so we're left with zero:

9 / 3 => 9 - 3 = 6; 6 - 3 = 3; 3 - 3 = 0 ; we subtracted three three times and had zero left over, so 9/ 3 = 3, remainder 0.

Now what happens if we divide 10 by 3?

10 / 3 => 10 - 3 = 7 ; 7 - 3 = 4 ; 4 - 3 = 1 ; we subtracted three three times, and have one left over so, 10 / 3 = 3, remainder 1.

The Modulus operator just gives you that remainder, so 9 % 3 = 0 and 10 % 3 = 1.

Incidentally, you use modulus operations everyday, when you use a twelve-hour clock: after hitting twelve, the clock "warps around" to one. Three o'clock PM is really 1500 hours = 15 o'clock, which is 15 % 12 = 3.

posted by orthogonality at 6:29 AM on August 8, 2006

GET THAT OUT OF YOUR HEAD.

Ok, good, now that it's out: Division is really repeated subtraction. When we divide 9 by 3, we keep taking away threes from nine until we have less than three left. As it happens, with nine, three "divides evenly" into none, so we're left with zero:

9 / 3 => 9 - 3 = 6; 6 - 3 = 3; 3 - 3 = 0 ; we subtracted three three times and had zero left over, so 9/ 3 = 3, remainder 0.

Now what happens if we divide 10 by 3?

10 / 3 => 10 - 3 = 7 ; 7 - 3 = 4 ; 4 - 3 = 1 ; we subtracted three three times, and have one left over so, 10 / 3 = 3, remainder 1.

The Modulus operator just gives you that remainder, so 9 % 3 = 0 and 10 % 3 = 1.

Incidentally, you use modulus operations everyday, when you use a twelve-hour clock: after hitting twelve, the clock "warps around" to one. Three o'clock PM is really 1500 hours = 15 o'clock, which is 15 % 12 = 3.

posted by orthogonality at 6:29 AM on August 8, 2006

Another way to think of modulo operators is to do no division and treat "X % Y" as saying "X when I define Y = 0." So if I am working modulo 4, I can start at 1 and add one every time:

1, 2, 3, 0, 1, 2, 3, 0, ...

It is like you have Y spots in a circle and you make X hops between them in the same direction. Every time you move by Y, you wind up in the same place you start.

posted by Schismatic at 6:34 AM on August 8, 2006

1, 2, 3, 0, 1, 2, 3, 0, ...

It is like you have Y spots in a circle and you make X hops between them in the same direction. Every time you move by Y, you wind up in the same place you start.

posted by Schismatic at 6:34 AM on August 8, 2006

You're half way there. Lets work through one and maybe that will help:

8%3 = ?

Say we had little bags of 3 items, and we wanted to put those bags into a bucket so there were 8 items in the bucket. We could put in two whole bags, but then we would have to open up a bag and take 2 items out of a bag and put them in the bucket. So 8%3 is 2, the number of items we need to take out of a bag.

This is what the modulus operator is after - how many items must be taken out of a set.

1%2 = 1 because we have bags of two items, so we can't put in any whole bags, and so must open up a bag and take 1 out to get 1.

12%100 is 12 because we can put zero bags of 100 into a bucket sized to hold 12 items, but instead we must open the bag and put take out 12 individual items.

9%3 is 0 because we can just put 3 bags of 3 into the bucket to get 9, and never open the bags and take anything out.

Once you have the idea, you can then perform the math by taking the divisor ( 8/3=2.66 ) and truncating it (2). That is the whole number of bags we can put in. If we take that number (2) and multiply by the bag size (3) we get 6 items. So we can put in 6 items without opening a bag. We want 8 items, so 8-6 =2 is the number of items which must be removed from a bag.

As a side note, I never realized how hard it is to explain the modulus operator... I hope this helped.

Cheers!

posted by PJensen at 6:38 AM on August 8, 2006

Rereading my circle analogy, I don't think I was very clear and I like the identifying numbers with zero approach a lot.

Let's say I'm on a circle that takes six paces to walk around. I'm told to walk nine paces, so I get going. Six paces in, I find myself back to where I began, and then I finish my task three paces later. This final distance from the starting place is the same operation as 9 % 6.

Why do I like this approach? The division and subtraction approaches define modulus perfectly well, but sometimes there are situations where you are using it to get the cyclic nature of the operator and this is then a natural analogy.

posted by Schismatic at 7:33 AM on August 8, 2006

Let's say I'm on a circle that takes six paces to walk around. I'm told to walk nine paces, so I get going. Six paces in, I find myself back to where I began, and then I finish my task three paces later. This final distance from the starting place is the same operation as 9 % 6.

Why do I like this approach? The division and subtraction approaches define modulus perfectly well, but sometimes there are situations where you are using it to get the cyclic nature of the operator and this is then a natural analogy.

posted by Schismatic at 7:33 AM on August 8, 2006

Ah ha! Gotcha.

I think, between all of the replies, you managed to clear my brain-fug. Marking every answer as best would look weird, so give yerselves a pat on the back, a round of applause and a drink of your choice.

posted by TheDonF at 7:36 AM on August 8, 2006

I think, between all of the replies, you managed to clear my brain-fug. Marking every answer as best would look weird, so give yerselves a pat on the back, a round of applause and a drink of your choice.

posted by TheDonF at 7:36 AM on August 8, 2006

This thread is closed to new comments.

3 goes into 9 three times (3x3=9) with 0 left over (remainder).

Wikipedia page

posted by handee at 6:22 AM on August 8, 2006