Quick Question about "Mod" operator
I've been searching through the forums and the help file that came with MMF2, but none can tell what the "Mod" operator does in the expression editor to different numbers, they only point out that it is there. I've never used it but can someone tell what it does to numbers and why it is included?
Thanks, Hughster
Re: Quick Question about "Mod" operator
http://en.wikipedia.org/wiki/Modulo_operation
Re: Quick Question about "Mod" operator
It basically divides two numbers and returns the remainder.
Re: Quick Question about "Mod" operator
Its especially useful when dealing with 2 or more dimensions.
For example (1D-2D)
let V='1D and W='x width' position' then in 2d
x=V MOD W
y=(v-(V MOD W))/w
Re: Quick Question about "Mod" operator
As a more practical whatever example:
1 mod 3 = 1
2 mod 3 = 2
3 mod 3 = 0
5 mod 3 = 2
8 mod 3 = 2
8 mod 3 = 0
Re: Quick Question about "Mod" operator
By practical you mean incorrect?
8 mod 3 = 2
Re: Quick Question about "Mod" operator
I think of mod as a circle. Once you've reached the desired end you go back to the start.
Re: Quick Question about "Mod" operator
It's just a shame most mod implementations don't handle negative numbers.. that'd be so much more useful (although I saw some that did before.. not sure in which language though) - especially with rotations.
Re: Quick Question about "Mod" operator
And an even more practical example:
128 seconds:
128 / 60 = 2
128 mod 60 = 8
128 seconds = 2 minutes and 8 seconds
Re: Quick Question about "Mod" operator
Quote:
Originally Posted by Random
It's just a shame most mod implementations don't handle negative numbers.. that'd be so much more useful (although I saw some that did before.. not sure in which language though) - especially with rotations.
I think it does, but like this:
-128 / 60 = -2
-128 mod 60 = -8
To wrap it positive IIRC you do this:
((number mod limit) + limit) mod limit)
Re: Quick Question about "Mod" operator
Re: Quick Question about "Mod" operator
Quote:
Try abs(negative number)
What?
Re: Quick Question about "Mod" operator
Thats how to get it to work with negative numbers.
Re: Quick Question about "Mod" operator
Thanks, although I'll still probably never use it since I have no programming skills, it's useful to know what it is just in case. And thanks for the examples.
Re: Quick Question about "Mod" operator
Quote:
Originally Posted by dragonguy
Try abs(negative number)
Thats how to get it to work with negative numbers.
Actually no. abs(-2) is 2, when you'd expect it to be 30 using mod. Think directions: 0 = 32, -1 = 31, -2 = 30...
Re: Quick Question about "Mod" operator
Quote:
Try abs(negative number)
*ignore*
Quote:
I think it does, but like this:
-128 / 60 = -2
-128 mod 60 = -8
To wrap it positive IIRC you do this:
((number mod limit) + limit) mod limit)
Indeed. I don't find it very practical though. I've seen implementations where -128 mod 60 is 52. I find it rather annoying to have to write it the complicated way all the time, but then I guess I'm just lazy.
According to wikipedia there are three common ways to implement modulo. I suppose then it's down to luck which one you get with your development environment..
Re: Quick Question about "Mod" operator
Euclidean definition ftw! I wish MMF2 used this.
Re: Quick Question about "Mod" operator
The mod function has heaps of uses - here's a couple more...
To determine if a number is odd or even:
if "Number" mod 2 = 0, then "Number" is even
if "Number" mod 2 = 1, then "Number" is odd
To find the difference between two angles:
((Angle1 - Angle2 + 540) mod 360) - 180
It essentially tells you whether to rotate clockwise or counter-clockwise, depending on whether the result is positive or negative.
To load a tile-based map from an array, using a fast loop:
XPosition = LoopIndex(">Fastloop<") mod DimX( ">Array<" )
YPosition = LoopIndex(">Fastloop<") / DimX( ">Array<" )