HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Helpful "kool" Math: Grouping Numbers

11-19-2007, 11:45 PM#1
Salbrismind
This is a simple math equation I found during the creation of my map and I decided to share with the community in hopes that it can help someone.

Basically it is a 1-2 line math equation (2 so it's easier to look at) that take the number of one member of a group and returns the other member all within one array. The equation is limited to the fact that it can only work with groups of two (One number goes in, one number comes out).


Collapse JASS:
function GetOtherNumber takes integer firstnum returns integer
local integer temp
set temp = R2I(I2R(x)/2 - 0.6)
return (4 * temp + 3 - x)
endfunction

The function takes the number of the array already known and then gives the user the corresponding number back. Such that if it takes the number 2 in it will return 1, if it takes 1 it will return 2, if takes 3 returns 4, etc.

I found this useful in my map when I had 8 different units stored in an array and i needed to find its pair quickly. My map has 4 arenas and there will be a maximum of 2 units per arena so I grouped the array into 4 parts based on what arena the units were in: First 2 parts of the array are arena one, 3-4 is arena 2 and so on.

I can't think of any direct uses for this in other places but anywhere you have any number of things in an array grouped together in pairs where both can be used then this could be helpful in determining the objects pair.
11-20-2007, 06:20 AM#2
Silvenon
That's cool, thanks.
11-20-2007, 06:39 AM#3
Strilanc
Your function is a bit... insane. I can see that it will work for 1 to 8, but it's not obvious at ALL what it should be doing. Here is a function which will do the same thing, but is faster, simpler, and works for all numbers.

Collapse JASS:
///Returns the 'partner' of the given number. eg. 1 with 2, 3 with 4, ...
function getEvenOddPartner takes integer i returns integer
  return i - 1 + ModuloInteger(i, 2)*2
endfunction

ModuloInteger(i, 2) returns the remainder after dividing i by 2 (so 1 if i is odd, 0 if i is even). Modulo is a really, really useful function to remember.
11-20-2007, 06:40 AM#4
Jazradel
I'm sure that would be easier done just using if/else:
Quote:
if R2I(x/2) == x/2 then
return x
endif
return x-1
That's slightly faster than yours, according to pipedream's tests (http://www.wc3campaigns.net/pastebin...047f7dfb816411)

Edit: And strilanc's is even better. I always forget how to use ModuloInteger.
11-20-2007, 07:14 PM#5
Salbrismind
Your absolutely correct, Strilanc, I'll alter my code with yours, thanks.

Jazradel, that doesn't work for what I was showing. Not only does it not do it even half proper but it has to give the numbers pair, not the number or 0.
11-21-2007, 10:02 AM#6
Jazradel
Damn, for some reason I always type return 0 instead of what I actually mean.
11-21-2007, 10:41 AM#7
cohadar
That is because you have been brainwashed by handle vars.