| 07-08-2008, 02:15 AM | #1 |
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=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 |
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 |
This is still really cool, I love mathematical code. |
| 07-08-2008, 06:46 AM | #4 |
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 | |
Sometimes maths code is needed in strange places. Quote:
Or you can itinerate it, I think, which'd be faster. Far faster. |
| 07-08-2008, 01:22 PM | #6 | |
Quote:
Pity you can't do recursion in wc3... |
| 07-08-2008, 04:31 PM | #7 |
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 |
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 |
never saw use of such functions in wc3 |
| 07-09-2008, 02:29 PM | #10 |
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 |
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 |
| 07-11-2008, 02:38 PM | #12 | |
Quote:
Just for the sake of completeness set i=i+1, in case someday someone really needs log... |
| 07-11-2008, 03:22 PM | #13 |
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 | |
Quote:
|
| 07-17-2008, 04:51 PM | #15 |
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. |
