[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Bignumber conversion and arithmetic on unsigned operand pairs using +,-,* now work in anybase.

JT

5/20/2015 1:12:00 PM

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>
82 Answers

JT

5/20/2015 1:38:00 PM

0

Den onsdag 20 maj 2015 kl. 15:11:38 UTC+2 skrev jonas.t...@gmail.com:
> 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>

Can not say i testet it extensively, but it seem to hold for doing arithmetic in anybase and anynumber.

http://jt.node365.se/arithme...

Evertjan.

5/20/2015 3:59:00 PM

0

jonas.thornvall@gmail.com wrote on 20 mei 2015 in comp.lang.javascript:

> And i though Janevert said i could not do it, no overflow.

Impolite to misquote my name.

I never said you cannot do something,
only that you should not do some things.

I never discussed overflow with you.

I only critisize your methods
and understanding of mathmatics and scripting.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

JT

6/21/2015 4:12:00 PM

0

Den onsdag 20 maj 2015 kl. 17:58:52 UTC+2 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 20 mei 2015 in comp.lang.javascript:
>
> > And i though Janevert said i could not do it, no overflow.
>
> Impolite to misquote my name.
>
> I never said you cannot do something,
> only that you should not do some things.
>
> I never discussed overflow with you.
>
> I only critisize your methods
> and understanding of mathmatics and scripting.
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

Had some time and fixed bugs in bignumber baseconverter.
It seem correct now i tried 337^6 on calculator equal 1464803622199009.
I added 336 and get 1464803622199345.

Using base 337 i do get 1,0,0,0,0,0,336 which seem fair. I think the bases work up to 8 digit at least the numbersize should not matter more than timewise.

<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"><script language="Javascript">

/* CONVERT A DECIMAL NUMBER INTO ANYBASE */

function newbase(decArr, base)
{
tempBase = base.toString();
baseArr = tempBase.split("").reverse();
digRes = [];
resultmult = [];
digit = 0;
oldigmult = [];
resultmult[0] = 1;
convArr = []
digits = 0;
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.trash.value +="\n*LOOP USE BASE FIND CLOSEST EXPONENT THAT IS LESS THAN NUMBER* \n";
while(loop == true)
{
oldigmult[digits] = resultmult;
olddigits = digits;
orderArrayMult(10, baseArr, resultmult);
loop = arrCompareTwo(resultmult, decArr);
document.calc.trash.value +="RETURNED VALUE ->"+ loop+"\n";
digits ++ ;
}

while(olddigits >= 0)
{
resultmult = [];
counterArr = [];
looptwo = true;
mymin = 0;
mymax = base;
myval = parseInt(base / 2);
while (looptwo == true)
{
document.calc.trash.value +="\nTry digit ->"+myval + "\n";
tOne = myval.toString();
myDig = [];
for(y = 0; y < tOne.length; y ++ )
{
tTwo = tOne.charAt(y);
myDig[y] = parseInt(tTwo);
}
myDig.reverse();
orderArrayMult(10, myDig, oldigmult[olddigits]);
multarr = resultmult.slice();
looptwo = arrCompareTwo(resultmult, decArr);
document.calc.trash.value +="RETURNED VALUE ->"+ looptwo+"\n";
document.calc.trash.value += "MyVal=" + myDig + " ->MIN=" + mymin + " ->MAX=" + mymax + "\n";
if(looptwo == true && mymax != mymin + 1)
{
mymin = myval;
myval = mymin + ((mymax - mymin) / 2);
myval = parseInt(myval);
}
else
{
if (mymin + 1 == mymax)
{
looptwo = false;
}
else
{
looptwo = true;
mymax = myval;
myval = mymax - ((mymax - mymin) / 2);
myval = parseInt(myval);
}
}
}

myVar=myDig.join("");
document.calc.trash.value += "***FOUND NEWDIGIT*** ->" + myVar+"\n";
convArr[olddigits] = myVar;
olddigits -- ;
document.calc.trash.value += "decArr- multarr ->" + decArr + " - " + multarr;
decArr = naiveSub(10, decArr, multarr);
document.calc.trash.value += " = " + decArr + "\n";
checkifzero = decArr.join("");
testValZero = parseInt(checkifzero);
if(testValZero <= 0)
break;
}
for(j = olddigits; j >= 0; j -- )
{
convArr[j] = 0;
}
return convArr;
}

/* COMPARE SIZE OF TWO ARRAYS, IF A < B RETURN TRUE, IF B >= A RETURN FALSE */

function arrCompareTwo(A, B)
{
document.calc.trash.value += "COMPARE resultmult < decarr TRUE, IF resultmult >= decarr FALSE \n" +A+" VS "+B+"\n";
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;
}

/* 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;
}

function orderArrayMult(Abase, A, B)
{
return lessThan(A, B) ? resultmult = naiveMult(Abase, B, A) : resultmult = naiveMult(Abase, A, B);
}

function prepMult(multOne, multTwo)
{
if(multOne.length > multTwo.length)
{
mbigArr = multOne.length;
msmallArr = multTwo.length
}
else
{
mbigArr = multTwo.length;
msmallArr = multOne.length;
}

}

/* MULTIPLY THE TWO VARIABLES STORED IN MULTONE AND MULTTWO USING THE CHOSEN BASE */

function naiveMult(base, multOne, multTwo)
{

prepMult(multOne, multTwo);
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)
{
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)
}
document.calc.trash.value += total+" = " +multOne+" * "+multTwo+ "\n";
return total;
}

/* ADD TWO THE TWO VARIABLES STORED IN ADDONE AND ADDTWO USING CHOSEN BASE */

function naiveAdd(base, addOne, addTwo)
{
prepAdd(addOne, addTwo);
addResult = [];
remainder = 0;

for (i = 0; i < asmallArr; 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;
}
}
// Copying values from the shorter string
while(i < abigArr)
{
addOne[i] = parseInt(addOne[i]);
addResult[i] = addOne[i] + remainder;
remainder = 0;
i ++ ;
}
// If strings of equal length but there is a remainder;
if (remainder == 1) addResult[i ++ ] = 1;
else addResult.splice(i, 1);
return addResult;
}

function prepAdd(addOne, addTwo)
{
if(addOne.length > addTwo.length)
{
abigArr = addOne.length;
asmallArr = addTwo.length
}
else
{
abigArr = addTwo.length;
asmallArr = addOne.length;
}
// Parse with zeros to get same length, and make sure they are integers.
// for (i = 0; i < bigArr; i ++ )
// {
// multOne[i] = parseInt(multOne[i]);
// multTwo[i] = parseInt(multTwo[i]);
// if (isNaN(multOne[i])) multOne[i] = 0;
// if (isNaN(multTwo[i])) multTwo[i] = 0;
// }
}

/* FIX THIS LATER */

function prepSubt(subOne, subTwo)
{
if(subOne.length > subTwo.length)
{
bigArr = subOne.length;
smallArr = subTwo.length
}
else
{
bigArr = subTwo.length;
smallArr = subOne.length;
}
// Parse with zeros to get same length, and make sure they are integers.
for (i = 0; i < bigArr; 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;
}
}

/* SUBTRACT THE TWO VARIABLES STORED IN SUBONE AND SUBTWO USING THE CHOSEN BASE */

function naiveSub(base, subOne, subTwo)
{
prepSubt(subOne, subTwo);
result = [];
for (i = 0; i < smallArr; i ++ )
{
j = 0;

if (subOne[i] < subTwo[i])
{

intBorrow ++ ;
subOne[i] += base;
j = i;
while (subOne[j + 1] == 0)
{
subOne[j + 1] += base - 1;

j ++ ;
}
subOne[j + 1] += - 1;

}

j = 0;
result[i] = subOne[i] - subTwo[i];
intSub ++ ;
}
while(i < bigArr)
{
result[i] = subOne[i];
i ++ ;
}

while (result[result.length - 1] == 0 && result.length - 1 > 0)
{
result.splice(result.length - 1, 1)
}
return result;
}

