HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Some question, need help ><

01-01-2007, 05:06 AM#1
zen87
kay i need something like this
Collapse JASS:
function IsIntEven takes integer i returns boolean

...
...

endfunction

this function checks weather a integer is even or odd, e,g :
Collapse JASS:
IsIntEven(2) //returns true
IsIntEven(19) // returns false
IsIntEven(100) //returns true
IsIntEven(7) // returns false

+rep to first who solved the question
01-01-2007, 05:30 AM#2
Pyrogasm
A roundabout way, and it's sorta messy but here's the steps:
  • Divide the Integer by 2 to get a Real
  • Convert the Real to a new Integer
  • Check to see if the Real equals the new Integer

Example:
9/2 = 4.5
4.5 (real) —> 4 (integer)
4.5 =/= 4, so it's not even.

16/2 = 8
8 (real) = 8 (integer)
8 == 8, so it is even.

However, I don't think that solves your problem, though; you need a boolean.
01-01-2007, 05:38 AM#3
zen87
lol atctually it does !! thanks !!

Collapse JASS:
function IsIntEven takes integer i returns boolean
 local integer n = 2*R2I(I2R(i)/2)
    if n==i then
        return true
    else
        return false
    endif
endfunction

+rep xD
01-01-2007, 05:41 AM#4
TaintedReality
Use the Modulo bj function.

Collapse JASS:
function ModuloInteger takes integer dividend, integer divisor returns integer
    local integer modulus = dividend - (dividend / divisor) * divisor

    // If the dividend was negative, the above modulus calculation will
    // be negative, but within (-divisor..0).  We can add (divisor) to
    // shift this result into the desired range of (0..divisor).
    if (modulus < 0) then
        set modulus = modulus + divisor
    endif

    return modulus
endfunction

It returns the "leftovers" if dividend is divided by divisor. So your divisor will be 2, your dividend will be i. If the leftover is = to 0, then it's even.

Edit: Just saw your last post..that will work too ^^.
01-01-2007, 06:00 AM#5
Pyrogasm
Nice! I was helpful for once! Thanks for the +Rep man!
01-01-2007, 08:52 AM#6
Captain Griffen
Collapse JASS:
function IsIntEven takes integer i returns boolean
    return i == (i/2)*2
endfunction
01-01-2007, 09:10 AM#7
Pyrogasm
Quote:
Originally Posted by Captain Griffen
Collapse JASS:
function IsIntEven takes integer i returns boolean
    return i == (i/2)*2
endfunction
Am I missing something, or is that completely and utterly pointless because the multiplication and division cancel each other out?
01-01-2007, 10:43 AM#8
BertTheJasser
no, your PC calculates part for part. The first part will round the devided int (if it was 1,2,3..), the second will multiply again, which means if the int was rounded, there will be mistake.

So this is seriously correct and the bset solution at all. Give Captain Griffen +rep, his solution is faster than all the others.

EDIT: Wrong name you should +rep
01-06-2007, 08:52 AM#9
Pyrogasm
Bert, not to rebuke myself but... how is my solution faster than Captain Griffen's? It seems that his requires only one division and 1 multiplication, while mine requires 1 division, 1 multiplication, an I2R() conversion, and an R2I() conversion.

Also, seems rather hypocritical that you instructed others to rep me, but did not do so yourself...
01-06-2007, 02:48 PM#10
BertTheJasser
Sorry, .. I guess my eyes were shut, but I did not see the quote. :?
01-06-2007, 08:41 PM#11
Pyrogasm
I was gonna say...