• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Opposite Power in wc3?

Status
Not open for further replies.

LeP

LeP

Level 13
Joined
Feb 13, 2008
Messages
543
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.
 
Level 17
Joined
Nov 13, 2006
Messages
1,814
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
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,255
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.
 
Level 17
Joined
Nov 13, 2006
Messages
1,814
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
 
Level 2
Joined
Nov 22, 2010
Messages
21
sorry for too stupid quetsion but i dont got how can i get the opposite of power

so let say Pow(2,3)=8
how could i convert back 8 to 3?

only way the making a loop and compare each time x=x*2 with 8 and count the loop?

Pow(8,0.333)

EDIT: whoops, you're looking for 3, not 2, sry
 
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.
Top