How Modulo Works


#1

Math and the modulo

Explain how modulo works


#2

Well consider integer division meaning no fraction whether 3/4 or 0.75. Now if you divide let's say 7/5 you have 1 because 5 fits one time in 7 and you have a rest/remainder of 2. Now what modulo does is ignoring the result of 1 and instead just giving you the remainder (2).


#3

It took me ages to understand what modulo is.

Here is the bread and butter version:

15 % 8 = 7

How exactly? Well I guess Codecademy's tutorial for #20 is a little unclear for those who didn't really care about maths in school (such as me! :wink: )

In order to find out the answer you have to follow the following formulae:

15 divided by 8 = 1.8... blah blah

You then clear your calculator and take the 1 (ignore the decimal values)

1 X the 2nd number (in this case the 2nd number is 8) so 1 X 8 = 8.

Finally all you have left to do is do the First number in the original equation, minus the result you just got.

Funnily enough, its actually 15 - 8, which results in 7.

Aaaand, therefore, 15 % 8 = 7 :smiley:

Make sense?


#4

@wannabecodingguru
Well no. 15/8 is 1.875 so it is rather 1.9 also if you take 99%2 it would be 49.5 but 99-2*5 wouldn't be the result of 99%2 which should be 1.

What might work is to do the following:

15/8 is 1.875 get rid of the decimal fraction and you get 1. Now you take this value times the value that you use for your division (8) and subtract it from the original value:

15 - 1*8 = 7

which also works for 99%2

99 - 2*49 = 1

PS: I don't think that this is much easier but if you like it.