HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Logarithm

07-08-2008, 02:15 AM#1
Vexorian
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...

Collapse 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=i-1
            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=i-1
            if (Pow(base,mid)>=x) then
                set max=mid
            else
                set min=mid
            endif
        endloop
     return mid
    endfunction

endlibrary
07-08-2008, 03:43 AM#2
PandaMine
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
07-08-2008, 04:49 AM#3
Rising_Dusk
This is still really cool, I love mathematical code.
07-08-2008, 06:46 AM#4
d07.RiV
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.
07-08-2008, 08:18 AM#5
Captain Griffen
Sometimes maths code is needed in strange places.

Quote:
Originally Posted by d07.RiV
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.

Or you can itinerate it, I think, which'd be faster. Far faster.
07-08-2008, 01:22 PM#6
PandaMine
Quote:
Originally Posted by Captain Griffen
Sometimes maths code is needed in strange places.



Or you can itinerate it, I think, which'd be faster. Far faster.

Pity you can't do recursion in wc3...
07-08-2008, 04:31 PM#7
Strilanc
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.
07-09-2008, 01:02 AM#8
PandaMine
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
07-09-2008, 01:30 PM#9
DioD
never saw use of such functions in wc3
07-09-2008, 02:29 PM#10
Vexorian
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.
07-11-2008, 07:01 AM#11
d07.RiV
Also there are much better methods than bin search. Like:
Collapse 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
Of course logarithm with another base would require you to know ln (base) but that can be computed the same way or, since base is usually constant, hard-coded.
07-11-2008, 02:38 PM#12
Pytho
Quote:
Originally Posted by d07.RiV
Collapse 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
Like your method
Just for the sake of completeness set i=i+1, in case someday someone really needs log...
07-11-2008, 03:22 PM#13
Vexorian
I was going to make one pipe suggested that does not use Pow, it is probably what Strilanc said as well.
07-17-2008, 02:06 PM#14
d07.RiV
Quote:
POW probably works by repeated squaring so you're repeating the low squares a lot.
No, POW is a crt function which (in 99.999% implementations) uses FPU (a processor for floating point arithmetics).
07-17-2008, 04:51 PM#15
BlinkBoy
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.