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
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top