/* RESET FORM FIELDS, FETCH BASE AND STRING TO EVALUATE */

function fetchVal()
{
document.calc.stats.value = "";
strEval = document.calc.eval.value;
genArr[0] = strEval;
base = document.calc.formbase.value;
base = parseInt(base);
}

/* DO CONVERSION OF OPERANDS TO BASE OF CHOICE */

function baseConversion()
{
arrOne = genArr[0].split("").reverse();
arrOne = newbase(arrOne, base);
arrOne.reverse();
if (base < 11)
{
bout = arrOne.join("");
}
else
{
bout = arrOne
}
for(j=0;j<arrOne.length;j++){
intRev=arrOne[j].toString();
arrOne[j]=intRev.split("").reverse().join("");
}
document.calc.stats.value += "Converted base digits = " + bout.length + "\n";
document.calc.stats.value += "Converted arrOne " + bout + "\n";
}

function orderArraySubt(Abase, A, B)
{
return lessThan(A, B) ? resultmult = naiveSub(Abase, B, A) : resultmult = naiveSub(Abase, A, B);
}

/* 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()
{

/* INIT GLOBAL VARIABLES START */
genArr = [];
baseArr = [];
resultmult = [];
plusOne = [];
plusOne[0] = 1;
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 = "";
document.calc.stats.value = "";
document.calc.trash.value = "";
document.calc.trash.value += "Array VALUES printed out are reversed\n";
fetchVal();
// Get stringsvalues from form

/* INIT END */
/* MAIN FUNCTION */
document.calc.stats.value += "Operand A decimal digits = " + strEval.length + "\n";
// Do base conversion into the base fetched from form
baseConversion();
}
</script>

<meta charset="utf-8"></head>
<body onload="MAIN()" bgcolor="gold">
<h3>BIG NUMBERS BASE CONVERTER (anybase) version 0.0001 ;)</h3><p>
</p><form name="calc" onsubmit="MAIN(); return false;"><b>
<input name="calc" value="Calc" type="submit"> *use radiX* <input name="formbase" value="337" size="10" type="text"><br><br>
Trash print<br>
<textarea name="trash" rows="20" cols="120"></textarea><br>
Base 10 value to convert<br>
<textarea name="eval" rows="2" cols="120">1464803622199345</textarea><br>
New base out<br>
<textarea name="stats" rows="40" cols="120"></textarea>
</form>
</body>
</html>

JT

6/21/2015 4:33:00 PM

0

