# Modulo?

**URL:** <https://www.ruby-forum.com/t/modulo/163316>\
**Category:** Ruby\
**Created:** [March 19, 2009, 6:51am UTC](https://www.ruby-forum.com/t/modulo/163316 "2009-03-19T06:51:57Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 6:51am UTC](https://www.ruby-forum.com/t/modulo/163316/1 "2009-03-19T06:51:57Z")

</div>

Maybe i need to get some sleep, but can someone explain how modulos  
work?

Thanks

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 7:18am UTC](https://www.ruby-forum.com/t/modulo/163316/2 "2009-03-19T07:18:08Z")

</div>

Zayd C. wrote:

> Maybe i need to get some sleep, but can someone explain how modulos  
> work?
> 
> Thanks

result = 7 % 3  
puts result

–output:–  
1

7 has two 3’s in it, and after removing those two 3’s from 7, the  
remainder is 1.

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 2:50pm UTC](https://www.ruby-forum.com/t/modulo/163316/3 "2009-03-19T14:50:34Z")

</div>

Zayd C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> Maybe i need to get some sleep, but can someone explain how modulos  
> work?

From “Discrete Mathematics by Rosen”:

"Let a be an integer and m be a positive integer. We denote by a mod m  
the remainder when a is divided by m.

It follows from the definition of remainder that a mod m is the  
integer r such that:

a = q \* m + r and 0 \<= r \< m "

This is all assuming you didn’t type an ‘o’ when you meant ‘e’ 😉

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 3:26pm UTC](https://www.ruby-forum.com/t/modulo/163316/4 "2009-03-19T15:26:25Z")

</div>

On 19.03.2009 06:50, Zayd C. wrote:

> Maybe i need to get some sleep, but can someone explain how modulos  
> work?

> **[LMGTFY](https://lmgtfy.com/?q=modulo)**
>
> Let Me Google That For You (LMGTFY): for all those people who find it more convenient to bother you with their question rather than search it for themselves.

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 3:43pm UTC](https://www.ruby-forum.com/t/modulo/163316/5 "2009-03-19T15:43:33Z")

</div>

On Mar 19, 2009, at 10:22 AM, Robert K. wrote:

> On 19.03.2009 06:50, Zayd C. wrote:
> 
> > Maybe i need to get some sleep, but can someone explain how modulos  
> > work?
> 
> [LMGTFY](http://lmgtfy.com/?q=modulo)

This seems completely unnecessary. There was already a great response  
from Brian who not only directly addressed the “modulos”, but also  
picked up and pointed out (subtly) that the question might have been  
about “modules”. Something that makes perfect sense, but I certainly  
didn’t see that possibility.

And did you Google modulo or module yourself to see how useful the  
result really is? If you’re going to simply shout lmgtfy, at least put  
“ruby” in there, too (well, for module, not for modulo 😉

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 4:05pm UTC](https://www.ruby-forum.com/t/modulo/163316/6 "2009-03-19T16:05:56Z")

</div>

On 19.03.2009 15:40, Rob B. wrote:

> And did you Google modulo or module yourself to see how useful the  
> result really is?

I did.

robert

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 9:02pm UTC](https://www.ruby-forum.com/t/modulo/163316/7 "2009-03-19T21:02:32Z")

</div>

> Thanks guys,(singing) I can see clearly now the rain is gone :). Maybe I  
> should have been more clear and added the % sign when mentioning modulo,  
> so I wouldn’t confuse anyone thinking I meant modules 🙂
> 
> Thanks

Though there is one thing I would like to point out: 0 % 7 = 0  
So ‘remainder’ is not strictly true

Michael

# ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission.

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 9:53pm UTC](https://www.ruby-forum.com/t/modulo/163316/8 "2009-03-19T21:53:00Z")

</div>

Michael M. wrote:

> Though there is one thing I would like to point out: 0 % 7 = 0  
> So ‘remainder’ is not strictly true

Sorry I don’t follow you. What’s the remainder of 0/7 if not 0?  
0-7\*0 is 0, is it not?

Confused,  
Sebastian

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 4:15pm UTC](https://www.ruby-forum.com/t/modulo/163316/9 "2009-03-19T16:15:02Z")

</div>

Rob B. wrote:

> On Mar 19, 2009, at 10:22 AM, Robert K. wrote:
> 
> > On 19.03.2009 06:50, Zayd C. wrote:
> > 
> > > Maybe i need to get some sleep, but can someone explain how modulos  
> > > work?
> > 
> > [LMGTFY](http://lmgtfy.com/?q=modulo)
> 
> This seems completely unnecessary. There was already a great response  
> from Brian who not only directly addressed the “modulos”, but also  
> picked up and pointed out (subtly) that the question might have been  
> about “modules”. Something that makes perfect sense, but I certainly  
> didn’t see that possibility.
> 
> And did you Google modulo or module yourself to see how useful the  
> result really is? If you’re going to simply shout lmgtfy, at least put  
> “ruby” in there, too (well, for module, not for modulo 😉
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks guys,(singing) I can see clearly now the rain is gone :). Maybe I  
should have been more clear and added the % sign when mentioning modulo,  
so I wouldn’t confuse anyone thinking I meant modules 🙂

Thanks

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 10:45pm UTC](https://www.ruby-forum.com/t/modulo/163316/10 "2009-03-19T22:45:49Z")

</div>

On Mar 19, 2009, at 5:07 PM, Michael M. wrote:

> > Confused,
> 
> Michael

[I hope this survives email formatting…]

```
    __0_r_0_
  7 ) 0

```

0\*7 =\> -0  
==  
0

Just because people can’t understand division and remainders isn’t  
enough to keep them away from technical discussions. The original  
response (which I deleted months ago [or was that yesterday?]) had an  
accurate definition.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 11:06pm UTC](https://www.ruby-forum.com/t/modulo/163316/11 "2009-03-19T23:06:56Z")

</div>

Michael M. wrote:

> Many people I know and work with simplify the modulo operator to  
> themselves as remainder, so mentally (whether or not it is correct)  
> assume 0/7 = 0 r 7

Maybe I’m slow, but I don’t get it.  
You’re saying that many people assume that x % y is the same as the  
remainder  
of dividing x by y, right? I don’t see anything wrong with that.  
If I understand you correctly, you’re also saying that this assumption  
is  
wrong in the case of 0%7. I don’t understand why that should be the  
case. The  
remainder of 0/7 is 0, right? And 0%7 is also 0, so where’s the problem?

Still confused,  
Sebastian

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 11:18pm UTC](https://www.ruby-forum.com/t/modulo/163316/12 "2009-03-19T23:18:39Z")

</div>

Sebastian H. wrote:

> If I understand you correctly, you’re also saying that this assumption is  
> wrong in the case of 0%7. I don’t understand why that should be the case. The  
> remainder of 0/7 is 0, right? And 0%7 is also 0, so where’s the problem?
> 
> Still confused,  
> Sebastian

Sorry for confusing everyone here, I just know of a particular case  
where 0%7 = 7 was assumed. I was trying to stop this happening again,  
but I think I’ve caused more confusion than it’s worth. Sorry folks.  
Ignore my post and you’ll sleep more easily.

Michael

# ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission.

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 10:11pm UTC](https://www.ruby-forum.com/t/modulo/163316/13 "2009-03-19T22:11:12Z")

</div>

Sebastian H. wrote:

> Sebastian

Many people I know and work with simplify the modulo operator to  
themselves as remainder, so mentally (whether or not it is correct)  
assume 0/7 = 0 r 7  
I am just making an explicit example of this not necessarily obvious  
case. It’s totally fine when one knows the semantics of modulo, it’s  
the simplification to remainder that many people make that causes  
problems here.

Michael

# ======================================================================= This email, including any attachments, is only for the intended addressee. It is subject to copyright, is confidential and may be the subject of legal or other privilege, none of which is waived or lost by reason of this transmission. If the receiver is not the intended addressee, please accept our apologies, notify us by return, delete all copies and perform no other act on the email. Unfortunately, we cannot warrant that the email has not been altered or corrupted during transmission.

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 20, 2009, 3:10am UTC](https://www.ruby-forum.com/t/modulo/163316/14 "2009-03-20T03:10:55Z")

</div>

On 3/19/09 6:03 PM, Sebastian H. wrote:

> remainder of 0/7 is 0, right? And 0%7 is also 0, so where’s the problem?  
> Going by the usual definition of “remainder”, there is a difference  
> between modulo and remainder when negative numbers get involved.

remainder(a,b) = a - trunc(a/b) \* b  
modulo(a,b) = a - floor(a/b) \* b

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 19, 2009, 11:25pm UTC](https://www.ruby-forum.com/t/modulo/163316/15 "2009-03-19T23:25:42Z")

</div>

Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> On 19.03.2009 06:50, Zayd C. wrote:
> 
> > Maybe i need to get some sleep, but can someone explain how modulos  
> > work?
> 
> [LMGTFY](http://lmgtfy.com/?q=modulo)

That is _awesome_ ! Sponsored by “Backpack” - interesting.

---

<div class="post-metadata">

**Author:** ![devrubygem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ac8455/32.png) [@devrubygem](https://www.ruby-forum.com/u/devrubygem)\
**Post date:** [March 20, 2009, 3:58am UTC](https://www.ruby-forum.com/t/modulo/163316/16 "2009-03-20T03:58:59Z")

</div>

On Mar 19, 5:15 pm, Michael M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sorry for confusing everyone here, I just know of a particular case  
> where 0%7 = 7 was assumed.

This is confusing. As in 0%7 = 7 is very confusing.

A mod B shouldn’t have a result that’s equal to or greater than B. If  
that happens, you take a B out until you can’t anymore. The remainder  
when A is divided by B is the same thing. If you end up with something  
greater than B, you stopped too soon.

As John W. Kennedy pointed out, the difference between modulo and a  
simple remainder comes up when dealing with negative numbers.
