Remainder/Modulus Operator Confusion and explanation!

The first time I used the remainder operator I got really confused, specially when the left value is smaller than the right value because it always returns the left value no matter what. For instance:
30 % 60 = 30

If we solve the division it will give us a remainder of 0, because 30 / 60 = 0.5, Why it is returning 30?

I searched the internet and I found this weird operation in the microsoft docs:
a % b = a - (a / b) * b

Even if you’re not a math genius you can clearly see that the operation is silly at best, the result will always be 0, so, What is missing?

Well, after almost an hour of reading I found this: a - trunc(a / b) * b
The operation truncates(floor) the division value, so, we get this:
30 - trunc(30 / 60) * 60
30 - trunc(0.5) * 60
30 - 0 * 60
30 - 0 = 30

If we use the same operation with the challenge (63 % 4):
63 - trunc(63 / 4) * 4
63 - trunc(15.75) * 4
63 - 15 * 4;
63 - 60 = 3

Why is this important?
This means that the Remainder operator can be really versatile, you can use it for timers:

Time % 60 = Time in seconds or minutes. Perfect for display purposes.

You can use it to count turns and assign them as the lesson suggests:

TotalTurns % NumberOfPlayers
Turn ends
Total turns + 1

You can get really creative and use it to tell an enemy which ability will it use next:

Abilitiy to use = Game time % Total number of abilities
Array of abilities[ability to use]

Of course, there are far more uses for this, but it really helps to understand what is going on in the background, I only used the operator for timers, now that I know exactly what it’s going on, my mind immediatly flooded with ideas and ways to optimize and clean my code.

2 Likes

Great explanation @Yee :slight_smile:
The remainder operator really is an incredibly useful tool to have in your coding arsenal. It’s great to see you taking the extra time to really dig in to how it works, so that you can understand it better.

You’re right that it has a myriad of applications and can make using arrays a lot easier.

1 Like

For me it’s like …

20%60 To get 60, you have already 20 of the first 60. So it returns ‘20’.
80%60 To get 60, you have already 20 of the second 60. First 60 is full. So it returns ‘20’.

3 Likes

Yeh I totally spent an hour researching why you get the result when the dividend is less than the divisor i.e. 20 % 60 = remainder 20 …or in you example:

0 % 0 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0

I think you’ve made a bit of a cognitive leap in presuming what the learner knows, it’s been a long time since some of have done math! I think the video could use a quick explanation of this as a stepping stone so the example makes sense.

The rest of the video/example is great, thank you.

2 Likes

@Fireal, thank you for your feedback. I’ll certainly review this lecture and see if I can clarify some of the explanations.

If you think of the division as appearing on a circle with n-steps, it can help explain why things work the way they do.
You’re really looking at how many times you can completely walk through each step on the circle and if you can’t make it all the way around, the remainder is how many steps you still have left (that you haven’t used yet).

So if you took 20 % 60, you could say that this is zero trips around the circle (minutes) with 20 steps (seconds) left over.

You could work this out as:
20 % 60
20 ÷ 60 = 0.3…
0.3… x 60 = 20

Once you hit 60, then you have 1 complete minute with no remainder, so you’re stepping around the circle exactly once;
60 % 60 = 0
60 / 60 = 0

If you have larger numbers then you will have some whole number of minutes plus a some number of seconds remaining.

So for 80 seconds you have;
80 % 60 = 1.3…
1.3… - 1 = 0.3…
0.3… x 60 = 20

So 80 seconds is 1 full minute plus a remainder of 20 seconds.

I hope that explanation helps.
Please do continue to leave feedback, I’m always happy to receive it and it really helps us to improve the course content :slight_smile:

4 Likes

Gary this is a brilliant way of explaining it, you should totally slot this in as the example illustration!

Thanks for the reply, much appreciated.

2 Likes

For anyone else who got stuck on this, these links were helpful…

https://www.mathsisfun.com/numbers/division-remainder.html

https://www.omnicalculator.com/math/remainder

1 Like

Hi Fireal, based on feedback I’ve now reworked this video to hopefully make it easier to follow.

1 Like