Den söndag 21 juni 2015 kl. 18:12:20 UTC+2 skrev jonas.t...@gmail.com:
> Den onsdag 20 maj 2015 kl. 17:58:52 UTC+2 skrev Evertjan.:
> > jonas.thornvall@gmail.com wrote on 20 mei 2015 in comp.lang.javascript:
> >
> > > And i though Janevert said i could not do it, no overflow.
> >
> > Impolite to misquote my name.
> >
> > I never said you cannot do something,
> > only that you should not do some things.
> >
> > I never discussed overflow with you.
> >
> > I only critisize your methods
> > and understanding of mathmatics and scripting.
> >
> > --
> > Evertjan.
> > The Netherlands.
> > (Please change the x'es to dots in my emailaddress)
>
> Had some time and fixed bugs in bignumber baseconverter.
> It seem correct now i tried 337^6 on calculator equal 1464803622199009.
> I added 336 and get 1464803622199345.
>
> Using base 337 i do get 1,0,0,0,0,0,336 which seem fair. I think the bases work up to 8 digit at least the numbersize should not matter more than timewise.
>
> <html lang="en"><head>
> <meta http-equiv="content-type" content="text/html; charset=UTF-8"><script language="Javascript">
>
> /* CONVERT A DECIMAL NUMBER INTO ANYBASE */
>
> function newbase(decArr, base)
> {
> tempBase = base.toString();
> baseArr = tempBase.split("").reverse();
> digRes = [];
> resultmult = [];
> digit = 0;
> oldigmult = [];
> resultmult[0] = 1;
> convArr = []
> digits = 0;
> 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.trash.value +="\n*LOOP USE BASE FIND CLOSEST EXPONENT THAT IS LESS THAN NUMBER* \n";
> while(loop == true)
> {
> oldigmult[digits] = resultmult;
> olddigits = digits;
> orderArrayMult(10, baseArr, resultmult);
> loop = arrCompareTwo(resultmult, decArr);
> document.calc.trash.value +="RETURNED VALUE ->"+ loop+"\n";
> digits ++ ;
> }
>
> while(olddigits >= 0)
> {
> resultmult = [];
> counterArr = [];
> looptwo = true;
> mymin = 0;
> mymax = base;
> myval = parseInt(base / 2);
> while (looptwo == true)
> {
> document.calc.trash.value +="\nTry digit ->"+myval + "\n";
> tOne = myval.toString();
> myDig = [];
> for(y = 0; y < tOne.length; y ++ )
> {
> tTwo = tOne.charAt(y);
> myDig[y] = parseInt(tTwo);
> }
> myDig.reverse();
> orderArrayMult(10, myDig, oldigmult[olddigits]);
> multarr = resultmult.slice();
> looptwo = arrCompareTwo(resultmult, decArr);
> document.calc.trash.value +="RETURNED VALUE ->"+ looptwo+"\n";
> document.calc.trash.value += "MyVal=" + myDig + " ->MIN=" + mymin + " ->MAX=" + mymax + "\n";
> if(looptwo == true && mymax != mymin + 1)
> {
> mymin = myval;
> myval = mymin + ((mymax - mymin) / 2);
> myval = parseInt(myval);
> }
> else
> {
> if (mymin + 1 == mymax)
> {
> looptwo = false;
> }
> else
> {
> looptwo = true;
> mymax = myval;
> myval = mymax - ((mymax - mymin) / 2);
> myval = parseInt(myval);
> }
> }
> }
>
> myVar=myDig.join("");
> document.calc.trash.value += "***FOUND NEWDIGIT*** ->" + myVar+"\n";
> convArr[olddigits] = myVar;
> olddigits -- ;
> document.calc.trash.value += "decArr- multarr ->" + decArr + " - " + multarr;
> decArr = naiveSub(10, decArr, multarr);
> document.calc.trash.value += " = " + decArr + "\n";
> checkifzero = decArr.join("");
> testValZero = parseInt(checkifzero);
> if(testValZero <= 0)
> break;
> }
> for(j = olddigits; j >= 0; j -- )
> {
> convArr[j] = 0;
> }
> return convArr;
> }
>
> /* COMPARE SIZE OF TWO ARRAYS, IF A < B RETURN TRUE, IF B >= A RETURN FALSE */
>
> function arrCompareTwo(A, B)
> {
> document.calc.trash.value += "COMPARE resultmult < decarr TRUE, IF resultmult >= decarr FALSE \n" +A+" VS "+B+"\n";
> 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;
> }
>
> /* 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;
> }
>
> function orderArrayMult(Abase, A, B)
> {
> return lessThan(A, B) ? resultmult = naiveMult(Abase, B, A) : resultmult = naiveMult(Abase, A, B);
> }
>
> function prepMult(multOne, multTwo)
> {
> if(multOne.length > multTwo.length)
> {
> mbigArr = multOne.length;
> msmallArr = multTwo.length
> }
> else
> {
> mbigArr = multTwo.length;
> msmallArr = multOne.length;
> }
>
> }
>
> /* MULTIPLY THE TWO VARIABLES STORED IN MULTONE AND MULTTWO USING THE CHOSEN BASE */
>
> function naiveMult(base, multOne, multTwo)
> {
>
> prepMult(multOne, multTwo);
> 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)
> {
> 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)
> }
> document.calc.trash.value += total+" = " +multOne+" * "+multTwo+ "\n";
> return total;
> }
>
> /* ADD TWO THE TWO VARIABLES STORED IN ADDONE AND ADDTWO USING CHOSEN BASE */
>
> function naiveAdd(base, addOne, addTwo)
> {
> prepAdd(addOne, addTwo);
> addResult = [];
> remainder = 0;
>
> for (i = 0; i < asmallArr; 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;
> }
> }
> // Copying values from the shorter string
> while(i < abigArr)
> {
> addOne[i] = parseInt(addOne[i]);
> addResult[i] = addOne[i] + remainder;
> remainder = 0;
> i ++ ;
> }
> // If strings of equal length but there is a remainder;
> if (remainder == 1) addResult[i ++ ] = 1;
> else addResult.splice(i, 1);
> return addResult;
> }
>
> function prepAdd(addOne, addTwo)
> {
> if(addOne.length > addTwo.length)
> {
> abigArr = addOne.length;
> asmallArr = addTwo.length
> }
> else
> {
> abigArr = addTwo.length;
> asmallArr = addOne.length;
> }
> // Parse with zeros to get same length, and make sure they are integers.
> // for (i = 0; i < bigArr; i ++ )
> // {
> // multOne[i] = parseInt(multOne[i]);
> // multTwo[i] = parseInt(multTwo[i]);
> // if (isNaN(multOne[i])) multOne[i] = 0;
> // if (isNaN(multTwo[i])) multTwo[i] = 0;
> // }
> }
>
> /* FIX THIS LATER */
>
> function prepSubt(subOne, subTwo)
> {
> if(subOne.length > subTwo.length)
> {
> bigArr = subOne.length;
> smallArr = subTwo.length
> }
> else
> {
> bigArr = subTwo.length;
> smallArr = subOne.length;
> }
> // Parse with zeros to get same length, and make sure they are integers.
> for (i = 0; i < bigArr; 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;
> }
> }
>
> /* SUBTRACT THE TWO VARIABLES STORED IN SUBONE AND SUBTWO USING THE CHOSEN BASE */
>
> function naiveSub(base, subOne, subTwo)
> {
> prepSubt(subOne, subTwo);
> result = [];
> for (i = 0; i < smallArr; i ++ )
> {
> j = 0;
>
> if (subOne[i] < subTwo[i])
> {
>
> intBorrow ++ ;
> subOne[i] += base;
> j = i;
> while (subOne[j + 1] == 0)
> {
> subOne[j + 1] += base - 1;
>
> j ++ ;
> }
> subOne[j + 1] += - 1;
>
> }
>
> j = 0;
> result[i] = subOne[i] - subTwo[i];
> intSub ++ ;
> }
> while(i < bigArr)
> {
> result[i] = subOne[i];
> i ++ ;
> }
>
> while (result[result.length - 1] == 0 && result.length - 1 > 0)
> {
> result.splice(result.length - 1, 1)
> }
> return result;
> }
>
> /* RESET FORM FIELDS, FETCH BASE AND STRING TO EVALUATE */
>
> function fetchVal()
> {
> document.calc.stats.value = "";
> strEval = document.calc.eval.value;
> genArr[0] = strEval;
> base = document.calc.formbase.value;
> base = parseInt(base);
> }
>
> /* DO CONVERSION OF OPERANDS TO BASE OF CHOICE */
>
> function baseConversion()
> {
> arrOne = genArr[0].split("").reverse();
> arrOne = newbase(arrOne, base);
> arrOne.reverse();
> if (base < 11)
> {
> bout = arrOne.join("");
> }
> else
> {
> bout = arrOne
> }
> for(j=0;j<arrOne.length;j++){
> intRev=arrOne[j].toString();
> arrOne[j]=intRev.split("").reverse().join("");
> }
> document.calc.stats.value += "Converted base digits = " + bout.length + "\n";
> document.calc.stats.value += "Converted arrOne " + bout + "\n";
> }
>
> function orderArraySubt(Abase, A, B)
> {
> return lessThan(A, B) ? resultmult = naiveSub(Abase, B, A) : resultmult = naiveSub(Abase, A, B);
> }
>
> /* 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()
> {
>
> /* INIT GLOBAL VARIABLES START */
> genArr = [];
> baseArr = [];
> resultmult = [];
> plusOne = [];
> plusOne[0] = 1;
> 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 = "";
> document.calc.stats.value = "";
> document.calc.trash.value = "";
> document.calc.trash.value += "Array VALUES printed out are reversed\n";
> fetchVal();
> // Get stringsvalues from form
>
> /* INIT END */
> /* MAIN FUNCTION */
> document.calc.stats.value += "Operand A decimal digits = " + strEval.length + "\n";
> // Do base conversion into the base fetched from form
> baseConversion();
> }
> </script>
>
> <meta charset="utf-8"></head>
> <body onload="MAIN()" bgcolor="gold">
> <h3>BIG NUMBERS BASE CONVERTER (anybase) version 0.0001 ;)</h3><p>
> </p><form name="calc" onsubmit="MAIN(); return false;"><b>
> <input name="calc" value="Calc" type="submit"> *use radiX* <input name="formbase" value="337" size="10" type="text"><br><br>
> Trash print<br>
> <textarea name="trash" rows="20" cols="120"></textarea><br>
> Base 10 value to convert<br>
> <textarea name="eval" rows="2" cols="120">1464803622199345</textarea><br>
> New base out<br>
> <textarea name="stats" rows="40" cols="120"></textarea>
> </form>
> </body>
> </html>

And for those in doubt try a 100 digit number with a 8 digit base no problem.
http://jt.node365.se/baseconver...

JT

6/21/2015 5:14:00 PM

0

