'Mod' Operator isn't actually a Mod operator...

This is less of a complaint and more of a heads up for others using the mod operator when adjusting variables. 


I was playing around with some ideas the other day and one of them was to have a dial of numbers you could select from, the dials starts at 0 and as you hit the 'right' key, it would continue to add to the value until you reach 8 and then it would go back to 0.


This happened because:


If: Button Right is pressed down


   Control Variables: X +=1


   Control Variables: X %=8


If: Button Left is pressed down


   Control Variables: X -=1


   Control Variables: X %=8


With the mod function subtract 1, mod 8 should produce 7 but does not in RPG maker MV because of JS.


There is a difference between the Mod operator and the Remainder operator, if I had to make this into a request rather than a helpful heads up, I would ask the the game creators add the bit of code at the bottom of my post into RPGMMV to make Mod actually Mod.(Alternatively, they could just change the operator name to 'Rem' for remainder).


This isn't really the fault of anything except for JS, it handles mod as remainder even though they call it Mod.


Here is the code to make it work like mod

function mod(n, m) {


        return ((n % m) + m) % m;


}



I am aware that we can add this in as script, but it would sure be nice if it was just built in.


Thanks!


Kanoonoo(TGK)


(As an alternative solution for those wanting the same functionality as what I was trying to achieve but don't want to mess with code, you can just say:


If: Button Left is pressed down


   Control Variables: X -=1


   Control Variables: X %=8


   If: X = -1


      Control Variables: X +=8(Or whatever number you decided to mod by)
Um... Calculating a remainder is the point of a mod operator. I'm not totally sure I follow what you're expecting it to do.


MSDN's JavaScript documentation describes the modulus operator as calculating the remainder of division:


https://msdn.microsoft.com/en-us/library/9f59bza0(v=vs.94).aspx


Strangely enough, MSDN's C# documentation (I thought about digging up Java, but seeing as JS was deigned to be syntactically similar, it may not have gotten the point across) also says the exact same thing about the modulus operator:


https://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx


Again, I'm not sure what you're expecting a mod operator to do, but in any language I've learned, mod calculates and returns a remainder.


[Edit] To do what you're trying to do, I think you need something like if x mod 8 = 0 then...


Of course, that opens ip the possibility of anything divisible by 8 working... why not just check for if it equals 9, then set it to 0.
The definition of mod is the following:


a mod b returns the remainder r that results when a is divided by b. That is the official mathematical definition that has been used for centuries (since at least the Euclidean algorithm was invented). Changing it would require us to rewrite mathematics completely.


Maybe you really wanted a different function? Could you clarify what result you wanted?
Mod and Rem are not the same but are treated the same in many languages, in most cases it will do to define mod as the same as rem because in most applications the result is the same. Because of this, there are a lot of languages that use the mod operator and return the rem.


Here is a representation of the differences of behavior:


Rem(x, 5):

5+ o o
| / /
| / /
| / /
| / /
*---------*---------*---------*---------*
-10 / -5 / 0 5 10
/ / |
/ / |
/ / |
o o -5+

Mod(x, 5):

o 5o o o
/ / | / /
/ / | / /
/ / | / /
/ / | / /
*---------*---------*---------*---------*
-10 -5 0 5 10

Rem(x, -5):

5+ o o
| / /
| / /
| / /
| / /
*---------*---------*---------*---------*
-10 / -5 / 0 5 10
/ / |
/ / |
/ / |
o o -5+

Mod(x, -5):

*---------*---------*---------*---------*
-10 / -5 / 0 / 5 /10
/ / | / /
/ / | / /
/ / | / /
o o -5o o




Both of you two come from languages where the functions are the same, my post is more for those who do not come from your respective languages.


We are all right here to some degree.
A good example of this is if you google Modulo calculator, the top result handles the equation differently than the second result. The first one does exactly what I am saying and the second result does what you two are saying.


Top:http://www.miniwebtool.com/modulo-calculator/?number1=1&number2=-8


-1 mod 8 returns 7


Second:https://www.easycalculation.com/algebra/modulo-calculator.php


-1 mod 8 returns -1
Your examples don't really go into detail WHY this is different, or from what background you come that these are treated differently. It seems like the majority of programming languages do it in the way bgillisp and I have described, so I'm at a bit of a loss as to why you would expect it to behave any other way.
Actually, I never said the second was correct. -1 mod 8 is 7, here's why:


When you divide a by b, there is a number q (quotient) and remainder (r) such that a = qb + r, r >= 0. That's the mathematics definition as used by most of the world as far as I know. However, since r must be >= 0, -1 mod 8 has to be 7, as -1 = -1 * 8 + 7. So 7 is by definition, the remainder of -1 divided by 8.


Now, there are some alternate definitions that creeped up in the 60's in the USSR, so maybe the r being < 0 is one of those? But I'm not aware of it existing at least.


And if you're curious, my background is mathematics, not programming. I do very little programming at all myself.
Actually, I never said the second was correct. -1 mod 8 is 7, here's why:


When you divide a by b, there is a number q (quotient) and remainder (r) such that a = qb + r, r >= 0. That's the mathematics definition as used by most of the world as far as I know. However, since r must be >= 0, -1 mod 8 has to be 7, as -1 = -1 * 8 + 7. So 7 is by definition, the remainder of -1 divided by 8.


Now, there are some alternate definitions that creeped up in the 60's in the USSR, so maybe the r being < 0 is one of those? But I'm not aware of it existing at least.


And if you're curious, my background is mathematics, not programming. I do very little programming at all myself.



Neat to know... I guess it's important to know how any given programming language will handle things like that. However, I think the most common use of mod in a programming perspective is to simply see if a given number is a multiple of something, and do something if it is. Like, say, giving an item every 20th time you do a particular thing.
@taarna23: True. and most people don't use mod on negative numbers anyways. The only thing I ever used it on was to mod a random number between 0 and the upper limit of the integer to generate random damage in a range. In fact, I can't think of a single time I ever had to mod a negative number, hence why this probably almost never comes up.
I prefer to not think about mod on negative numbers.  Instead of subtracting 1, try adding 7.  That might get you results closer to what you're after.
@Shaz Thanks for the tip, I ended up doing basically just that, making it add 8 when the value of -1 was current and it's behaving exactly as I want it to. basically it needed to be a dial that could wrap back around to the original value.


<-- Left Key Press


0 as Starting Value


--> Right Key Press


1 <-- 2 <-- 3 <-- 4 <-- 5 <-- 6 <-- 7 <--     0     --> 1 --> 2 --> 3 --> 4 --> 5 --> 6 --> 7


@bgillisp Sorry, misunderstood you then.


@taarna23 I apologize I didn't explain that very well, bgillisp seemed to cover it though. My post was really for people with a similar background to mine in hopes it would help. I literally sat there for an hour or two wondering what the deuce was going on with mod returning the value of -7. I had to look it up myself. 
@Kanoonoo I suppose I'm just at a loss as to why you would use mod in this use case at all. Why not just...


(assume your number/variable/whatever is x)

(on left press...)
if x = 0 (or -1 if the decrement is already done)
x = 7

(on right press...)
if x = 7 (or 8 if the increment is already done)
x = 0


It's pseudocode, but I hope you get what I'm saying.
@taarna23


Because assuming mod works according to what I define mod by and how I have used it many times, its this vs this:


Screen Shot 2016-06-23 at 12.36.20 PM.png


 VS


Screen Shot 2016-06-23 at 12.36.28 PM.png


And even when I add my correction:


Screen Shot 2016-06-23 at 12.40.56 PM.png


It's still simpler code in my mind. But to each his own.
Basically, I am testing out what I can do with MV, so I wanted to create a similar meditation clock to what The Witcher 3 Wild Hunt has, Screen Shot 2016-06-23 at 12.46.57 PM.pngScreen Shot 2016-06-23 at 12.46.50 PM.png

Screen Shot 2016-06-23 at 12.45.24 PM.png

Screen Shot 2016-06-23 at 12.45.09 PM.png
When you divide a by b, there is a number q (quotient) and remainder (r) such that a = qb + r, r >= 0. That's the mathematics definition as used by most of the world as far as I know. However, since r must be >= 0, -1 mod 8 has to be 7, as -1 = -1 * 8 + 7. So 7 is by definition, the remainder of -1 divided by 8.



The equation was a = quotient*b + remainder


so why is -1 = -1*8 + 7?


-1 isn't the quotient when you divide -1 by 8, the quotient is actually 0 so it should be -1 = 0*8 + (-1)


making -1 the remainder if we use that formula... but since it needs r >= 0, I'm not sure you could actually use that same formula. 
@Engr. Adiktuzmiko: It's because the definition used requires the remainder to be >= 0. So the quotient has to be shifted to -1 to make it work. Weird, I know, but that's the formula as it is in the Division Algorithm and as it is shown in every Discrete Math book I have (and I have about 5 of them).


Edit to clarify: What the division algorithm says is this: When we divide b by a, there exists numbers q and r which are unique, such that:


b = aq + r


and r >= 0 and r < a.


The number Q which makes this true is defined as the quotient of dividing b by a, and the number r which makes this true is defined as the remainder of dividing b by a.


Since the numbers are unique, there is only one answer. Therefore, the quotient of dividing -1 by 8 is actually -1, as -1 = -1*8 + 7. 0 doesn't work as the quotient as that violates the rules for r having to be >= 0, as you would require -1 = 0*8 + -1 now.


It's counter-intuitive I know, but that's what the definition says the quotient is.
Kanoonoo, please avoid double posting, as it is against the forum rules.  If you need to add information, please edit your previous post. You can review our forum rules here. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/mod-operator-isnt-actually-a-mod-operator.63942/