Adding n multiplying huge numbers

the Immortal

I know, I know...
Reaction score
51
So ... I need to do some math with really huge (for the WE ... ) numbers which, it seems, aren't supported. Like, for example:
JASS:
set i = var1 * 2180262404997607 + var2 * 518986528207 + var3 * 123538807 + var4 * 29407 + var5 * 7 + var6

var1 is [1;18750]
var2,var3,var4,var5 are [1;4200]
So in the end I may get something like
JASS:
set i = 18750 * 2180262404997607 + 4200 * 518986528207 + 4200 * 123538807 + 4200 * 29407 + 4200 * 7 + 6

... which, as I said, WE can't handle

Well, so I made 2 functions -- 'Add' and 'Multiply' that take strings, work with numbers from 0-19 and work perfectly with 'normal' numbers (tho they should work with huge numbers like mine):
JASS:
function Add takes string s1, string s2 returns string
    local integer i = 0
    local integer n = 0
    local integer ii
    local string ret = ""
    //s1 >= s2 ... if needed change their places, bj_lastPlayedMusic isn't used anywhere
    if StringLength(s2) > StringLength(s1) then
        set bj_lastPlayedMusic = s2
        set s2 = s1
        set s1 = bj_lastPlayedMusic
    endif
    //make the numbers with the same length, adding '0'-s in the beginning
    set s1 = "0" + s1
    loop
        exitwhen StringLength(s1) == StringLength(s2)
        set s2 = "0" + s2
    endloop
    set ii = StringLength(s1)
    //add
    loop
        set n = S2I(SubString(s1,ii-1,ii)) + S2I(SubString(s2,ii-1,ii)) + i
        if n >= 10 then
            set ret = I2S(n - 10) + ret
            set i = 1
        else
            set ret = I2S(n) + ret
            set i = 0
        endif
        set ii = ii-1
        exitwhen ii == 0
    endloop
    //remove the '0' if it is still there
    if SubString(ret,0,1) == "0" then
        set ret = SubString(ret,1,StringLength(ret))
    endif
    return ret
endfunction

JASS:
function Multiply takes string s1, string s2 returns string
    local string i = "0"
    local string ret = "0"
    //repeat s1 times: ret = ret + s2
    loop
        set ret = Add(ret, s2)
        set i = Add(i, "1")
        exitwhen i == s1
    endloop
    return ret
endfunction

Well, multiplying numbers like 5324 and 9812 works perfectly using these functions ... but! When I do something like:
call BJDebugMsg(Multiply("18750", "2180262404997607"))
or even:
call BJDebugMsg(Multiply("4200", "518986528207"))

I just get red 'DebugMsg' saying:
Hit op limit in
Add(106392238282435, 518986528207)
Multiply(4200, 518986528207)
Say_Actions()

And, hm, Where's the problem in this? Am I doing something wrong? What is op? And what limit? Any way to still do that multiplying?

(yarr, too many questions, I know)
 

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
Some basics:
12 * 34
= (1 * 10 + 2) * (3 * 10 + 4)
= 1 * 10 * 3 * 10 + 1 * 10 * 4 + 2 * 3 * 10 + 2 * 4
= 1 * 3 * 10 * 10 + (1 * 4 + 2 * 3) * 10 + 2 * 4
= 3 * 10 * 10 + (4 + 6) * 10 + 8
= 3 * 10 * 10 + 10 * 10 + 8
= 3 * 10 * 10 + (1 * 10 + 0) * 10 + 8
= 3 * 10 * 10 + 1 * 10 * 10 + 0 * 10 + 8
= (3 + 1) * 10 * 10 + 0 * 10 + 8
= 4 * 10 * 10 + 0 * 10 + 8
= 408

Rather lengthy :p
Well, the idea being that numbers can be split into, for example, hundreds, tens and ones...

Or whatever really.
Like 12345 = 12 * 1000 + 345, which "cuts" that huge number into two smaller parts that you can work on.

With 10000 for example, 1234567890 becomes 12, 3456 and 7890, each one being much smaller than the original.

Getting the array math correctly requires some work, but
1. there's basically no limit anymore on the size
2. it's much faster than digit by digit
3. it's insanely faster than calling a "long add" digit by digit...


The limit you're hitting is the operations limit.
Once your trigger used up more than 12000 (or close to that) operations without waiting inbetween, the game simply kills the trigger...
 

the Immortal

I know, I know...
Reaction score
51
@Kenoriga: *cough* did you read what I wrote?

@AceHart: Friendly Neighborhood Admin as always ^^ I didn't know about that 12k operations without wait. Gonna try to implement it using that way you suggested

A system with such type of adding / substracting / multiplying / dividing may be idd useful.
 

Vexorian

Why no custom sig?
Reaction score
187
@Kenoriga: *cough* did you read what I wrote?

@AceHart: Friendly Neighborhood Admin as always ^^ I didn't know about that 12k operations without wait. Gonna try to implement it using that way you suggested

A system with such type of adding / substracting / multiplying / dividing may be idd useful.
I made a bignum struct once for an unreleased alternative to codemaker that uses stream objects to do stuff, I can post the bignum struct if you want... It has got a flaw and it is that a bignum can only devide by normal integer numbers.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top