So, I have this little problem with the modulus, because what i want is a sequence that goes:
10, 8, 6, 4, 2, 0, 10, 8, 6, 4, 2, 0, 10, ...
And what better way to do this than with modulus?
So, the way that I do it is
Variables
cw = 1 IT IS THE ONLY ONE THAT WILL BE CHANGING, cw = cw + 1
var = 5
var2 = 6
var3 = (var -(cw - 1))
z = (var3 mod var2)
It seems that it works well, but when I start debugging it in the game, I get:
10, 8, 6, 4, 2, 0, -2, -4, -6, -8, -10, ...
In other words, modulus isn't applied for some reason, modulus is supposed to work also with negative numbers, I don't know whats wrong with the math here. Please help
Other languages differ in how they handle the negative values in a modulus... But in this case I'm quite sure mod is simply the remainder of the division, therefore -1 mod 6 = -1.
All the ideas are great, my only true question is that, isn't -1 mod 6 = 5? At least that was what i was taught. I was just confused that mod didn't worked how I thought it would.
There is great diversity as to which convention for modulo is used in programming languages.
See the wikipedia entry for a giant table demonstrating this:
http://en.wikipedia.org/wiki/Modulo_operation
This all really stems from how to implement integer division.
While there is only the one convention for doing real division, there are 3 defensible ways of doing integer division and modulo:
Do you divide the two and then truncate the fractional part?
Do you instead take the floor function of the real quotient?
or Do you do an iterative process like in grade-school until you end up with a positive remainder with absolute value less than the divisor?(Euclidean Division)
Whatever your choice, If you have two integers, a and b, then normally this is gonna be true:
a = (a/b)*b + (a mod b)
At least for some division type operation and modulo type operation that your language defines.
Each different way of doing integer division results in a different modulo to satisfy that relationship.
Mathemagicians like their modulo always positive and their integer division euclidean.
Computer Scientists seem decidedly undecided on the matter. Many a holy flame war I am sure...
In a perfect world the computing lexicon would have come up with a nice consistent vocabulary in their languages for the 3 different divisions and modulos, and much confusion would be avoided.
Alas it is too late.
I like to use the traditional method, so I've been really used to the euclidean modulus. By the way, isn't truncating and applying floor function the same thing? If you have 4.3 and you truncate it, you're gonna have 4, the same way with floor function. Right?
So, I have this little problem with the modulus, because what i want is a sequence that goes:
10, 8, 6, 4, 2, 0, 10, 8, 6, 4, 2, 0, 10, ...
And what better way to do this than with modulus?
So, the way that I do it is
Variables
cw = 1 IT IS THE ONLY ONE THAT WILL BE CHANGING, cw = cw + 1
var = 5
var2 = 6
var3 = (var -(cw - 1))
z = (var3 mod var2)
It seems that it works well, but when I start debugging it in the game, I get:
10, 8, 6, 4, 2, 0, -2, -4, -6, -8, -10, ...
In other words, modulus isn't applied for some reason, modulus is supposed to work also with negative numbers, I don't know whats wrong with the math here. Please help
This is how it looks in code:
Variable Declarations
int lv_var;
int lv_var2;
int lv_var3;
int lv_zerglets;
Variable Initialization
lv_var = 5;
lv_var2 = 6;
lv_var3 = (lv_var - (gv_currentWaves - 1));
lv_zerglets = ModI(lv_var3, lv_var2);
Erm, how is your sequence even starting with 10 when you have a remainder of 4 to start? From what I can tell, your first iteration is:
z = (var3 mod var2) = ((var - (cw - 1)) mod 6) = (5 - (2 - 1)) mod 6 = (5 - 1) mod 6 = 4 mod 6 = 4
Then your next sequence is:
z = (5 - (3 - 1)) mod 6) = 3 mod 6 = 3
Multiply z by 2. I forgot that part.
z*2
@Tudentau: Go
cw is 1 in the first sequence, so it should be (5 (1-1)) mod 6 = 5 mod 6 = 5
But the problem is when the left side of mod reaches negative, mod isn't applied when I started to debug.
Example:
2 mod 6 = 2
1 mod 6 = 1
0 mod 6 = 0
-1 mod 6 = -1 (but it should be 5)
-2 mod 6 = -2 (but it should be 4)
@joemart06: Go
It seems SC2Galaxy takes the absolute value of numbers with mod. So continuing down that line,
z = (5 - (4 - 1)) mod 6) = 2 mod 6 = 2 (x2 =4)
z = (5 - (5 - 1)) mod 6) = 1 mod 6 = 1 (x2 =2)
z = (5 - (6 - 1)) mod 6) = 0 mod 6 = 0 (x2 =0)
z = (5 - (7 - 1)) mod 6) = -1 mod 6 = -1 (x2 =-2)
z = (5 - (8 - 1)) mod 6) = -2 mod 6 = -2 (x2 =-4)
z = (5 - (9 - 1)) mod 6) = -3 mod 6 = -3 (x2 =-6)
z = (5 - (10 - 1)) mod 6) = -4 mod 6 = -4 (x2 =-8)
z = (5 - (11 - 1)) mod 6) = -5 mod 6 = -5 (x2 =-10)
z = (5 - (12 - 1)) mod 6) = -6 mod 6 = 0 (x2 =0)
z = (5 - (13 - 1)) mod 6) = -7 mod 6 = -1 (x2 =-2)
etc.
If you want a sequence of 10/8/6/4/2/0 why not do
var1 = 12 (global)
if (var1 - (cw * 2 ) < 0 then modify var1 + 10
z = var1 - (cw * 2)
@joemart06: Go
Other languages differ in how they handle the negative values in a modulus... But in this case I'm quite sure mod is simply the remainder of the division, therefore -1 mod 6 = -1.
You could try this instead:
z = 5 - ((cw - 1) mod 6))
Not sure why you are going for such a complex approach.
Simply have one integer starting with 10. Have a loop that decrements it by 2 every run. Once it reaches 0, set it to 10.
Since you are most likely using your sequence in a loop anyways i dont really understand the complexity here.
All the ideas are great, my only true question is that, isn't -1 mod 6 = 5? At least that was what i was taught. I was just confused that mod didn't worked how I thought it would.
@DeltaV: Go
But the remainder must be always greater or equal than 0. That's why it doesn't make sense to me.
-1 : 6 = 0 in integer arithmetic. Therefor 0 - 1 = -1.
@DeltaV: Go
I used your idea, it worked! Thank you.
@joemart06: Go
There is great diversity as to which convention for modulo is used in programming languages. See the wikipedia entry for a giant table demonstrating this: http://en.wikipedia.org/wiki/Modulo_operation
This all really stems from how to implement integer division. While there is only the one convention for doing real division, there are 3 defensible ways of doing integer division and modulo:
Do you divide the two and then truncate the fractional part?
Do you instead take the floor function of the real quotient?
or Do you do an iterative process like in grade-school until you end up with a positive remainder with absolute value less than the divisor?(Euclidean Division)
Whatever your choice, If you have two integers, a and b, then normally this is gonna be true:
a = (a/b)*b + (a mod b)
At least for some division type operation and modulo type operation that your language defines. Each different way of doing integer division results in a different modulo to satisfy that relationship.
Mathemagicians like their modulo always positive and their integer division euclidean.
Computer Scientists seem decidedly undecided on the matter. Many a holy flame war I am sure...
In a perfect world the computing lexicon would have come up with a nice consistent vocabulary in their languages for the 3 different divisions and modulos, and much confusion would be avoided. Alas it is too late.
@intanjir: Go
Hmm so basically he's used to Euclidean division, whereas galaxy uses truncated division...
@intanjir: Go
I like to use the traditional method, so I've been really used to the euclidean modulus. By the way, isn't truncating and applying floor function the same thing? If you have 4.3 and you truncate it, you're gonna have 4, the same way with floor function. Right?
@joemart06: Go
For positive quotients it's the same, but for negative it's not. E.g. -3.4 floored is -4, -3.4 truncated is -3
@DeltaV: Go
Ah yes, then floor function on negatives is like ceiling function on positives.
@joemart06: Go
Yep.