Den söndag 21 juni 2015 kl. 18:33:07 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 21 juni 2015 kl. 18:12:20 UTC+2 skrev jonas.t...@gmail.com:
> > Den onsdag 20 maj 2015 kl. 17:58:52 UTC+2 skrev Evertjan.:
> > > jonas.thornvall@gmail.com wrote on 20 mei 2015 in comp.lang.javascript:
> > >
> > > > And i though Janevert said i could not do it, no overflow.
> > >
> > > Impolite to misquote my name.
> > >
> > > I never said you cannot do something,
> > > only that you should not do some things.
> > >
> > > I never discussed overflow with you.
> > >
> > > I only critisize your methods
> > > and understanding of mathmatics and scripting.
> > >
> > > --
> > > Evertjan.
> > > The Netherlands.
> > > (Please change the x'es to dots in my emailaddress)
> >
> > Had some time and fixed bugs in bignumber baseconverter.
> > It seem correct now i tried 337^6 on calculator equal 1464803622199009.
> > I added 336 and get 1464803622199345.
> >
> > Using base 337 i do get 1,0,0,0,0,0,336 which seem fair. I think the bases work up to 8 digit at least the numbersize should not matter more than timewise.
> >
> > <html lang="en"><head>
> > <meta http-equiv="content-type" content="text/html; charset=UTF-8"><script language="Javascript">
> >
> > /* CONVERT A DECIMAL NUMBER INTO ANYBASE */
> >
> > function newbase(decArr, base)
> > {
> > tempBase = base.toString();
> > baseArr = tempBase.split("").reverse();
> > digRes = [];
> > resultmult = [];
> > digit = 0;
> > oldigmult = [];
> > resultmult[0] = 1;
> > convArr = []
> > digits = 0;
> > 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.trash.value +="\n*LOOP USE BASE FIND CLOSEST EXPONENT THAT IS LESS THAN NUMBER* \n";
> > while(loop == true)
> > {
> > oldigmult[digits] = resultmult;
> > olddigits = digits;
> > orderArrayMult(10, baseArr, resultmult);
> > loop = arrCompareTwo(resultmult, decArr);
> > document.calc.trash.value +="RETURNED VALUE ->"+ loop+"\n";
> > digits ++ ;
> > }
> >
> > while(olddigits >= 0)
> > {
> > resultmult = [];
> > counterArr = [];
> > looptwo = true;
> > mymin = 0;
> > mymax = base;
> > myval = parseInt(base / 2);
> > while (looptwo == true)
> > {
> > document.calc.trash.value +="\nTry digit ->"+myval + "\n";
> > tOne = myval.toString();
> > myDig = [];
> > for(y = 0; y < tOne.length; y ++ )
> > {
> > tTwo = tOne.charAt(y);
> > myDig[y] = parseInt(tTwo);
> > }
> > myDig.reverse();
> > orderArrayMult(10, myDig, oldigmult[olddigits]);
> > multarr = resultmult.slice();
> > looptwo = arrCompareTwo(resultmult, decArr);
> > document.calc.trash.value +="RETURNED VALUE ->"+ looptwo+"\n";
> > document.calc.trash.value += "MyVal=" + myDig + " ->MIN=" + mymin + " ->MAX=" + mymax + "\n";
> > if(looptwo == true && mymax != mymin + 1)
> > {
> > mymin = myval;
> > myval = mymin + ((mymax - mymin) / 2);
> > myval = parseInt(myval);
> > }
> > else
> > {
> > if (mymin + 1 == mymax)
> > {
> > looptwo = false;
> > }
> > else
> > {
> > looptwo = true;
> > mymax = myval;
> > myval = mymax - ((mymax - mymin) / 2);
> > myval = parseInt(myval);
> > }
> > }
> > }
> >
> > myVar=myDig.join("");
> > document.calc.trash.value += "***FOUND NEWDIGIT*** ->" + myVar+"\n";
> > convArr[olddigits] = myVar;
> > olddigits -- ;
> > document.calc.trash.value += "decArr- multarr ->" + decArr + " - " + multarr;
> > decArr = naiveSub(10, decArr, multarr);
> > document.calc.trash.value += " = " + decArr + "\n";
> > checkifzero = decArr.join("");
> > testValZero = parseInt(checkifzero);
> > if(testValZero <= 0)
> > break;
> > }
> > for(j = olddigits; j >= 0; j -- )
> > {
> > convArr[j] = 0;
> > }
> > return convArr;
> > }
> >
> > /* COMPARE SIZE OF TWO ARRAYS, IF A < B RETURN TRUE, IF B >= A RETURN FALSE */
> >
> > function arrCompareTwo(A, B)
> > {
> > document.calc.trash.value += "COMPARE resultmult < decarr TRUE, IF resultmult >= decarr FALSE \n" +A+" VS "+B+"\n";
> > 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;
> > }
> >
> > /* 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;
> > }
> >
> > function orderArrayMult(Abase, A, B)
> > {
> > return lessThan(A, B) ? resultmult = naiveMult(Abase, B, A) : resultmult = naiveMult(Abase, A, B);
> > }
> >
> > function prepMult(multOne, multTwo)
> > {
> > if(multOne.length > multTwo.length)
> > {
> > mbigArr = multOne.length;
> > msmallArr = multTwo.length
> > }
> > else
> > {
> > mbigArr = multTwo.length;
> > msmallArr = multOne.length;
> > }
> >
> > }
> >
> > /* MULTIPLY THE TWO VARIABLES STORED IN MULTONE AND MULTTWO USING THE CHOSEN BASE */
> >
> > function naiveMult(base, multOne, multTwo)
> > {
> >
> > prepMult(multOne, multTwo);
> > 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)
> > {
> > 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)
> > }
> > document.calc.trash.value += total+" = " +multOne+" * "+multTwo+ "\n";
> > return total;
> > }
> >
> > /* ADD TWO THE TWO VARIABLES STORED IN ADDONE AND ADDTWO USING CHOSEN BASE */
> >
> > function naiveAdd(base, addOne, addTwo)
> > {
> > prepAdd(addOne, addTwo);
> > addResult = [];
> > remainder = 0;
> >
> > for (i = 0; i < asmallArr; 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;
> > }
> > }
> > // Copying values from the shorter string
> > while(i < abigArr)
> > {
> > addOne[i] = parseInt(addOne[i]);
> > addResult[i] = addOne[i] + remainder;
> > remainder = 0;
> > i ++ ;
> > }
> > // If strings of equal length but there is a remainder;
> > if (remainder == 1) addResult[i ++ ] = 1;
> > else addResult.splice(i, 1);
> > return addResult;
> > }
> >
> > function prepAdd(addOne, addTwo)
> > {
> > if(addOne.length > addTwo.length)
> > {
> > abigArr = addOne.length;
> > asmallArr = addTwo.length
> > }
> > else
> > {
> > abigArr = addTwo.length;
> > asmallArr = addOne.length;
> > }
> > // Parse with zeros to get same length, and make sure they are integers.
> > // for (i = 0; i < bigArr; i ++ )
> > // {
> > // multOne[i] = parseInt(multOne[i]);
> > // multTwo[i] = parseInt(multTwo[i]);
> > // if (isNaN(multOne[i])) multOne[i] = 0;
> > // if (isNaN(multTwo[i])) multTwo[i] = 0;
> > // }
> > }
> >
> > /* FIX THIS LATER */
> >
> > function prepSubt(subOne, subTwo)
> > {
> > if(subOne.length > subTwo.length)
> > {
> > bigArr = subOne.length;
> > smallArr = subTwo.length
> > }
> > else
> > {
> > bigArr = subTwo.length;
> > smallArr = subOne.length;
> > }
> > // Parse with zeros to get same length, and make sure they are integers.
> > for (i = 0; i < bigArr; 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;
> > }
> > }
> >
> > /* SUBTRACT THE TWO VARIABLES STORED IN SUBONE AND SUBTWO USING THE CHOSEN BASE */
> >
> > function naiveSub(base, subOne, subTwo)
> > {
> > prepSubt(subOne, subTwo);
> > result = [];
> > for (i = 0; i < smallArr; i ++ )
> > {
> > j = 0;
> >
> > if (subOne[i] < subTwo[i])
> > {
> >
> > intBorrow ++ ;
> > subOne[i] += base;
> > j = i;
> > while (subOne[j + 1] == 0)
> > {
> > subOne[j + 1] += base - 1;
> >
> > j ++ ;
> > }
> > subOne[j + 1] += - 1;
> >
> > }
> >
> > j = 0;
> > result[i] = subOne[i] - subTwo[i];
> > intSub ++ ;
> > }
> > while(i < bigArr)
> > {
> > result[i] = subOne[i];
> > i ++ ;
> > }
> >
> > while (result[result.length - 1] == 0 && result.length - 1 > 0)
> > {
> > result.splice(result.length - 1, 1)
> > }
> > return result;
> > }
> >
> > /* RESET FORM FIELDS, FETCH BASE AND STRING TO EVALUATE */
> >
> > function fetchVal()
> > {
> > document.calc.stats.value = "";
> > strEval = document.calc.eval.value;
> > genArr[0] = strEval;
> > base = document.calc.formbase.value;
> > base = parseInt(base);
> > }
> >
> > /* DO CONVERSION OF OPERANDS TO BASE OF CHOICE */
> >
> > function baseConversion()
> > {
> > arrOne = genArr[0].split("").reverse();
> > arrOne = newbase(arrOne, base);
> > arrOne.reverse();
> > if (base < 11)
> > {
> > bout = arrOne.join("");
> > }
> > else
> > {
> > bout = arrOne
> > }
> > for(j=0;j<arrOne.length;j++){
> > intRev=arrOne[j].toString();
> > arrOne[j]=intRev.split("").reverse().join("");
> > }
> > document.calc.stats.value += "Converted base digits = " + bout.length + "\n";
> > document.calc.stats.value += "Converted arrOne " + bout + "\n";
> > }
> >
> > function orderArraySubt(Abase, A, B)
> > {
> > return lessThan(A, B) ? resultmult = naiveSub(Abase, B, A) : resultmult = naiveSub(Abase, A, B);
> > }
> >
> > /* 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()
> > {
> >
> > /* INIT GLOBAL VARIABLES START */
> > genArr = [];
> > baseArr = [];
> > resultmult = [];
> > plusOne = [];
> > plusOne[0] = 1;
> > 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 = "";
> > document.calc.stats.value = "";
> > document.calc.trash.value = "";
> > document.calc.trash.value += "Array VALUES printed out are reversed\n";
> > fetchVal();
> > // Get stringsvalues from form
> >
> > /* INIT END */
> > /* MAIN FUNCTION */
> > document.calc.stats.value += "Operand A decimal digits = " + strEval.length + "\n";
> > // Do base conversion into the base fetched from form
> > baseConversion();
> > }
> > </script>
> >
> > <meta charset="utf-8"></head>
> > <body onload="MAIN()" bgcolor="gold">
> > <h3>BIG NUMBERS BASE CONVERTER (anybase) version 0.0001 ;)</h3><p>
> > </p><form name="calc" onsubmit="MAIN(); return false;"><b>
> > <input name="calc" value="Calc" type="submit"> *use radiX* <input name="formbase" value="337" size="10" type="text"><br><br>
> > Trash print<br>
> > <textarea name="trash" rows="20" cols="120"></textarea><br>
> > Base 10 value to convert<br>
> > <textarea name="eval" rows="2" cols="120">1464803622199345</textarea><br>
> > New base out<br>
> > <textarea name="stats" rows="40" cols="120"></textarea>
> > </form>
> > </body>
> > </html>
>
> And for those in doubt try a 100 digit number with a 8 digit base no problem.
> http://jt.node365.se/baseconver...

