Modulo


#1

Good Day Everyone!

I feel that the lessons don’t provide enough explanations to comprehend the materials.

I am having difficulties to understand this topic.

Prints 4 because 29 / 5 is 5 with a remainder of 4

print(29 % 5)

Prints 2 because 32 / 3 is 10 with a remainder of 2

print(32 % 3)

Modulo by 2 returns 0 for even numbers and 1 for odd numbers

Prints 0

print(44 % 2)

Here, we use the modulo operator to find the remainder of division operations. We see that 29 % 5 equals 4, 32 % 3 equals 2, and 44 % 2 equals 0.

I see why 44 % 2 equals 0, but the other two don’t seem to be right.
29/5, i would think the remainder is 8
32/2 , I would think the remainder is 7


#2

for 29%5 how do you get to 8?

for 32%3 how could the result be 7?

for example for 29%5, the lesson says:

Prints 4 because 29 / 5 is 5 with a remainder of 4

the biggest possible (whole) division for 29 / 5 is 25. So then we have 4 left (29 - 25)


#3

29 % 5 equals 5 with a remainder 8. that’s where the comes from
32 % 3 equals 10 with a remainder 66 when rounded that brings it to 7


#4

I think you might be confusing the integer remainder of the division, which is what the modulo operator gives you, with the decimal value of computing the division which the math would actually return.

29 / 5 = 5.8 is correct, but the remainder in this result is not 8.

Your remainder, expressed as a decimal, is 0.8, and 0.8 * 5 = 4. That’s why 29 % 5 returns the value of 4.

Does that make it clearer?


#5

this has nothing to do with the decimal value of the division. let go of this way of thinking, we are only looking at whole numbers

if we have the table of 5:
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30

the biggest whole number lesser then or equal to 29 is 25, so 25 is the possible largest division. to get the remainder, we have to number - biggest possible division. 29 - 25 = 4


#6

Got it. Thank you much!


#7

If you ever need the decimal value you can do this 29/5. or 29./5 both equal 5.8

Note that 29/5 will give you 5. If you want the decimal value you have to turn one or both of the divisors into a float


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.