07082008, 03:15 AM  #1 
Free Software Terrorist
Technical Director

Logarithm
Approximates the logarithm after 20 calls to Pow. Does someone need logarithms? I can think of some uses for them but they don't fit warcraft too much, well, just in case someone does...
__________________ JASS: library Logarithm globals private constant integer ITERATIONS=20 endglobals function Log takes real x returns real local real min=88.0 local real max= 88.0 local real mid local integer i=ITERATIONS loop set mid=(min+max)/2 exitwhen(i<=0) set i=i1 if (Pow(bj_E,mid)>=x) then set max=mid else set min=mid endif endloop return mid endfunction function Logarithm takes real base, real x returns real local real min=88.0 local real max= 88.0 local real mid local integer i=ITERATIONS loop set mid=(min+max)/2 exitwhen(i<=0) set i=i1 if (Pow(base,mid)>=x) then set max=mid else set min=mid endif endloop return mid endfunction endlibrary 
Sponsored Links  Login to hide this ad! 

07082008, 04:43 AM  #2 
Panda > You

Well general use of logs is for caculating max time in searches (i.e. binary search).
__________________Possibly physics might need it, but I don't really think a situation one would need it in wc3 
07082008, 07:46 AM  #4 
User
Join Date: May 2008
Posts: 241

You need logarithms to find negative armor knowing damage reduction, I doubt there is much use unless you made your formulas use logarithms on purpose.
__________________ 
07082008, 09:18 AM  #5  
Dread Lord of the Cookies
Content Director

Sometimes maths code is needed in strange places.
__________________Quote:
Or you can itinerate it, I think, which'd be faster. Far faster. 

07082008, 02:22 PM  #6  
Panda > You

Quote:
Pity you can't do recursion in wc3... 

07082008, 05:31 PM  #7 
User

Nice. I actually thought there was already log function (which shows how often I have needed it).
__________________It would probably be faster to just repeatedly square b (or 1/b) (storing each computed value in an array) until you went past the number, then working from there. POW probably works by repeated squaring so you're repeating the low squares a lot. 
07092008, 02:02 AM  #8 
Panda > You

Apparently POW is faster then doing x*x*x*x all the time (think pipedream proved this somewhere).
__________________It might be vice versa, can't remember completley 
07092008, 02:30 PM  #9 
obey

never saw use of such functions in wc3

07092008, 03:29 PM  #10 
Free Software Terrorist
Technical Director

If you keep looking for fun curves, Log will eventually appear in a derivative. The problem is that the only math that people conceive in war3 is geometry, though after seeing a couple of systems in which heuristics were used, Log doesn't seem all that much of an unlikely requirement.
__________________ 
07112008, 08:01 AM  #11 
User
Join Date: May 2008
Posts: 241

Also there are much better methods than bin search. Like:
__________________ JASS: function Logarithm takes real a returns real local real x = 0 local integer i = 0 loop exitwhen i > 10 set x = x  1 + a / Pow (bj_E, x) set i = 1 + 1 endloop return x endfunction 
07112008, 03:38 PM  #12  
User
Join Date: Aug 2007
Posts: 29

Quote:
Just for the sake of completeness set i=i+1, in case someday someone really needs log... 

07112008, 04:22 PM  #13 
Free Software Terrorist
Technical Director

I was going to make one pipe suggested that does not use Pow, it is probably what Strilanc said as well.
__________________ 
07172008, 03:06 PM  #14  
User
Join Date: May 2008
Posts: 241

Quote:


07172008, 05:51 PM  #15 
User
Respected User

you could try using Briggs' method, it does not require pow, but you'll have to store in arrays the consecutive values of 10^(1/2^n) so, you end up storing 10^1/2, 10^1/4, 10^1/8..... and so on. 10 is the base.
__________________Google it around. Last edited by BlinkBoy : 07172008 at 05:54 PM. 
Thread Tools  Search this Thread 