Any improvements suggestion?

JT

6/21/2015 6:09:00 PM

0

Den söndag 21 juni 2015 kl. 19:13:41 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 21 juni 2015 kl. 18:33:07 UTC+2 skrev jonas.t...@gmail.com:
> > Den söndag 21 juni 2015 kl. 18:12:20 UTC+2 skrev jonas.t...@gmail.com:
> > > Den onsdag 20 maj 2015 kl. 17:58:52 UTC+2 skrev Evertjan.:
> > > > jonas.thornvall@gmail.com wrote on 20 mei 2015 in comp.lang.javascript:
> > > >
> > > > > And i though Janevert said i could not do it, no overflow.
> > > >
> > > > Impolite to misquote my name.
> > > >
> > > > I never said you cannot do something,
> > > > only that you should not do some things.
> > > >
> > > > I never discussed overflow with you.
> > > >
> > > > I only critisize your methods
> > > > and understanding of mathmatics and scripting.
> > > >
> > > > --
> > > > Evertjan.
> > > > The Netherlands.
> > > > (Please change the x'es to dots in my emailaddress)
> > >
> > > Had some time and fixed bugs in bignumber baseconverter.
> > > It seem correct now i tried 337^6 on calculator equal 1464803622199009.
> > > I added 336 and get 1464803622199345.
> > >
> > > Using base 337 i do get 1,0,0,0,0,0,336 which seem fair. I think the bases work up to 8 digit at least the numbersize should not matter more than timewise.
> > >
> > > <html lang="en"><head>
> > > <meta http-equiv="content-type" content="text/html; charset=UTF-8"><script language="Javascript">
> > >
> > > /* CONVERT A DECIMAL NUMBER INTO ANYBASE */
> > >
> > > function newbase(decArr, base)
> > > {
> > > tempBase = base.toString();
> > > baseArr = tempBase.split("").reverse();
> > > digRes = [];
> > > resultmult = [];
> > > digit = 0;
> > > oldigmult = [];
> > > resultmult[0] = 1;
> > > convArr = []
> > > digits = 0;
> > > 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.trash.value +="\n*LOOP USE BASE FIND CLOSEST EXPONENT THAT IS LESS THAN NUMBER* \n";
> > > while(loop == true)
> > > {
> > > oldigmult[digits] = resultmult;
> > > olddigits = digits;
> > > orderArrayMult(10, baseArr, resultmult);
> > > loop = arrCompareTwo(resultmult, decArr);
> > > document.calc.trash.value +="RETURNED VALUE ->"+ loop+"\n";
> > > digits ++ ;
> > > }
> > >
> > > while(olddigits >= 0)
> > > {
> > > resultmult = [];
> > > counterArr = [];
> > > looptwo = true;
> > > mymin = 0;
> > > mymax = base;
> > > myval = parseInt(base / 2);
> > > while (looptwo == true)
> > > {
> > > document.calc.trash.value +="\nTry digit ->"+myval + "\n";
> > > tOne = myval.toString();
> > > myDig = [];
> > > for(y = 0; y < tOne.length; y ++ )
> > > {
> > > tTwo = tOne.charAt(y);
> > > myDig[y] = parseInt(tTwo);
> > > }
> > > myDig.reverse();
> > > orderArrayMult(10, myDig, oldigmult[olddigits]);
> > > multarr = resultmult.slice();
> > > looptwo = arrCompareTwo(resultmult, decArr);
> > > document.calc.trash.value +="RETURNED VALUE ->"+ looptwo+"\n";
> > > document.calc.trash.value += "MyVal=" + myDig + " ->MIN=" + mymin + " ->MAX=" + mymax + "\n";
> > > if(looptwo == true && mymax != mymin + 1)
> > > {
> > > mymin = myval;
> > > myval = mymin + ((mymax - mymin) / 2);
> > > myval = parseInt(myval);
> > > }
> > > else
> > > {
> > > if (mymin + 1 == mymax)
> > > {
> > > looptwo = false;
> > > }
> > > else
> > > {
> > > looptwo = true;
> > > mymax = myval;
> > > myval = mymax - ((mymax - mymin) / 2);
> > > myval = parseInt(myval);
> > > }
> > > }
> > > }
> > >
> > > myVar=myDig.join("");
> > > document.calc.trash.value += "***FOUND NEWDIGIT*** ->" + myVar+"\n";
> > > convArr[olddigits] = myVar;
> > > olddigits -- ;
> > > document.calc.trash.value += "decArr- multarr ->" + decArr + " - " + multarr;
> > > decArr = naiveSub(10, decArr, multarr);
> > > document.calc.trash.value += " = " + decArr + "\n";
> > > checkifzero = decArr.join("");
> > > testValZero = parseInt(checkifzero);
> > > if(testValZero <= 0)
> > > break;
> > > }
> > > for(j = olddigits; j >= 0; j -- )
> > > {
> > > convArr[j] = 0;
> > > }
> > > return convArr;
> > > }
> > >
> > > /* COMPARE SIZE OF TWO ARRAYS, IF A < B RETURN TRUE, IF B >= A RETURN FALSE */
> > >
> > > function arrCompareTwo(A, B)
> > > {
> > > document.calc.trash.value += "COMPARE resultmult < decarr TRUE, IF resultmult >= decarr FALSE \n" +A+" VS "+B+"\n";
> > > 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;
> > > }
> > >
> > > /* 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;
> > > }
> > >
> > > function orderArrayMult(Abase, A, B)
> > > {
> > > return lessThan(A, B) ? resultmult = naiveMult(Abase, B, A) : resultmult = naiveMult(Abase, A, B);
> > > }
> > >
> > > function prepMult(multOne, multTwo)
> > > {
> > > if(multOne.length > multTwo.length)
> > > {
> > > mbigArr = multOne.length;
> > > msmallArr = multTwo.length
> > > }
> > > else
> > > {
> > > mbigArr = multTwo.length;
> > > msmallArr = multOne.length;
> > > }
> > >
> > > }
> > >
> > > /* MULTIPLY THE TWO VARIABLES STORED IN MULTONE AND MULTTWO USING THE CHOSEN BASE */
> > >
> > > function naiveMult(base, multOne, multTwo)
> > > {
> > >
> > > prepMult(multOne, multTwo);
> > > 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)
> > > {
> > > 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)
> > > }
> > > document.calc.trash.value += total+" = " +multOne+" * "+multTwo+ "\n";
> > > return total;
> > > }
> > >
> > > /* ADD TWO THE TWO VARIABLES STORED IN ADDONE AND ADDTWO USING CHOSEN BASE */
> > >
> > > function naiveAdd(base, addOne, addTwo)
> > > {
> > > prepAdd(addOne, addTwo);
> > > addResult = [];
> > > remainder = 0;
> > >
> > > for (i = 0; i < asmallArr; 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;
> > > }
> > > }
> > > // Copying values from the shorter string
> > > while(i < abigArr)
> > > {
> > > addOne[i] = parseInt(addOne[i]);
> > > addResult[i] = addOne[i] + remainder;
> > > remainder = 0;
> > > i ++ ;
> > > }
> > > // If strings of equal length but there is a remainder;
> > > if (remainder == 1) addResult[i ++ ] = 1;
> > > else addResult.splice(i, 1);
> > > return addResult;
> > > }
> > >
> > > function prepAdd(addOne, addTwo)
> > > {
> > > if(addOne.length > addTwo.length)
> > > {
> > > abigArr = addOne.length;
> > > asmallArr = addTwo.length
> > > }
> > > else
> > > {
> > > abigArr = addTwo.length;
> > > asmallArr = addOne.length;
> > > }
> > > // Parse with zeros to get same length, and make sure they are integers.
> > > // for (i = 0; i < bigArr; i ++ )
> > > // {
> > > // multOne[i] = parseInt(multOne[i]);
> > > // multTwo[i] = parseInt(multTwo[i]);
> > > // if (isNaN(multOne[i])) multOne[i] = 0;
> > > // if (isNaN(multTwo[i])) multTwo[i] = 0;
> > > // }
> > > }
> > >
> > > /* FIX THIS LATER */
> > >
> > > function prepSubt(subOne, subTwo)
> > > {
> > > if(subOne.length > subTwo.length)
> > > {
> > > bigArr = subOne.length;
> > > smallArr = subTwo.length
> > > }
> > > else
> > > {
> > > bigArr = subTwo.length;
> > > smallArr = subOne.length;
> > > }
> > > // Parse with zeros to get same length, and make sure they are integers.
> > > for (i = 0; i < bigArr; 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;
> > > }
> > > }
> > >
> > > /* SUBTRACT THE TWO VARIABLES STORED IN SUBONE AND SUBTWO USING THE CHOSEN BASE */
> > >
> > > function naiveSub(base, subOne, subTwo)
> > > {
> > > prepSubt(subOne, subTwo);
> > > result = [];
> > > for (i = 0; i < smallArr; i ++ )
> > > {
> > > j = 0;
> > >
> > > if (subOne[i] < subTwo[i])
> > > {
> > >
> > > intBorrow ++ ;
> > > subOne[i] += base;
> > > j = i;
> > > while (subOne[j + 1] == 0)
> > > {
> > > subOne[j + 1] += base - 1;
> > >
> > > j ++ ;
> > > }
> > > subOne[j + 1] += - 1;
> > >
> > > }
> > >
> > > j = 0;
> > > result[i] = subOne[i] - subTwo[i];
> > > intSub ++ ;
> > > }
> > > while(i < bigArr)
> > > {
> > > result[i] = subOne[i];
> > > i ++ ;
> > > }
> > >
> > > while (result[result.length - 1] == 0 && result.length - 1 > 0)
> > > {
> > > result.splice(result.length - 1, 1)
> > > }
> > > return result;
> > > }
> > >
> > > /* RESET FORM FIELDS, FETCH BASE AND STRING TO EVALUATE */
> > >
> > > function fetchVal()
> > > {
> > > document.calc.stats.value = "";
> > > strEval = document.calc.eval.value;
> > > genArr[0] = strEval;
> > > base = document.calc.formbase.value;
> > > base = parseInt(base);
> > > }
> > >
> > > /* DO CONVERSION OF OPERANDS TO BASE OF CHOICE */
> > >
> > > function baseConversion()
> > > {
> > > arrOne = genArr[0].split("").reverse();
> > > arrOne = newbase(arrOne, base);
> > > arrOne.reverse();
> > > if (base < 11)
> > > {
> > > bout = arrOne.join("");
> > > }
> > > else
> > > {
> > > bout = arrOne
> > > }
> > > for(j=0;j<arrOne.length;j++){
> > > intRev=arrOne[j].toString();
> > > arrOne[j]=intRev.split("").reverse().join("");
> > > }
> > > document.calc.stats.value += "Converted base digits = " + bout.length + "\n";
> > > document.calc.stats.value += "Converted arrOne " + bout + "\n";
> > > }
> > >
> > > function orderArraySubt(Abase, A, B)
> > > {
> > > return lessThan(A, B) ? resultmult = naiveSub(Abase, B, A) : resultmult = naiveSub(Abase, A, B);
> > > }
> > >
> > > /* 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()
> > > {
> > >
> > > /* INIT GLOBAL VARIABLES START */
> > > genArr = [];
> > > baseArr = [];
> > > resultmult = [];
> > > plusOne = [];
> > > plusOne[0] = 1;
> > > 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 = "";
> > > document.calc.stats.value = "";
> > > document.calc.trash.value = "";
> > > document.calc.trash.value += "Array VALUES printed out are reversed\n";
> > > fetchVal();
> > > // Get stringsvalues from form
> > >
> > > /* INIT END */
> > > /* MAIN FUNCTION */
> > > document.calc.stats.value += "Operand A decimal digits = " + strEval.length + "\n";
> > > // Do base conversion into the base fetched from form
> > > baseConversion();
> > > }
> > > </script>
> > >
> > > <meta charset="utf-8"></head>
> > > <body onload="MAIN()" bgcolor="gold">
> > > <h3>BIG NUMBERS BASE CONVERTER (anybase) version 0.0001 ;)</h3><p>
> > > </p><form name="calc" onsubmit="MAIN(); return false;"><b>
> > > <input name="calc" value="Calc" type="submit"> *use radiX* <input name="formbase" value="337" size="10" type="text"><br><br>
> > > Trash print<br>
> > > <textarea name="trash" rows="20" cols="120"></textarea><br>
> > > Base 10 value to convert<br>
> > > <textarea name="eval" rows="2" cols="120">1464803622199345</textarea><br>
> > > New base out<br>
> > > <textarea name="stats" rows="40" cols="120"></textarea>
> > > </form>
> > > </body>
> > > </html>
> >
> > And for those in doubt try a 100 digit number with a 8 digit base no problem.
> > http://jt.node365.se/baseconver...
>
> Any improvements suggestion?

