the % mod is something of a complete mystery for me
Alright there's already enough explanation above, I just want to give more example about modulo.
alpha = 10 % 5
alpha value will be
0, because
10 / 5 is 2 without any
VALUE left, or we know
5 x 2 = 10, then 10 - 10 = 0
beta = 10 % 3
beta value will be ? that's right 1, because
10 / 3 is 3 but will left
1 as a remainder , or we know to reach
10, number
3 need to be multiplied by three times and then it becomes
9, the closest value to 10, but not pass over it. But then it still give a remainder from 10, which is
1 and
1 is not that can be divided by 3
So
beta balue will be 1.
delta = 10 % 6
delta value will be 4, you know why right? Yes,
6 multiplied by 1 is 6, then 10 - 6 = 4
Here's some explanation again, and the main idea.
my_modulo = A % B, Does A divisible by B? If yes then the value of
my_modulo will be 0. If not? Then B need to be multiplied with by a number, and the result of the multiplication must reach the closest value of A but not passing over it. Finally just substract A with the B, the result will become the value of
my_modulo.
Here's the basic idea :
Spoiler
The general formula to calculate the mod
A % B would be to find a multiple of B, that less than A, but closest to A. Subtract A with the result of multiplication,then you got the value of the mod.
IMPORTANT : All my explanation above is good on positive operands, if you try to use negative operands
,
For example: 10 % -3 or -5 % 8, or -30 % - 34(though the last will give same result like positive and positive)
Then you need more deep different logic explanation(although the algorythm is still same).
Modulo with negative operands
alpha = -10 % 6
alpha will result in
2. Surprised? Unfortunately this is not wrong, let's get the workflow.
1. Does number -10 divisible by 6? No is not. Then
alpha value will never be 0.
2. 6 need to be multiplied by
a NUMBER to get into a value nearest to -10(negative 10). Let's see, in order to get negative value 6 must be multiplied by a
negative value too(remember this), so let's try with
-1, the result is -6, Oh yeah we got the value closest to -10,
DO WE..?, Wrong.. For negative number we know that -6 is greater than -10, so it's not legal(or what is the word?). So then we need to multiplied it again until we get the nearest value but again, NOT PASSING OVER IT, for this we will try
-2, which then resulting in
-12, Yaaay, finally we get the number we want.
3. Lastly check the remainder, for this case
-10 - (-12) will result in
2 <= This is the final result.
Alright, that some explanation about modulo, hope it helps =), sorry if I made a mistake.