- Joined
- Aug 26, 2010
- Messages
- 573
Content:
Intro:
I have seen somewhere request for fast powering library. So I made it as our math teacher said. Here are 2 variants - 2 based and 3 based. I haven't still tested yet, but I'll do it tomorrow. Theoretically 3 based will be fast with HUGE numbers, 2 based with mid numbers and comond multipling with small numbers.
Macros:
JASS:
library FastPow
//! textmacro FastPowMacroBig takes TYPE, MUL, INIT
function FastPow2$TYPE$ takes $TYPE$ x, integer y returns $TYPE$
local $TYPE$ temp = $INIT$
loop
exitwhen y <= 0
if y - (y / 2) * 2 == 1 then
call temp.$MUL$(x)
endif
call temp.$MUL$(temp)
set y = y / 2
endloop
return temp
endfunction
//! endtextmacro
//! textmacro FastPowMacro2 takes TYPE
function FastPow2$TYPE$ takes $TYPE$ x, integer y returns $TYPE$
local $TYPE$ temp = 1
loop
exitwhen y <= 0
if y - (y / 2) * 2 == 1 then
set temp = temp * x
endif
set temp = temp * temp
set y = y / 2
endloop
return temp
endfunction
//! endtextmacro
//! textmacro FastPowMacro3 takes TYPE
function FastPow3$TYPE$ takes $TYPE$ x, integer y returns $TYPE$
local $TYPE$ temp = 1
loop
exitwhen y <= 0
if y - (y / 3) * 3 == 1 then
set temp = temp * x
elseif y - (y / 3) * 3 == 2 then
set temp = temp * x * x
endif
set temp = temp * temp
set y = y / 3
endloop
return temp
endfunction
//! endtextmacro
//! runtextmacro FastPowMacro2("integer")
//! runtextmacro FastPowMacro2("real")
//! runtextmacro FastPowMacro3("integer")
//! runtextmacro FastPowMacro3("real")
//! runtextmacro FastPowMacroBig("BigInt", "multiplyBig", "BigInt.convertString(\"1\", Base[\"0123456789\"])")
endlibrary
API description:
This lib is divided into 3 parts:
- FastPow2
- FastPow3
- FastPow2 for big integers
You can easily cange using basment or use both. Here all 4 functions are enabled. If you don't need some of them you have just to remove their runtextmacro lines.
Also you can easily change big numbers support - the one in lib is BigInt by Nestharus. To do it you have to change/add+change this line:
JASS:
//! runtextmacro FastPowMacroBig("BigInt", "multiplyBig", "BigInt.convertString(\"1\", Base[\"0123456789\"])")
JASS:
//! runtextmacro FastPowMacroBig("YourLibraryName", "FunctionMultiplyingBigXBig", "LineInitializingATempBigNumberToBeEqual1")
Example of usage:
JASS:
local integer int
local real r
local BigInt bi
set int = FastPow2integer(2, 10)
call BJDebugMsg(I2S(int))
set r = FastPow2real(3.5, 10)
call BJDebugMsg(R2S(r))
set int = FastPow3integer(2, 10)
call BJDebugMsg(I2S(int))
set r = FastPow3real(3.5, 10)
call BJDebugMsg(R2S(r))
set bi = FastPow3BigInt(5, 70)
call BJDebugMsg(bi.toString())
Example with Nestharus BigInt
Changelog:
Added 2 based big integers
Added 3 basement
First version
Last edited: