Blockland Forums > Modification Help
Math with Very large numbers using Strings
<< < (3/3)
Ipquarx:
Allright, I managed to make a working bare-bones addition function (in other words, no decimals, and no negatives) and i'll post the (heavily commented) code for reference if anyone wants to see it.
I'll be working on multiplication next, which is unfourtanetly much harder than addition.
Also, Red_Guy, if you would like, we could collaborate and make a division function.
PS: why is it required to have a function create a number from a string? the engine does that automatically.
and how many lines are your functions? I cant view the file because im using a DSi.
Red_Guy:

--- Quote from: Ipquarx on November 12, 2011, 10:01:32 PM ---
Also, Red_Guy, if you would like, we could collaborate and make a division function.
PS: why is it required to have a function create a number from a string? the engine does that automatically.
and how many lines are your functions? I cant view the file because im using a DSi.

--- End quote ---
The big numbers are stored as script objects - not strings. thats why you need a function to create a number.  I also use a function because when the engine creates a number from a really long string, it changes it into scientific notation and that messes everything up -- which is the whole point behind writing your own math functions.

Also its a good idea to write a simple method to test your add/subtract functions to make sure they are getting the right answer when you change/tweak stuff

The file has 505 lines in it.  not really that big when compared to other files.

Multiply & divide would be really nice to have. let me know if you get anywhere on that part.
Ipquarx:
Numbers stored in Scriptobjects eh...
Mine does not use scriptobjects, allthough I could use one to store the functions, mine is just using strings, simply strAdd("235787448", "31364973"); and it will return the answer in about 50 lines of code without cleaning up, and with all the comments.
Allthough its strange, when i try to open the cs, it says it's a .ogg file.
Red_Guy:

--- Quote from: Ipquarx on November 12, 2011, 10:36:43 PM ---Numbers stored in Scriptobjects eh...
Mine does not use scriptobjects, allthough I could use one to store the functions, mine is just using strings, simply strAdd("235787448", "31364973"); and it will return the answer in about 50 lines of code without cleaning up, and with all the comments.

--- End quote ---
well the script object makes it easier to store stuff about the number: +/- and such
I also split the number into groups of 5 digits and let torque do the math on the smaller groups - makes it much faster when you have a lot of numbers to add, or have to add to the big number really fast.

Try these with your script and see what you get:
strAdd("100","100")
strAdd("100000", "100000")
strAdd("100", "-50")
strAdd("100", "-100")
strAdd("100000", "-50000")
strAdd("100000", "-100000")



--- Quote ---Allthough its strange, when i try to open the cs, it says it's a .ogg file.

--- End quote ---
Sounds like your browser is messed up. 
Try "save as" ?
Ipquarx:

--- Quote from: Red_Guy on November 13, 2011, 02:14:31 AM ---strAdd("100", "-50")
strAdd("100", "-100")
strAdd("100000", "-50000")
strAdd("100000", "-100000")

--- End quote ---
Those are what are called "Sign Rules", adding a positive and a negative number is really just subtracting, and i have yet to do the Subtraction function. In other words, only Positives will work.
I found a online application that adds 2 very large random numbers together, and i'm testing it using that as I speak.
But just for some confirmation; It does work correctly.

--- Quote from: Red_Guy on November 13, 2011, 02:14:31 AM ---Sounds like your browser is messed up.  
Try "save as" ?

--- End quote ---
It's a DSi. I can't "Save As" unfourtanetly.

EDIT: I just looked at your script... Almost none of that ScriptObject stuff is needed. My adding code is only 29 lines (without decimals).
Here it is, and it works 100%. If you wanna test it; try adding "32412345673735723456126234763 4" and "12652357348634652345132465674 7", it should come out as:
45064703022370375801258700438 1
And also, equ0s is a function to equal out the length of the 2 strings, like
21287345 and
        923
would become
21287345 and
00000923

Although im not 100% sure that's nessesary, because it might display as false, which displays as 0.

--- Code: ---function stringAdd(%num1, %num2)
{
if(!(isValidInteger(%num1) && isValidInteger(%num2)))
return "invalidNumber";
%num1 = getWord(equ0s(%num1, %num2), 0);
%num2 = getWord(equ0s(%num1, %num2), 1);
for(%a=0;%a<strLen(%num1);%a++)
{
%start[%a] = getSubStr(%num1, %a, 1);
%adder[%a] = getSubStr(%num2, %a, 1);
}
%Length = strLen(%num1);
for(%a = %Length - 1; %a >= 0; %a--)
{
%res = %start[%a] + %adder[%a] + %Carry;
if(%res > 9 && %a != 0)
{
%Carry = 1;
%Ans[%a] = %res - 10;
continue;
}
if(%res < 10)
%Carry = 0;
%Ans[%a] = %res;
}
for(%a = 0; %a < %length; %a++)
%Answer = %Answer @ %Ans[%a];
return %Answer;
}

--- End code ---
Navigation
Message Index
Previous page

Go to full version