HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

MathTable useful?

06-17-2009, 02:10 PM#1
Opossum
I thought it might be useful for speedfreaks to just store the values of complex functions in a table so I created this MathTable library:
Collapse JASS:
library MathTable requires Table

    function interface MathFunction takes real x returns real
    
    struct MathTable
        private Table table
        readonly real precision
        
        static method create takes MathFunction whichFunction, real lowBound, real highBound, integer precision returns MathTable
            local MathTable mt = MathTable.allocate()
            local integer i
            local integer max
            
            set mt.table = Table.create()
            set mt.precision = Pow(10, precision)
            set i = R2I(lowBound*mt.precision)
            set max = R2I(highBound*mt.precision)
            loop
                exitwhen i > max
                set mt.table[i] = R2I(whichFunction.evaluate(i/mt.precision)*mt.precision+0.5)
                set i = i+1
            endloop
            
            return mt
        endmethod
        
        method operator [] takes real x returns real
            return I2R(.table[R2I(x*.precision+0.5)])/.precision
        endmethod
        
        method onDestroy takes nothing returns nothing
            call .table.destroy()
        endmethod
    endstruct
    
endlibrary
Example usage:
Collapse JASS:
scope Test initializer Init

    private function TestFunc takes real x returns real
        return x*x + 0.5*x + 27
    endfunction

    private function Init takes nothing returns nothing
        local MathFunction mf = MathFunction.TestFunc
        local MathTable mt = MathTable.create(mf, 0, 20, 2)
        
        call BJDebugMsg(R2S(mt[18.257])) // Displays 369.560. Exact value would be 369.447
    endfunction

endscope

What this does is pretty simple actually. You create a MathTable passing a function that only takes one real value, a lowBound and highBound value and a precision value.
The struct then creates a table and stores all values from lowBound to highBound in a table. precision determines the decimal places e.g. using the numbers from the above example all values from 0.00 to 20.00 will be calculated (i.e., 0.01, 0.02 etc.).
You can then easily access these values by calling mt[x]. mt being a previously created MathTable and x being a value within the previously determined boundaries.

I just have some doubts concerning the usefulness of this library. One thing is that table currently uses gamecache which is said to be slow as hell making this whole thing kinda useless. Another issue is that you can easily hit the op limit if the precision or function is too big.

Any thoughts?
06-17-2009, 03:06 PM#2
Anitarf
Quote:
I just have some doubts concerning the usefulness of this library. One thing is that table currently uses gamecache which is said to be slow as hell making this whole thing kinda useless. Another issue is that you can easily hit the op limit if the precision or function is too big.

Any thoughts?
No, I think you summed it up pretty well. I'd like to see an equation that is slower than two GC reads.