And i though Janevert said i could not do it, no overflow.
<SCRIPT LANGUAGE="Javascript">
/* COMPARE SIZE OF TWO ARRAYS, IF A < B RETURN TRUE, IF B >= A RETURN FALSE */
function arrCompareTwo(A, B)
{
var AA = A.length;
var BB = B.length;
if(AA > BB) return false;
if(AA < BB) return true;
// AA = BB, compare indexes from biggest to smallest.
for(i = AA - 1; i >= 0; i -- )
{
if(A[i] < B[i]) return true;
if(A[i] > B[i]) return false;
}
return true;
}
/* MERGE THE INTEGER INDEXES OF AN ARRAY INTO ONE INTEGER */
function mergeArr(myArr)
{
myInt = 0;
x = 1;
for (i = 0; i < myArr.length; i ++ )
{
myInt = (myArr[i] * x) + myInt;
x = x * 10;
}
return myInt;
}
/* CONVERT A DECIMAL NUMBER INTO ANYBASE */
function newbase(decArr, base)
{
document.calc.converse.value += "\n\nNEW NUMBER = " + decArr + " TO BASE= " + base + "\n";
tempBase = base.toString();
baseArr = tempBase.split("").reverse();
digRes = [];
resultmult = [];
digit = 0;
oldigmult = [];
resultmult[0] = 1;
convArr = []
digits = 0;
decbase = 10;
for(i = 0; i < baseArr.length; i ++ )
{
baseArr[i] = parseInt(baseArr[i]);
}
for(i = 0; i < decArr.length; i ++ )
{
decArr[i] = parseInt(decArr[i]);
}
loop = true;
document.calc.converse.value += "\nFIND MULTIPLE OF BASE=EXPONENT ***LOOPONE START***\n"
// While resultmult < decArr
while(loop == true)
{
document.calc.converse.value += "Digits = " + digits + "\n";
console.log('step 1');
oldigmult[digits] = resultmult;
olddigits = digits;
document.calc.converse.value += "(baseArr*resultmult=resultmult) " + baseArr + " * " + resultmult;
orderArrayMult(10, baseArr, resultmult);
document.calc.converse.value += " = " + resultmult + "\n";
firstloop ++ ;
loop = arrCompareTwo(resultmult, decArr);
document.calc.converse.value += "While(resulmult<decarr) " + resultmult + " < " + decArr + " = " + loop + "\n";
if( ! loop)document.calc.converse.value += "**BREAK LOOPONE**\n";
digits ++ ;
}
counterbase = 10;
mymultbase = 10;
// While digits to convert
while(olddigits >= 0)
{
resultmult = [];
counterArr = [];
counterArr[0] = - 1;
looptwo = true;
document.calc.converse.value += "\nFIND DIGIT FOR EXPONENT MULTIPLE *** LOOPTWO START***\n"
// While resultmult < decArr
while (looptwo == true)
{
console.log('step 2');
// Make a copy of last resultmult before next multiplication
oldMultArr = resultmult.slice();
oldCountArr = counterArr;
secondloop ++ ;
counterArr = orderArrayAdd(counterbase, counterArr, plusOne);
document.calc.converse.value += "(counterArr*oldigmult[x]=resultmult) " + counterArr + " * " + oldigmult[olddigits];
console.log("old", oldigmult[olddigits]);
orderArrayMult(mymultbase, counterArr, oldigmult[olddigits]);
document.calc.converse.value += " = " + resultmult + "\n";
looptwo = arrCompareTwo(resultmult, decArr);
document.calc.converse.value += "While(resultmult<decArr) " + resultmult + " < " + decArr + " = " + looptwo + "\n";
if( ! looptwo)document.calc.converse.value += "**BREAK LOOPTWO**\n";
}
if (base > 10)
{
digtemp = mergeArr(oldCountArr);
}
else
{
digtemp = oldCountArr;
}
document.calc.converse.value += "MERGED " + digtemp + "\n";
convArr[olddigits] = digtemp;
document.calc.converse.value += "DIGITS NEWBASE " + convArr + "\n";
olddigits -- ;
document.calc.converse.value += "(decArr-oldMultArr=decArr) " + decArr + " - " + oldMultArr;
decArr = subtOrderArray(10, decArr, oldMultArr);
document.calc.converse.value += " = " + decArr + "\n";
checkifzero = decArr.join("");
testValZero = parseInt(checkifzero);
if(testValZero <= 0)
break;
}
// If decArr = 0 but there is still digits all equal 0 = ZERO
for(j = olddigits; j >= 0; j -- )
{
convArr[j] = 0;
document.calc.converse.value += "No residue ZeroFill Digits " + convArr + "\n";
}
document.calc.converse.value += "DIGITS NEWBASE " + convArr + "\n";
return convArr;
}
function prepSubt(subOne, subTwo)
{
subtbigArr = subOne.length;
subtsmallArr = subTwo.length
// Parse with zeros to get same length, and make sure they are integers.
for (i = 0; i < subtbigArr; i ++ )
{
subOne[i] = parseInt(subOne[i]);
subTwo[i] = parseInt(subTwo[i]);
if (isNaN(subOne[i])) subOne[i] = 0;
if (isNaN(subTwo[i])) subTwo[i] = 0;
}
}
/* CHANGE ORDER OF ARRAYS, SO SUBT ALWAYS CALLED WITH BIGGER OPERAND BEFORE LESSER */
function subtOrderArray(Abase, A, B)
{
return lessThan(A, B) ? resultmult = naiveSub(Abase, B, A) : resultmult = naiveSub(Abase, A, B);
}
/* SUBTRACT TWO THE TWO VARIABLES STORED IN SUBTONE AND SUBTTWO USING CHOSEN BASE */
function naiveSub(base, subOne, subTwo)
{
// document.write(subOne, "-", subTwo); p.stop();
prepSubt(subOne, subTwo);
result = [];
for (i = 0; i < subtsmallArr; i ++ )
{
j = 0;
if (subOne[i] < subTwo[i])
{
intBorrow ++ ;
subOne[i] += base;
j = i;
while (subOne[j + 1] == 0)
{
console.log('step 4');
subOne[j + 1] += base - 1;
j ++ ;
}
subOne[j + 1] += - 1;
}
j = 0;
result[i] = subOne[i] - subTwo[i];
intSub ++ ;
}
while(i < subtbigArr)
{
console.log('step 5');
result[i] = subOne[i];
i ++ ;
}
while (result[result.length - 1] == 0 && result.length - 1 > 0)
{
result.splice(result.length - 1, 1)
}
return result;
}
/* MULTIPLY THE TWO VARIABLES STORED IN MULTONE AND MULTTWO USING THE CHOSEN BASE */
function naiveMult(base, multOne, multTwo)
{
// document.calc.converse.value += "(multOne*multTwo) " + multOne + " * " + multTwo + "\n";
mbigArr = multOne.length;
msmallArr = multTwo.length
var total = [];
var addResult = [];
total[0] = 0;
for (var i = 0; i < msmallArr; i ++ )
{
multresult = [];
remainder = 0;
tempVal = 0;
tempDig = 0;
j = 0;
if(i > 0)
{
for(zero = 0; zero < i; zero ++ ) multresult[zero] = 0;
}
while(j < mbigArr)
{
console.log('step 6');
intMult ++ ;
tempVal = (multOne[j] * multTwo[i]) + remainder;
remainder = 0;
if (tempVal >= base)
{
tempDig = tempVal % base;
remainder = (tempVal - tempDig) / base;
multresult[j + i] = tempDig;
}
else
{
multresult[j + i] = tempVal;
}
j ++ ;
}
if(remainder != 0)
{
// Remainder after last digit multiplication
multresult[j + i] = remainder;
}
smallAddArr = multresult.length;
bigAddArr = total.length;
addResult = naiveAdd(base, multresult, total, smallAddArr, bigAddArr);
total = addResult;
}
while (total[total.length - 1] == 0 && total.length - 1 > 0)
{
total.splice(total.length - 1, 1)
}
return total;
}
/* Get which array shorter longer ADD */
function prepAdd(addOne, addTwo)
{
abigArr = addOne.length;
asmallArr = addTwo.length
}
function orderArrayAdd(Abase, A, B)
{
return lessThan(A, B) ? counterArr = naiveAdd(Abase, B, A) : counterArr = naiveAdd(Abase, A, B);
}
/* ADD TWO THE TWO VARIABLES STORED IN ADDONE AND ADDTWO USING CHOSEN BASE */
/* ADD TWO THE TWO VARIABLES STORED IN ADDONE AND ADDTWO USING CHOSEN BASE */
function naiveAdd(base, addOne, addTwo)
{
abigArr = addOne.length;
asmallArr = addTwo.length;
addResult = [];
remainder = 0;
// document.write(addOne, "jonas<br>");
for (i = 0; i < abigArr; i ++ )
{
intAdd ++ ;
addOne[i] = parseInt(addOne[i]);
addTwo[i] = parseInt(addTwo[i]);
if (isNaN(addOne[i])) addOne[i] = 0;
if (isNaN(addTwo[i])) addTwo[i] = 0;
addResult[i] = addOne[i] + addTwo[i] + remainder;
if (addResult[i] >= base)
{
addResult[i] = addResult[i] - base;
remainder = 1;
// intCarry ++ ;
}
else
{
remainder = 0;
}
}
// If strings of equal length but there is a remainder;
if (remainder == 1)
{
addResult[i ++ ] = 1;
}
else
{
addResult.splice(i, 1);
}
return addResult;
}
/* Get which array shorter longer MULT */
function prepMult(multOne, multTwo)
{
if(multOne.length > multTwo.length)
{
mbigArr = multOne.length;
msmallArr = multTwo.length
}
else
{
mbigArr = multTwo.length;
msmallArr = multOne.length;
}
}
/* CHANGE ORDER OF ARRAYS, SO MULT ALWAYS CALLED WITH BIGGER OPERAND BEFORE LESSER */
function orderArrayMult(Abase, A, B)
{
return lessThan(A, B) ? resultmult = naiveMult(Abase, B, A) : resultmult = naiveMult(Abase, A, B);
}
/* COMPARE SIZE OF TWO ARRAYS, IF A < B RETURN TRUE, IF B >= A RETURN FALSE */
function lessThan(A, B)
{
var AA = A.length;
var BB = B.length;
if(AA > BB) return false;
if(AA < BB) return true;
// AA = BB, compare indexes from biggest to smallest.
for(i = AA - 1; i >= 0; i -- )
{
if(A[i] < B[i]) return true;
if(A[i] > B[i]) return false;
}
return false;
}
/* COMPARE SIZE OF TWO ARRAYS, IF A < B RETURN TRUE, IF B >= A RETURN FALSE */
function arrCompare(A, B)
{
var AA = A.length;
var BB = B.length;
if(AA > BB) return false;
if(AA < BB) return true;
// AA = BB, compare indexes from biggest to smallest.
for(i = AA - 1; i >= 0; i -- )
{
if(A[i] < B[i]) return true;
if(A[i] > B[i]) return false;
}
return false;
}
/* RESET FORM FIELDS, FETCH BASE AND STRING TO EVALUATE */
function fetchVal()
{
document.calc.result.value = "";
document.calc.stats.value = "";
strEval = document.calc.eval.value;
base = document.calc.formbase.value;
base = parseInt(base);
}
/* SPLIT OPERAND PAIR AND DECIDE OPERATION */
function checkOperation()
{
if (result = strEval.indexOf("-") != - 1)
{
genArr = strEval.split("-");
operation = "-";
}
else if (result = strEval.indexOf("+") != - 1)
{
genArr = strEval.split("+");
operation = "+";
}
else if (result = strEval.indexOf("*") != - 1)
{
genArr = strEval.split("*");
operation = "*";
}
else if (result = strEval.indexOf("/") != - 1)
{
genArr = strEval.split("/");
operation = "/";
}
return genArr;
}
/* CALL THE CORRECT OPERATION, DEPENDING UPON SIGN */
function performOperation()
{
baseStr += " arrOne = " + arrOne;
baseStr += " arrTwo = " + arrTwo;
if (operation == "-")
{
document.calc.stats.value += "[OPERATION SUBTRACT] - \n";
out = subtOrderArray(base, arrOne, arrTwo);
}
else if (operation == "+")
{
document.calc.stats.value += "[OPERATION ADD] + \n";
out = orderArrayAdd(base, arrOne, arrTwo);
}
else if (operation == "*")
{
document.calc.stats.value += "[OPERATION MULTIPLICATION] * \n";
out = naiveMult(base, arrOne, arrTwo);
}
else if (operation == "/")
{
out = naiveDiv(base, arrOne, arrTwo);
}
}
/* DO CONVERSION OF OPERANDS TO BASE OF CHOICE */
function baseConversion()
{
arrOne = genArr[0].split("").reverse();
arrOne = newbase(arrOne, base);
document.calc.converse.value += "DIGITS NEWBASE " + arrOne + "\n";
aArr = arrOne.slice();
aArr.reverse();
if (base < 11)
{
bout = aArr.join("");
}
else
{
bout = aArr;
}
document.calc.result.value += bout + "\n";
document.calc.stats.value += "arrOne CONVERSION START" + "\n";
document.calc.stats.value += "New base = " + base + "\n";
document.calc.stats.value += "Arraycompares = " + comparearrays + "\n";
document.calc.stats.value += "First loop " + firstloop + " times\n";
document.calc.stats.value += "Second loop " + secondloop + " times\n";
document.calc.stats.value += "Subtractions = " + intSub + " times\n";
document.calc.stats.value += "Additions = " + intAdd + " times\n";
document.calc.stats.value += "Multiplications = " + intMult + " times\n";
document.calc.stats.value += "Converted base digits = " + bout.length + "\n";
document.calc.stats.value += "Converted arrOne " + bout + "\n";
// p.stop();
arrTwo = genArr[1].split("").reverse();
arrTwo = newbase(arrTwo, base);
bArr = arrTwo.slice();
bArr.reverse();
if (base < 11)
{
bout = bArr.join("");
}
else
{
bout = bArr;
}
document.calc.result.value += bout + "\n";
document.calc.stats.value += "arrTwo CONVERSION START" + "\n";
document.calc.stats.value += "New base = " + base + "\n";
document.calc.stats.value += "Arraycompares = " + comparearrays + "\n";
document.calc.stats.value += "First loop " + firstloop + " times\n";
document.calc.stats.value += "Second loop " + secondloop + " times\n";
document.calc.stats.value += "Subtractions = " + intSub + " times\n";
document.calc.stats.value += "Additions = " + intAdd + " times\n";
document.calc.stats.value += "Multiplications = " + intMult + " times\n";
document.calc.stats.value += "Converted base digits = " + bout.length + "\n";
document.calc.stats.value += "Converted arrOne " + bout + "\n";
}
/* SPLIT THE CHARACTERS OF THE TWO OPERAND STRINGS INTO ARRAY INDEXES AND THEN REVERSE ARRAY */
function stringToArr()
{
arrOne = genArr[0].split("").reverse();
tempA = arrOne;
arrTwo = genArr[1].split("").reverse();
tempB = arrTwo;
}
/* PRINT RESULT TO FORM */
function resOutForm()
{
document.calc.stats.value += "\nINT Mult operations = " + intMult + "\n";
document.calc.stats.value += "INT Add operations = " + intAdd + "\n";
document.calc.stats.value += "INT Sub operations = " + intSub + "\n";
document.calc.stats.value += "INT Borrow operations = " + intBorrow + "\n";
document.calc.stats.value += "INT Carry operations = " + intCarry + "\n";
tempC = out.reverse();
if(base < 11)
{
tempC = tempC.toString();
tempC = tempC.replace(/,/g, '')
}
document.calc.result.value += "=" + tempC + "\n";
}
/* GLOBAL VARIABLES AND ARRAY FORMAT */
/* THE NOTATION OF NUMERALS USE BASE TEN, SO FOR INDEX VALUES IN ARRAY IF BASE 2000, DIGITVALUE IN INDEX RANGE = (0 - 1999) */
/* VARIABLES ARE STORED REVERSED IN ARRAY */
/* WITH A SINGLE "DIGITPLACE/VALUE" OF THE CHOSEN BASE, HOLD AT EACH INDEX */
function MAIN()
{
comparearrays = 0;
firstloop = 0;
secondloop = 0;
thirdloop = 0;
plusOne = [];
plusOne[0] = 1;
/* INIT GLOBAL VARIABLES START */
arrOne = [];
arrTwo = [];
tempA = [];
tempB = [];
// Counters, adds, subtraction, multiplications
intMult = 0;
intAdd = 0;
intSub = 0;
intBorrow = 0;
intCarry = 0;
// Length of the operands
bigArr = 0;
smallArr = 0;
addBigLength = 0;
addSmallLength = 0;
baseStr = "";
// Reset variables from form
base = 0;
strEval = "";
fetchVal();
// Get stringsvalues from form
/* INIT END */
document.calc.stats.value = "";
document.calc.result.value = "";
document.calc.converse.value = "";
document.calc.trashcan.value = "";
/* MAIN FUNCTION */
genArr = checkOperation();
document.calc.stats.value += "Operand A decimal digits = " + genArr[0].length + "\n";
document.calc.stats.value += "Operand B decimal digits = " + genArr[1].length + "\n";
// Check which arithmetic opeation to do upon operand pair
if (base == 10)
{
stringToArr();
// Split the characters within string into digitplace indexes in an array
}
else
{
baseConversion();
// Do base conversion into the base from form
}
/* TIMER START */
var start = new Date().getTime();
// Start time the opeation
performOperation(); // Do the arithmetic operation stored in sign, upon operand pair
var end = new Date().getTime();
// End time the operation
var time = end - start;
resOutForm();
// Printout result
document.calc.stats.value += "Timing ms " + time + "\n";
}
</SCRIPT>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"/></head>
<body bgcolor="gold" onload="MAIN()">
<H3>SCRIPT DO ARITHMETICS IN ANYBASE USING BIG NUMBERS(NO PARSER, SO ONLY OPERAND PAIRS WITH *,+,-)</H3><P>
<FORM name=calc onSubmit="MAIN(); return false;"><B>
<input type=submit name="calc" value="Calc"> *use radiX* <input type="text" name="formbase" value="16" size="10">
INPUT base 10-> Evaluate <input type="text" name="eval" value="1267650600228229401496703205377*13734678641239234769" value="" size="60"><br>
<FONT COLOR="gray">
<table border=!>
<tr>
<th align=left>RESULT</th><th align=left>BASE CONVERSION (reversed arrays)</th>
</tr>
<tr>
<td>
<textarea name="result" rows="10" cols="80"></textarea>
</td>
<td>
<textarea name="converse" rows="10" cols="80"></textarea>
</td>
</tr>
<tr>
<th align=left>STATUS</th><th align=left>TRASHCAN</th>
</tr>
<tr>
<td>
<textarea name="stats" rows="10" cols="80"></textarea>
</td>
<td>
<textarea name="trashcan" rows="10" cols="80"></textarea>
</td>
</tr>
</table>
</FORM>
</body>
</html>