Opposite Power in wc3?

Status
Not open for further replies.
You are probably looking for the logarithm of a number.
log2(8) means the number which 2 has to be raised by to get 8, aka. 3 because 2³=8.
Here is a general script for logarithms.

e: Also this.
e: But if you only want the logarithms of base 2 and only integer results there are faster ways doing that.
 
so dont have a simple command way :P

i know how to make it with loop but i hoped have a shorter form.

i also want skip in loop the pow since i heard pow(x,x) is slower than x*x :)

JASS:
function log takes integer n returns integer
local integer i = 2
local integer c = 1
if n == 1 then
set c = 0
endif
loop
exitwhen i >= n
set c = c + 1
set i = i * 2
endloop
return c
endfunction
 
Firstly it deals with integers, logs can be fractional.
Secondly it returns an incorrect value for a log of a negative or 0 (some error should happen here).

The way you currently have it you would be better off using a look up table and incriment through it instead of computing each value each time.

but i dont use engative numbers just 2^0-29

u mean save to hashtable or what table u mean?

ok its faster than last example no?

map init
JASS:
    loop
        exitwhen i > udg_Buff_Max
        set udg_Buff_ID2V[i] = R2I(Pow(2,i))
        set i = i + 1
    endloop
header func
JASS:
function log takes integer n returns integer
    local integer i = 2
    if n == 1 then
    endif
    loop
        exitwhen udg_Buff_ID2V[i] == n
        set i = i + 1
    endloop
    return i
endfunction
 
Yeah, like Dr SuperGood alluded to and to quote what Dark_Dragon posted, this way is the most optimized for integers (possibly a non-recursion option would be faster):

JASS:
// *** Returns log from given values ***
constant function log takes integer base, integer result returns integer
    if (result == 1) then
        return 0
    endif
    return log(base, result/base)+1
endfunction
 
Status
Not open for further replies.
Back
Top