The conversion is somewhat probalistic does that mean the algorithm work in sublinear time?

JT

6/21/2015 6:10:00 PM

0

Den söndag 21 juni 2015 kl. 20:08:38 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 21 juni 2015 kl. 19:13:41 UTC+2 skrev jonas.t...@gmail.com:
> > Den söndag 21 juni 2015 kl. 18:33:07 UTC+2 skrev jonas.t...@gmail.com:
> > > Den söndag 21 juni 2015 kl. 18:12:20 UTC+2 skrev jonas.t...@gmail.com:
> > > > Den onsdag 20 maj 2015 kl. 17:58:52 UTC+2 skrev Evertjan.:
> > > > > jonas.thornvall@gmail.com wrote on 20 mei 2015 in comp.lang.javascript:
> > > > >
> > > > > > And i though Janevert said i could not do it, no overflow.
> > > > >
> > > > > Impolite to misquote my name.
> > > > >
> > > > > I never said you cannot do something,
> > > > > only that you should not do some things.
> > > > >
> > > > > I never discussed overflow with you.
> > > > >
> > > > > I only critisize your methods
> > > > > and understanding of mathmatics and scripting.
> > > > >
> > > > > --
> > > > > Evertjan.
> > > > > The Netherlands.
> > > > > (Please change the x'es to dots in my emailaddress)
> > > >
> > > > Had some time and fixed bugs in bignumber baseconverter.
> > > > It seem correct now i tried 337^6 on calculator equal 1464803622199009.
> > > > I added 336 and get 1464803622199345.
> > > >
> > > > Using base 337 i do get 1,0,0,0,0,0,336 which seem fair. I think the bases work up to 8 digit at least the numbersize should not matter more than timewise.
> > > >
> > > > <html lang="en"><head>
> > > > <meta http-equiv="content-type" content="text/html; charset=UTF-8"><script language="Javascript">
> > > >
> > > > /* CONVERT A DECIMAL NUMBER INTO ANYBASE */
> > > >
> > > > function newbase(decArr, base)
> > > > {
> > > > tempBase = base.toString();
> > > > baseArr = tempBase.split("").reverse();
> > > > digRes = [];
> > > > resultmult = [];
> > > > digit = 0;
> > > > oldigmult = [];
> > > > resultmult[0] = 1;
> > > > convArr = []
> > > > digits = 0;
> > > > 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.trash.value +="\n*LOOP USE BASE FIND CLOSEST EXPONENT THAT IS LESS THAN NUMBER* \n";
> > > > while(loop == true)
> > > > {
> > > > oldigmult[digits] = resultmult;
> > > > olddigits = digits;
> > > > orderArrayMult(10, baseArr, resultmult);
> > > > loop = arrCompareTwo(resultmult, decArr);
> > > > document.calc.trash.value +="RETURNED VALUE ->"+ loop+"\n";
> > > > digits ++ ;
> > > > }
> > > >
> > > > while(olddigits >= 0)
> > > > {
> > > > resultmult = [];
> > > > counterArr = [];
> > > > looptwo = true;
> > > > mymin = 0;
> > > > mymax = base;
> > > > myval = parseInt(base / 2);
> > > > while (looptwo == true)
> > > > {
> > > > document.calc.trash.value +="\nTry digit ->"+myval + "\n";
> > > > tOne = myval.toString();
> > > > myDig = [];
> > > > for(y = 0; y < tOne.length; y ++ )
> > > > {
> > > > tTwo = tOne.charAt(y);
> > > > myDig[y] = parseInt(tTwo);
> > > > }
> > > > myDig.reverse();
> > > > orderArrayMult(10, myDig, oldigmult[olddigits]);
> > > > multarr = resultmult.slice();
> > > > looptwo = arrCompareTwo(resultmult, decArr);
> > > > document.calc.trash.value +="RETURNED VALUE ->"+ looptwo+"\n";
> > > > document.calc.trash.value += "MyVal=" + myDig + " ->MIN=" + mymin + " ->MAX=" + mymax + "\n";
> > > > if(looptwo == true && mymax != mymin + 1)
> > > > {
> > > > mymin = myval;
> > > > myval = mymin + ((mymax - mymin) / 2);
> > > > myval = parseInt(myval);
> > > > }
> > > > else
> > > > {
> > > > if (mymin + 1 == mymax)
> > > > {
> > > > looptwo = false;
> > > > }
> > > > else
> > > > {
> > > > looptwo = true;
> > > > mymax = myval;
> > > > myval = mymax - ((mymax - mymin) / 2);
> > > > myval = parseInt(myval);
> > > > }
> > > > }
> > > > }
> > > >
> > > > myVar=myDig.join("");
> > > > document.calc.trash.value += "***FOUND NEWDIGIT*** ->" + myVar+"\n";
> > > > convArr[olddigits] = myVar;
> > > > olddigits -- ;
> > > > document.calc.trash.value += "decArr- multarr ->" + decArr + " - " + multarr;
> > > > decArr = naiveSub(10, decArr, multarr);
> > > > document.calc.trash.value += " = " + decArr + "\n";
> > > > checkifzero = decArr.join("");
> > > > testValZero = parseInt(checkifzero);
> > > > if(testValZero <= 0)
> > > > break;
> > > > }
> > > > for(j = olddigits; j >= 0; j -- )
> > > > {
> > > > convArr[j] = 0;
> > > > }
> > > > return convArr;
> > > > }
> > > >
> > > > /* COMPARE SIZE OF TWO ARRAYS, IF A < B RETURN TRUE, IF B >= A RETURN FALSE */
> > > >
> > > > function arrCompareTwo(A, B)
> > > > {
> > > > document.calc.trash.value += "COMPARE resultmult < decarr TRUE, IF resultmult >= decarr FALSE \n" +A+" VS "+B+"\n";
> > > > 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;
> > > > }
> > > >
> > > > /* 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;
> > > > }
> > > >
> > > > function orderArrayMult(Abase, A, B)
> > > > {
> > > > return lessThan(A, B) ? resultmult = naiveMult(Abase, B, A) : resultmult = naiveMult(Abase, A, B);
> > > > }
> > > >
> > > > function prepMult(multOne, multTwo)
> > > > {
> > > > if(multOne.length > multTwo.length)
> > > > {
> > > > mbigArr = multOne.length;
> > > > msmallArr = multTwo.length
> > > > }
> > > > else
> > > > {
> > > > mbigArr = multTwo.length;
> > > > msmallArr = multOne.length;
> > > > }
> > > >
> > > > }
> > > >
> > > > /* MULTIPLY THE TWO VARIABLES STORED IN MULTONE AND MULTTWO USING THE CHOSEN BASE */
> > > >
> > > > function naiveMult(base, multOne, multTwo)
> > > > {
> > > >
> > > > prepMult(multOne, multTwo);
> > > > 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)
> > > > {
> > > > 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)
> > > > }
> > > > document.calc.trash.value += total+" = " +multOne+" * "+multTwo+ "\n";
> > > > return total;
> > > > }
> > > >
> > > > /* ADD TWO THE TWO VARIABLES STORED IN ADDONE AND ADDTWO USING CHOSEN BASE */
> > > >
> > > > function naiveAdd(base, addOne, addTwo)
> > > > {
> > > > prepAdd(addOne, addTwo);
> > > > addResult = [];
> > > > remainder = 0;
> > > >
> > > > for (i = 0; i < asmallArr; 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;
> > > > }
> > > > }
> > > > // Copying values from the shorter string
> > > > while(i < abigArr)
> > > > {
> > > > addOne[i] = parseInt(addOne[i]);
> > > > addResult[i] = addOne[i] + remainder;
> > > > remainder = 0;
> > > > i ++ ;
> > > > }
> > > > // If strings of equal length but there is a remainder;
> > > > if (remainder == 1) addResult[i ++ ] = 1;
> > > > else addResult.splice(i, 1);
> > > > return addResult;
> > > > }
> > > >
> > > > function prepAdd(addOne, addTwo)
> > > > {
> > > > if(addOne.length > addTwo.length)
> > > > {
> > > > abigArr = addOne.length;
> > > > asmallArr = addTwo.length
> > > > }
> > > > else
> > > > {
> > > > abigArr = addTwo.length;
> > > > asmallArr = addOne.length;
> > > > }
> > > > // Parse with zeros to get same length, and make sure they are integers.
> > > > // for (i = 0; i < bigArr; i ++ )
> > > > // {
> > > > // multOne[i] = parseInt(multOne[i]);
> > > > // multTwo[i] = parseInt(multTwo[i]);
> > > > // if (isNaN(multOne[i])) multOne[i] = 0;
> > > > // if (isNaN(multTwo[i])) multTwo[i] = 0;
> > > > // }
> > > > }
> > > >
> > > > /* FIX THIS LATER */
> > > >
> > > > function prepSubt(subOne, subTwo)
> > > > {
> > > > if(subOne.length > subTwo.length)
> > > > {
> > > > bigArr = subOne.length;
> > > > smallArr = subTwo.length
> > > > }
> > > > else
> > > > {
> > > > bigArr = subTwo.length;
> > > > smallArr = subOne.length;
> > > > }
> > > > // Parse with zeros to get same length, and make sure they are integers.
> > > > for (i = 0; i < bigArr; 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;
> > > > }
> > > > }
> > > >
> > > > /* SUBTRACT THE TWO VARIABLES STORED IN SUBONE AND SUBTWO USING THE CHOSEN BASE */
> > > >
> > > > function naiveSub(base, subOne, subTwo)
> > > > {
> > > > prepSubt(subOne, subTwo);
> > > > result = [];
> > > > for (i = 0; i < smallArr; i ++ )
> > > > {
> > > > j = 0;
> > > >
> > > > if (subOne[i] < subTwo[i])
> > > > {
> > > >
> > > > intBorrow ++ ;
> > > > subOne[i] += base;
> > > > j = i;
> > > > while (subOne[j + 1] == 0)
> > > > {
> > > > subOne[j + 1] += base - 1;
> > > >
> > > > j ++ ;
> > > > }
> > > > subOne[j + 1] += - 1;
> > > >
> > > > }
> > > >
> > > > j = 0;
> > > > result[i] = subOne[i] - subTwo[i];
> > > > intSub ++ ;
> > > > }
> > > > while(i < bigArr)
> > > > {
> > > > result[i] = subOne[i];
> > > > i ++ ;
> > > > }
> > > >
> > > > while (result[result.length - 1] == 0 && result.length - 1 > 0)
> > > > {
> > > > result.splice(result.length - 1, 1)
> > > > }
> > > > return result;
> > > > }
> > > >
> > > > /* RESET FORM FIELDS, FETCH BASE AND STRING TO EVALUATE */
> > > >
> > > > function fetchVal()
> > > > {
> > > > document.calc.stats.value = "";
> > > > strEval = document.calc.eval.value;
> > > > genArr[0] = strEval;
> > > > base = document.calc.formbase.value;
> > > > base = parseInt(base);
> > > > }
> > > >
> > > > /* DO CONVERSION OF OPERANDS TO BASE OF CHOICE */
> > > >
> > > > function baseConversion()
> > > > {
> > > > arrOne = genArr[0].split("").reverse();
> > > > arrOne = newbase(arrOne, base);
> > > > arrOne.reverse();
> > > > if (base < 11)
> > > > {
> > > > bout = arrOne.join("");
> > > > }
> > > > else
> > > > {
> > > > bout = arrOne
> > > > }
> > > > for(j=0;j<arrOne.length;j++){
> > > > intRev=arrOne[j].toString();
> > > > arrOne[j]=intRev.split("").reverse().join("");
> > > > }
> > > > document.calc.stats.value += "Converted base digits = " + bout.length + "\n";
> > > > document.calc.stats.value += "Converted arrOne " + bout + "\n";
> > > > }
> > > >
> > > > function orderArraySubt(Abase, A, B)
> > > > {
> > > > return lessThan(A, B) ? resultmult = naiveSub(Abase, B, A) : resultmult = naiveSub(Abase, A, B);
> > > > }
> > > >
> > > > /* 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()
> > > > {
> > > >
> > > > /* INIT GLOBAL VARIABLES START */
> > > > genArr = [];
> > > > baseArr = [];
> > > > resultmult = [];
> > > > plusOne = [];
> > > > plusOne[0] = 1;
> > > > 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 = "";
> > > > document.calc.stats.value = "";
> > > > document.calc.trash.value = "";
> > > > document.calc.trash.value += "Array VALUES printed out are reversed\n";
> > > > fetchVal();
> > > > // Get stringsvalues from form
> > > >
> > > > /* INIT END */
> > > > /* MAIN FUNCTION */
> > > > document.calc.stats.value += "Operand A decimal digits = " + strEval.length + "\n";
> > > > // Do base conversion into the base fetched from form
> > > > baseConversion();
> > > > }
> > > > </script>
> > > >
> > > > <meta charset="utf-8"></head>
> > > > <body onload="MAIN()" bgcolor="gold">
> > > > <h3>BIG NUMBERS BASE CONVERTER (anybase) version 0.0001 ;)</h3><p>
> > > > </p><form name="calc" onsubmit="MAIN(); return false;"><b>
> > > > <input name="calc" value="Calc" type="submit"> *use radiX* <input name="formbase" value="337" size="10" type="text"><br><br>
> > > > Trash print<br>
> > > > <textarea name="trash" rows="20" cols="120"></textarea><br>
> > > > Base 10 value to convert<br>
> > > > <textarea name="eval" rows="2" cols="120">1464803622199345</textarea><br>
> > > > New base out<br>
> > > > <textarea name="stats" rows="40" cols="120"></textarea>
> > > > </form>
> > > > </body>
> > > > </html>
> > >
> > > And for those in doubt try a 100 digit number with a 8 digit base no problem.
> > > http://jt.node365.se/baseconver...
> >
> > Any improvements suggestion?
>
> The conversion is somewhat probalistic does that mean the algorithm work in sublinear time?

