Blockland Forums > Modification Help

Math with Very large numbers using Strings

Pages: (1/3) > >>

Ipquarx:

I've heard of a method of doing math on very large numbers using strings, by turning the pen-and-paper math methods into functions that input strings that have numbers in them, and output the correct result.
I've tryed making an addition function, but it diddnt turn out so well.

Does anyone know how to make working addition, subtraction, multiplication, and (possibly) division? (to a specefied number of decimal places)

cheese6:

idk if this is what your looking for, but I found this.

--- Code: ---function StringMath(%str)
{
for(%i=0;%i<getWordCount(%str);%i++)
{
if(getWord(%str,%i) != getWord(%str,%i) * 1)
{
%nextnum = getWord(%str,%i);
%math = %math SPC %nextnum;
if(getWord(%math,%i - 1) != getWord(%math,%i - 1) * 1)
{
switch$(getWord(%math,%i - 1))
{
case "+":
%answer = %answer + %nextnum;
case "-":
%answer = %answer - %nextnum;
case "*":
%answer = %answer * %nextnum;
case "/":
%answer = %answer / %nextnum;
}
}
}
else
%math = %math SPC getWord(%str,%i);
}
}
--- End code ---

Not tested but should work
By Brian Smith :)
Got it from his calculator.

Ipquarx:

Not what i'm looking for...
functions, 2 arguments.
works on a char-by-char basis.
seperate functions for addition, subtraction, multiplication, and division (if possible).

cheese6:


--- Quote from: Ipquarx on November 08, 2011, 11:27:25 AM ---Not what i'm looking for...
functions, 2 arguments.
works on a char-by-char basis.
seperate functions for addition, subtraction, multiplication, and division (if possible).

--- End quote ---
What
you mean like

--- Code: ---function Add(%str1,%str2)
{
return %str1 + %str2;
}

function Subtract(%str1,%str2)
{
return %str1 - %str2;
}
function Mulitply(%str1,%str2)
{
return %str1 * %str2;
}
function Divide(%str1,%str2)
{
return %str1 / %str2;
}

--- End code ---
or like

--- Code: ---function MathFunction(%numbers,%type)
{
switch$(%type)
{
case "+":
for(%i=0;%i<getWordCount(%numbers);%i++)
{
%answer = %answer + getWord(%numbers,%i);
}
case "-":
for(%i=0;%i<getWordCount(%numbers);%i++)
{
%answer = %answer - getWord(%numbers,%i);
}
case "*":
for(%i=0;%i<getWordCount(%numbers);%i++)
{
%answer = %answer * getWord(%numbers,%i);
}
case "/":
for(%i=0;%i<getWordCount(%numbers);%i++)
{
%answer = %answer / getWord(%numbers,%i);
}
}
return %answer;
}
--- End code ---
Idk what your asking for.

Port:

He's asking for a function like this:

--- Code: ---function add( %a, %b )
{
    return ( any method of adding %a and %b together without using the + operator );
}

--- End code ---

Quark is working on something like this, I'll get back to you if he reaches any significant progress.
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.

Pages: (1/3) > >>

Go to full version