Fantastic Modulo(s?) and how to use It(them?)

Best title ever.

Anyway, very few people know what Modulo is. (Actually, by now everyone on the forum’ll know it and this won’t be helpful for months)(I’m kidding) so I made this topic to teach them.

I am going to show you a few basic uses for Modulo.
But first, I’m going to say that Modulo is the remainder of a division problem. So for example
10 % 2
Would equal 0. And
5 % 2
Would equal 1.

So now that you know what Modulo is, you will understand how these examples work.

Checking if a number is a multiple of something

If you have a variable which changes, and you need to see if it’s a multiple of something, use this code:

A variation of this can be used - depending on the color - to make a brick wall or a wood panel floor. I’m not going to say how to do this, but it involves subtraction and ‘or’ in check once ifs.

Leveling System

Modulo can be used for a leveling system. For example, if you play Minecraft, it can be used to make a less complex version of that leveling system. (Gary Banner) Just add ways to get experience.

And I could only think of those two examples, but I will add more. In fact, this is on global edit, so add you own examples (in details folders) below the line.

How to check if a number is odd or even
if number % 2 == 0:
 // the number is even
 // the number is odd

Posted by @MR.GAM3R


Wow great topic idea and awesome examples!!

:joy: I think people will understand however you put the title, but maybe singular makes it easier to grasp? I don't know :stuck_out_tongue:

I think there is a use that I've heard from others – rounding down a number. I'll try editing that in :smiley: Oh I don't think it works unfortunately.


Hehe. I used to know how to do that. @Valgo I think knows.


This is technically not true.
-3 % 4 =1
The remainder of -3/4 is -3.


That made me think about something. Is modular arithmetic slightly different then? :thinking:


@t1_hopscotch Rounding down doesn’t require modulo.

Round_Down(x) = Round(x-0.5)
This works for both positive & negative numbers.

Truncation can be done with modulo. For positive numbers it’s the same result, with the difference being trunc(x) moves closer to 0, so for negative numbers trunc(x) rounds up. Truncation is just keeping the integer portion of a number so is the same as an integer function or int(x).

I’ll post a explanation / derivation for trunc(x) in a few minutes. But I just tested and at least with the current TestFlight app version modulo is not working right for non-integers. When modulo was first introduced it had a similar problem with non-integers, then THT fixed it, now it’s broken again.


Truncation using modulo
(If/when it works right)

Just to restate, truncation is rounding towards zero or just keeping the integer portion of a number.

Definition of modulo:

where the bit with the "L"s is truncation

So we can solve for trunc(x) by letting y=1

x mod 1 = x - 1 * trunc(x/1)
x mod 1 = x - trunc(x)
trunc(x) = x - (x mod 1)

also int(x) = x - (x mod 1)

*but again, at least with the current TestFlight app version, mod doesn’t work right for non-integers so this doesn’t work. The bug has been reported here.

[Edit] Just to follow up, at some point the Modulo block was fixed (tested 3/16/19 with Player version f59fc18) so it handles non-integers correctly. So the above Truncate “function” now works as expects.


Oh. Thank you.


Ohhh okay thank you a lot @ThinBuffalo :smile:

And yeah this was the way I had been thinking — and ran into that problem too:


That's it. :smiley:


@BB-Box, here you go.


I see this must be confusing :blush:

As a start — modulo gives the remainder when one number is divided by another. E.g.

9 ÷ 2 = 4 remainder 1

1 is the remainder, so 9 % 2 = 1

  • Another example:

17 ÷ 5 = 3 remainder 2

2 is the remainder here, so 17 % 5 = 2


What is the "r" standing for?


r stands for remainder :smiley: I will edit that


But how do you get the reminder?


In 9/2=4r1

Is the reminder there because 8/2=4 and there is 1 between 8 and 9?


Yeah that was it :smiley:

I was going to make an example with dividing a bag of balls, or lollies. :candy: Say you have 20 lollies and you want to divide it equally between 6 friends.

You will end up with the friends having 6 lollies each, but there will be 2 lollies left over (or as remainder).


Oh, I understand. Thanks a lot.


@t1_hopscotch what is happening here:

This is from creations of a noobs darth Vader pixel art made my clones.


That is a good question! :smiley:

So it looks like the program is checking whether variable % 2 is equal to 1.

Another thing that modulo can be used for is to check whether a number divides perfectly into another number (no remainders). 15 % 5 = 0 because there is zero remainder when you divide 15 by 5.

If a number % 2 equals 0, then that number is perfectly divisible by 2. However, this seems to be checking if the variable % 2 = 1 — that is, if the number divided by 2 will give a remainder of 1. Do you want to have a think about what that might be? :thinking:

(Short explanation: Odd numbers are not divisible by 2 — when you divide an odd number by 2, there is always a remainder of 1. Odd number % 2 = 1. So the code is checking whether the CIID variable is an odd number :smiley:) I think I forgot to mention that modulo is when you try to divide numbers and keep the remainder, rather than change it into a fraction/decimal.


Ok, thanks a lot. That was what I was thinking but I wasn't sure.