Or maybe it is subquadratic?

Ben Bacarisse

6/21/2015 8:45:00 PM

0

jonas.thornvall@gmail.com writes:
<snip>
> Any improvements suggestion?

None that you haven't ignored more than once already!

--
Ben.

JT

6/21/2015 8:50:00 PM

0

Den söndag 21 juni 2015 kl. 22:45:25 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
> <snip>
> > Any improvements suggestion?
>
> None that you haven't ignored more than once already!
>
> --
> Ben.

And i who thought you would be impressed by the nifty way i do the baseconversion.
It is probalistic.

Ben Bacarisse

6/22/2015 12:20:00 AM

0

jonas.thornvall@gmail.com writes:

> Den söndag 21 juni 2015 kl. 22:45:25 UTC+2 skrev Ben Bacarisse:
>> jonas.thornvall@gmail.com writes:
>> <snip>
>> > Any improvements suggestion?
>>
>> None that you haven't ignored more than once already!
>
> And i who thought you would be impressed by the nifty way i do the
> baseconversion.
> It is probalistic.

The only curious things is why you don't do it the simple way. I
suspect it's because the code is so chaotic you can't use the simple
algorithm, but it's now so tangled that I can't plough past the first
dozen or so global names -- every function call could affect any of them
in ways that only you know (thought I suspect even you don't know
anymore) and I certainly don't have the time to check.

--
Ben.