[Tutorial] Modulo Operator
#1

Modulo Operator in Pawn



Preface
I'd like to begin to say that this is not an ordinary pawn scripting tutorial. This is a simple tutorial covering the modulo operator. When I searched on the forums with the word "modulo" on thread search I only found three threads where one was a tutorial in Portuguese, and two where "help me" threads so I decided to write this, even though if you guys find it bad, I tried my best to explain with my own words and with my knowledge from last semesters "Discrete Mathematics". This tutorial will most be math but "format" might show up.

Skip to next title to get to know how to use modulo in pawn language.



Mod what?
Modulo is an operator in most scripting languages and in mathematics just like addition (+), subtraction (-), multiplication (*) or other operators. They all have different functions. The modulo operator gives us the so called "rest" from a division. In the world we live in, we have no modulo. That is although wrong, we do have a mod but it's infinity. This is where it begins to struggle with most people. The mod tells us when to stop counting and start back again from 0.

Let's jump into some examples before it gets too annoying:
Code:
11 mod 6 ≡ 5
12 mod 6 ≡ 0
13 mod 6 ≡ 1
Figure 1
Wait wait what did just happen? Well let me explain before you go and report me to SA-MP for bad tutorial: When you use modulo you are dividing the number in front of the word "mod" with the number after "mod".
Code:
11/6 = 1,8333333333333333333333333333333
12/6 = 2
13/6 = 2,1666666666666666666666666666667
Figure 2
But that is not the what you wrote above? Why not? Because division and modulo are two different things but modulo wouldn't be explainable without division so I am using this method to explain modulo. To continue, what you do is that you use the fractional part to calculate the rest. Now you're asking what a fractional part is, look below:
Code:
Fractional part examples:
0.5 = Fractional part is 0.5
1.2 = Fractional part is 0.2 
5.852 = Fractional part is 0.852
1000000.52 = Fractional part is 0.52
Figure 3
It is everything after the comma sign. If you have larger numbers you will have to multiply the "fractional part" with the modulo number after removing anything to the left of the comma sign and then you will have the rest.
Code:
11/6 = 1,8333333333333333333333333333333
1,8333333333333333333333333333333 - 1 = 0,8333333333333333333333333333333
0,8333333333333333333333333333333 * 6 = 5 (rounded)
Answer: 11 mod 6 ≡ 5

12/6 = 2
2 - 2 = 0
0 * 6 = 0
Answer: 12 mod 6 ≡ 0

13/6 = 2,1666666666666666666666666666667
2,1666666666666666666666666666667 - 2 = 0,1666666666666666666666666666667
0,1666666666666666666666666666667 * 6 = 1
Answer: 12 mod 6 ≡ 1
Figure 4
"Oh okay now I understand that, but what is ≡?" Well that is just like equals sign (=) but when using modulo. More important when using in written language than in computer languages because you won't need that sign ever as far as I know. The actual meaning of it is, according to my high school physics teacher (that by the way was from Oxford, yes very much brag in this tutorial!), means that is it even stronger equals than normal equals. Enough with bragging.

What happens when you use it?
A bit more explained it counts basically like this:
Code:
You have "12 mod 6" as an example:
Our counting | modulo counting
0  | 0
1  | 1
2  | 2
3  | 3
4  | 4
5  | 5
6  | 0
7  | 1
8  | 2
9  | 3
10 | 4
11 | 5
12 | 0
Figure 5
Let's say you ever struggle with high amounts and want to calculate the modulo and you don't have a calculator who can calculate modulo, then the way of calculating above (figure 4) is better than writing what I wrote above here in Figure 5. Now all this was just to give an idea of what modulo actually is for those who are interested of knowing a bit more about it, and there are other methods to calculate it, I know, but just to explain one of them, I chose this one.



Pawn modulo
To use this "complicated" operator in pawn language we use the operator sign "%". Many languages uses this such as C#. To my previous examples, it would be written like this:
Code:
Previous: 12 mod 6
Pawn: 12 % 6
Simple right? Well I hope you feel that it is simple. You just change the word "mod" to "%". As taken from the preface where I stated that I searched for "modulo" on the SA-MP forums, I did not find very much. And I see the reason why, you don't use this many times but I have seen now two times in a week that people ask about getting minutes into seconds in the help section and I realized that it is used more than we think. Modulo is one way to calculate but here are other ways of course like manually calculating it like stated in previous figures. Here are some examples of using modulo in pawn practice:


Example 1)
You are in ajail because you broke the server rules and you want to know the time of still being in the ajail, so you contact the server scripter to script this. He would, in my honest opinion, write the code the best way by doing something like this:
pawn Code:
new minutes, seconds; //declare two integers (look E1)
new str[128]; //declare a string to hold everything

minutes = seconds / 60; //to get minutes you simply divide seconds / 60 as there are 60 seconds in one minute (easy math)
seconds = seconds % 60; //calculate modulo (look E2)

format(str, sizeof(str), "Time left: %i,%i", minutes, seconds); //using format to format everything into "str"

SendClientMessage(playerid, -1, str); //sending a message
E1: Delcare one integer holding minutes and one holding the seconds which should be the input from somewhere.
E2: Each time you have 60 seconds you want it to turn downwards into 0 right? Because 61 seconds doesn't exist in minutes (it does but you know...), so it will instead write 1 second instead of 61 seconds. So you take the float minutes and use modulo "%" with 60.

Discovery: Apparently by default if you do "minutes = seconds/60" minutes will always round downwards, so therefore no need to use "floatround".


Example 2)
You want to make so each time someone hits a 10-year anniversary you want to send message to congratulate them.
pawn Code:
new age, an; //declare a new float and an integer called "an"

an = age % 10; //calculate the age in 10-year anniversary terms (look E1)

if(an == 0) //asks if an is 0 (look E2)
{
    SendClientMessage(playerid, -1, "Congratulations on your anniversary!");
}
E1: Will "transform" so that whatever age is will be count into "10 terms" (mod 10). If age is 50 it will turn into 0. If age is 71 it will turn into 1.
E2: As we said, we wanted to make a congratulations each time someone turns a 10-years anniversary. We ask if an is equals to 0. If it is, it means the person is either 10, 20...etc. If not, it doesn't go inside the if-statement.



Additional notes:
Credits goes to e for writing the tutorial. All words are written by me. I really hope that this opens up a bit more knowledge to people reading this tutorial at least. I did not want to explain format or SendClientMessage more than I did because I didn't feel that it was important in this thread!

Credits for fixes:
pds2k12 - example code fix
Vince - English word fix
Reply


Messages In This Thread
Modulo Operator - by Hansrutger - 02.01.2014, 20:02
Re: Modulo Operator - by Patrick - 02.01.2014, 23:25
Re: Modulo Operator - by Hansrutger - 03.01.2014, 00:34
Re: Modulo Operator - by Sawalha - 03.01.2014, 05:52
Re: Modulo Operator - by Vince - 03.01.2014, 10:28
Re: Modulo Operator - by Hansrutger - 03.01.2014, 12:53
Re: Modulo Operator - by newbie scripter - 03.01.2014, 15:18
Re: Modulo Operator - by [CG]Milito - 03.01.2014, 15:56
Re: Modulo Operator - by Battlezone - 29.07.2015, 21:05

Forum Jump:


Users browsing this thread: 1 Guest(s)