Blockland Forums > Modification Help
Math with Very large numbers using Strings
Destiny/Zack0Wack0:
--- Quote from: Port on November 09, 2011, 02:39:58 AM ---I don't know if that's any help, but I thought about something like converting the two numbers to binary, doing binary math (which is a quite simple logical progress) and converting them back. I'm not sure if that's a good solution though.
--- End quote ---
How the hell would that work? Binary and bitwise operations are expressed as decimal numbers in Torque. Unless you mean a string of 1s and 0s.
EDIT: This seems to work...
--- Code: ---function setSubStr(%string,%start,%value) //support function for settings characters or sub strings in strings
{
return getSubStr(%string,0,%start) @ %value @ getSubStr(%string,%start + strLen(%value),strLen(%string));
}
function mBigAdd(%value,%other)
{
%numbers = "0123456789";
%length1 = strLen(%value);
%length2 = strLen(%other);
for(%i=0;%i<%length2;%i++) //loop forwards
{
%number2 = getSubStr(%other,%length2 - %i - 1,1);
if(%i >= %length1) // we don't have a column for this, so just append it at the front
{
if(%number2 $= "0") //so we don't get 010 + 10 = 020, but rather 20
continue;
%value = %number2 @ %value;
continue;
}
%number1 = getSubStr(%value,%length1 - %i - 1,1);
if(strPos(%numbers,%number1) == -1 || strPos(%numbers,%number2) == -1) //not a number character, continue
continue;
%new = %number1 + %number2; //add the two one digit numbers together
if(%new > 9) //reached above 10, need to carry this over to the next column of digits
{
%next = %length1 - %i - 2; //next column index in the string
%value = setSubStr(%value,%length1 - %i - 1,%new - 10); //set the value of the current column to the digits that go over 10
if(%next < 0) //next column doesn't exist, so just append 1 to the start of the string (example, 923 + 923 -> 1846)
{
%value = 1 @ %value;
continue;
}
%value = setSubStr(%value,%next,getSubStr(%value,%next,1) + 1);
}
else //not carrying over, we can simply change the character
%value = setSubStr(%value,%length1 - %i - 1,%new);
}
return %value;
}
--- End code ---
It's really scrubby, I just put it together quickly I'll clean it up and add a subtract thing some other time
Port:
--- Quote from: Destiny/Zack0Wack0 on November 09, 2011, 05:04:43 AM ---How the hell would that work? Binary and bitwise operations are expressed as decimal numbers in Torque. Unless you mean a string of 1s and 0s.
--- End quote ---
Ipquarx:
--- Quote from: Port on November 09, 2011, 08:14:49 AM ----snip-
--- End quote ---
i mean strings of numbers, you know;
0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
like
echo(4872984723894723400478120); will give a scientific notation rounded number
echo("4872984723894723400478120"); will give the entire numbers, because strings aren't limited.
Destiny/Zack0Wack0:
--- Quote from: Port on November 09, 2011, 08:14:49 AM ---
--- End quote ---
Probably easier to just parse it and handle it the elementary school way. Having pretty big numbers could result in a long string in binary form, although I doubt it's going to hit the character limit (can't remember it, something like 4096 I think?), but yeah.
--- Quote from: Ipquarx on November 09, 2011, 01:13:21 PM ---i mean strings of numbers, you know;
0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
like
echo(4872984723894723400478120); will give a scientific notation rounded number
echo("4872984723894723400478120"); will give the entire numbers, because strings aren't limited.
--- End quote ---
He understood what you meant, he was answering me.
Fixed a bug with my script (I forgot to make values at index 0 that go over 9 append to the beginning of the string), added an insight into what the hell is going on and added the setSubStr support function I forgot to include. Seems to work perfectly now.
Red_Guy:
I'll save you guys some of the hassle here.
This is the script I wrote for unlimited mining to handle VERY large numbers. It handles up to 20 digits with no problems. In theory it will handle an unlimited but 20 is the max thats been used in my server.
you can do add, subtract, greater than and less than. no multiply, no divide.
to use:
echo( BigNumberFromString("1234567890123").add("1234567").display() );
gives this:
1234569124690
where normally you would see:
echo(1234567890123+1234567);
1.91351e+009
in a script:
--- Code: ---%money = BigNumberFromString( some really big number here );
%money.add( another really big number );
echo(%money.display() );
%money.subtract("50000000000000000000");
echo(%money.display() );
--- End code ---