Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27135 > unrolled thread

naive Big Numb multiplication work.

Started byjonas.thornvall@gmail.com
First post2015-03-27 09:42 -0700
Last post2015-04-02 16:24 -0700
Articles 11 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-27 09:42 -0700
    Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-27 12:14 -0700
    Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-27 13:48 -0700
      Re: naive Big Numb multiplication work. John Harris <niam@jghnorth.org.uk.invalid> - 2015-03-28 11:15 +0000
        Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-28 07:07 -0700
        Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-28 07:18 -0700
        Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-28 07:20 -0700
          Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-29 10:22 -0700
          Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-03-29 10:24 -0700
            Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-04-02 13:25 -0700
    Re: naive Big Numb multiplication work. jonas.thornvall@gmail.com - 2015-04-02 16:24 -0700

#27135 — naive Big Numb multiplication work.

Fromjonas.thornvall@gmail.com
Date2015-03-27 09:42 -0700
Subjectnaive Big Numb multiplication work.
Message-ID<97053e48-7ead-4d3f-b8e9-26e9f569c573@googlegroups.com>
I can say i had hope for less code, those checks of lenghts ain't pretty.

<SCRIPT LANGUAGE="Javascript">

/* MULTIPLY TWO VARIABLES */

function naiveMult(base, multOne, multTwo)
{
   document.calc.comment.value = "[Multiply] "+ multOne + sign + multTwo+" \n"
 
   total = [];
   result = [];
   total[0] = 0;
   for (var i = 0; i < lengthTwo; i ++ )
   {
      multresult = [];
      remainder = 0;
      tempVal = 0;
      tempDig = 0;
      j = 0;

      if(i > 0)
      {
         for(zero = 0; zero < i; zero ++ )  multresult[zero] = 0;
      }

      while(j < length)
      {
         tempVal = (multOne[i] * multTwo[j]) + 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)
      {
         multresult[j + i] = remainder;
      }
      document.calc.comment.value += "\n SubSUM= " + total;
      document.calc.comment.value += "\n SubPROD= " + multresult;
      // KOLLA NOGA
      if (multresult.length >= total.length)
      {
         mlength = multresult.length;
         mlengthTwo = total.length;
      }
      else
      {
         mlength = total.length;
         mlengthTwo = multresult.length;
      }

      for (k = 0; k < mlength; k ++ )
      {
         if (isNaN(multresult[k])) multresult[k] = 0;
         if (isNaN(total[k])) total[k] = 0;
      }
      naiveAdd(base, mlength, total, multresult);
      total = result;
   }
   document.calc.comment.value += "\nTOTAL PRDDUKT= " + total;
}


/* ADD TWO VARIABLES */

function naiveAdd(base, alength, addOne, addTwo)
{
   document.calc.comment.value += "\nAdd termOne+termTwo =\n" + addOne+"\n"  + addTwo + " + ";
   addResult = [];
   remainder = 0;
   for (i = 0; i < alength; i ++ )
   {
      addResult[i] = addOne[i] + addTwo[i] + remainder;
      document.calc.comment.value += "\n subAdd = " + addResult[i];
      if (addResult[i] >= base)
      {
         addResult[i] = addResult[i] - base;
         //document.calc.comment.value += "\n *IF* Addresult = " + addResult[i];
         remainder = 1;
      }
      else
      {
         remainder = 0;
      }
   }

   if (remainder == 1) addResult[i ++ ] = 1;
   else addResult.splice(i, 1);
   result = addResult;
   document.calc.comment.value += "\n SUBSUM= " + addResult;
   return result;
}




/* SUBTRACT TWO VARIABLES */

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

      if (subOne[i] < subTwo[i])
      {
         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];

   }

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





/* PREPARE STRING PAIR */

function strPrep(sign, strOne, strTwo)
{

   arrOne = strOne.split("");
   arrTwo = strTwo.split("");
   arrOne.reverse();
   arrTwo.reverse();
   if (arrOne.length >= arrTwo.length)
   {
      length = arrOne.length;
      lengthTwo = arrTwo.length;
   }
   else
   {
      length = arrTwo.length;
      lengthTwo = arrOne.length;
   }
   for (i = 0; i < length; i ++ )
   {
      arrOne[i] = parseInt(arrOne[i]);
      arrTwo[i] = parseInt(arrTwo[i]);
      if (isNaN(arrOne[i])) arrOne[i] = 0;
      if (isNaN(arrTwo[i])) arrTwo[i] = 0;
   }
}

/* MULTIPLY Container */

function multWrap(sign, multiply, strBase)
{
   tONE = parseInt(multiply[0]);
   tTWO = parseInt(multiply[1]);
   if(tONE <= tTWO)
   {
      arrOne = multiply[0];
      arrTwo = multiply[1];
   }
   else
   {
      arrOne = multiply[1];
      arrTwo = multiply[0];
   }
   strPrep(sign, arrOne, arrTwo);
   naiveMult(base, arrOne, arrTwo);
   document.calc.msum.value = sum;
   out = total.reverse().toString();
   document.calc.result.value = out;
}

/* SUBTRACTION Container */

function subWrap(sign, strEval, strBase)
{
   tONE = parseInt(subtract[0]);
   tTWO = parseInt(subtract[1]);
   if(tONE < tTWO)
   {
      strOne = subtract[1];
      strTwo = subtract[0];
   }
   else
   {
      strOne = subtract[0];
      strTwo = subtract[1];
   }


   strPrep(sign, strOne, strTwo);
   naiveSub(base, arrOne, arrTwo);
   out = result.reverse().toString();
   document.calc.result.value = out;
}

/* ADDITION Container */

function addWrap(sign, strEval, strBase)
{
   strOne = adder[0];
   strTwo = adder[1];
   strPrep(sign, strOne, strTwo);
   naiveAdd(base, length, arrOne, arrTwo);
   out = result.reverse().toString();
   document.calc.result.value = out;
}

/* PARSE THE STRING */

function parser(strEval, strBase)
{

   sign = "*";
    multiply = strEval.split("*");
   // document.calc.firstval.value = multiply[0];
   // document.calc.secval.value = multiply[1];
    multWrap(sign, multiply, strBase)
    document.calc.tO.value = strOne;
   // document.calc.tT.value = strTwo;
   //  sign = "-";
   //  subtract = strEval.split("-");
   //  subWrap(sign, subtract, strBase);
   //sign = "+";
   //adder = strEval.split("+");
   //addWrap(sign, adder, strBase);
}

/* [MAIN PROGRAM] */
/* [TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT] */
/* [GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */

function fetchValues()
{

   strEval = document.calc.eval.value;
   strBase = document.calc.formbase.value;
   document.calc.trash.value = "1";
   base = parseInt(strBase);
   parser(strEval, strBase);


}

/* GLOBAL */
var multiply = [];
var subtract = [];
var adder  = [];
var arrOne = [];
var arrTwo = [];
// var addOne = [];
// var addTwo = [];
// var subOne = [];
// var subTwo = [];
var total = [];
var result = [];
var sum = [];

length = 0;
strOne = "";
strTwo = "";
base = 10;
temp = "";

</SCRIPT>

<HTML>
 <body bgcolor="gold" onload="fetchValues()">
<FORM name=calc onSubmit="fetchValues(); return false;">
BASE <input type="text" name="formbase" value="10" size="4"><br>
<input type=submit name="calc" value="Calc"><input type="text" name="eval" value="2213213219382198348348338*2382828248324834832" size="80"><br>
RESULT<input type="text" name="result" value="" size="80"><br>
Trash<input type="text" name="trash" value="" size="80"> <br>
FIRSTVAL<input type="text" name="firstval" value="" size="80"><br>
SECVAL<input type="text" name="secval" value="" size="80"><br>
tONE Small<input type="text" name="tO" value="" size="80"><br>
tTWO Big<input type="text" name="tT" value="" size="80"><br>
Sum<input type="text" name="msum" value="" size="80"><br>
<textarea name="comment" cols="80" rows="20" value=""></textarea>
</FORM>
</BODY>
</HTML>

[toc] | [next] | [standalone]


#27136

Fromjonas.thornvall@gmail.com
Date2015-03-27 12:14 -0700
Message-ID<55b7614e-0d7a-4179-a270-8a6f12758eb5@googlegroups.com>
In reply to#27135
Den fredag 27 mars 2015 kl. 17:42:28 UTC+1 skrev jonas.t...@gmail.com:
> I can say i had hope for less code, those checks of lenghts ain't pretty.
> 
> <SCRIPT LANGUAGE="Javascript">
> 
> /* MULTIPLY TWO VARIABLES */
> 
> function naiveMult(base, multOne, multTwo)
> {
>    document.calc.comment.value = "[Multiply] "+ multOne + sign + multTwo+" \n"
>  
>    total = [];
>    result = [];
>    total[0] = 0;
>    for (var i = 0; i < lengthTwo; i ++ )
>    {
>       multresult = [];
>       remainder = 0;
>       tempVal = 0;
>       tempDig = 0;
>       j = 0;
> 
>       if(i > 0)
>       {
>          for(zero = 0; zero < i; zero ++ )  multresult[zero] = 0;
>       }
> 
>       while(j < length)
>       {
>          tempVal = (multOne[i] * multTwo[j]) + 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)
>       {
>          multresult[j + i] = remainder;
>       }
>       document.calc.comment.value += "\n SubSUM= " + total;
>       document.calc.comment.value += "\n SubPROD= " + multresult;
>       // KOLLA NOGA
>       if (multresult.length >= total.length)
>       {
>          mlength = multresult.length;
>          mlengthTwo = total.length;
>       }
>       else
>       {
>          mlength = total.length;
>          mlengthTwo = multresult.length;
>       }
> 
>       for (k = 0; k < mlength; k ++ )
>       {
>          if (isNaN(multresult[k])) multresult[k] = 0;
>          if (isNaN(total[k])) total[k] = 0;
>       }
>       naiveAdd(base, mlength, total, multresult);
>       total = result;
>    }
>    document.calc.comment.value += "\nTOTAL PRDDUKT= " + total;
> }
> 
> 
> /* ADD TWO VARIABLES */
> 
> function naiveAdd(base, alength, addOne, addTwo)
> {
>    document.calc.comment.value += "\nAdd termOne+termTwo =\n" + addOne+"\n"  + addTwo + " + ";
>    addResult = [];
>    remainder = 0;
>    for (i = 0; i < alength; i ++ )
>    {
>       addResult[i] = addOne[i] + addTwo[i] + remainder;
>       document.calc.comment.value += "\n subAdd = " + addResult[i];
>       if (addResult[i] >= base)
>       {
>          addResult[i] = addResult[i] - base;
>          //document.calc.comment.value += "\n *IF* Addresult = " + addResult[i];
>          remainder = 1;
>       }
>       else
>       {
>          remainder = 0;
>       }
>    }
> 
>    if (remainder == 1) addResult[i ++ ] = 1;
>    else addResult.splice(i, 1);
>    result = addResult;
>    document.calc.comment.value += "\n SUBSUM= " + addResult;
>    return result;
> }
> 
> 
> 
> 
> /* SUBTRACT TWO VARIABLES */
> 
> function naiveSub(base, subOne, subTwo)
> {
>    result = [];
>    for (i = 0; i < length; i ++ )
>    {
>       j = 0;
> 
>       if (subOne[i] < subTwo[i])
>       {
>          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];
> 
>    }
> 
>    while (result[length - 1] == 0)
>    {
>       result.splice(length - 1, 1);
>       length += - 1;
>    }
> }
> 
> 
> 
> 
> 
> /* PREPARE STRING PAIR */
> 
> function strPrep(sign, strOne, strTwo)
> {
> 
>    arrOne = strOne.split("");
>    arrTwo = strTwo.split("");
>    arrOne.reverse();
>    arrTwo.reverse();
>    if (arrOne.length >= arrTwo.length)
>    {
>       length = arrOne.length;
>       lengthTwo = arrTwo.length;
>    }
>    else
>    {
>       length = arrTwo.length;
>       lengthTwo = arrOne.length;
>    }
>    for (i = 0; i < length; i ++ )
>    {
>       arrOne[i] = parseInt(arrOne[i]);
>       arrTwo[i] = parseInt(arrTwo[i]);
>       if (isNaN(arrOne[i])) arrOne[i] = 0;
>       if (isNaN(arrTwo[i])) arrTwo[i] = 0;
>    }
> }
> 
> /* MULTIPLY Container */
> 
> function multWrap(sign, multiply, strBase)
> {
>    tONE = parseInt(multiply[0]);
>    tTWO = parseInt(multiply[1]);
>    if(tONE <= tTWO)
>    {
>       arrOne = multiply[0];
>       arrTwo = multiply[1];
>    }
>    else
>    {
>       arrOne = multiply[1];
>       arrTwo = multiply[0];
>    }
>    strPrep(sign, arrOne, arrTwo);
>    naiveMult(base, arrOne, arrTwo);
>    document.calc.msum.value = sum;
>    out = total.reverse().toString();
>    document.calc.result.value = out;
> }
> 
> /* SUBTRACTION Container */
> 
> function subWrap(sign, strEval, strBase)
> {
>    tONE = parseInt(subtract[0]);
>    tTWO = parseInt(subtract[1]);
>    if(tONE < tTWO)
>    {
>       strOne = subtract[1];
>       strTwo = subtract[0];
>    }
>    else
>    {
>       strOne = subtract[0];
>       strTwo = subtract[1];
>    }
> 
> 
>    strPrep(sign, strOne, strTwo);
>    naiveSub(base, arrOne, arrTwo);
>    out = result.reverse().toString();
>    document.calc.result.value = out;
> }
> 
> /* ADDITION Container */
> 
> function addWrap(sign, strEval, strBase)
> {
>    strOne = adder[0];
>    strTwo = adder[1];
>    strPrep(sign, strOne, strTwo);
>    naiveAdd(base, length, arrOne, arrTwo);
>    out = result.reverse().toString();
>    document.calc.result.value = out;
> }
> 
> /* PARSE THE STRING */
> 
> function parser(strEval, strBase)
> {
> 
>    sign = "*";
>     multiply = strEval.split("*");
>    // document.calc.firstval.value = multiply[0];
>    // document.calc.secval.value = multiply[1];
>     multWrap(sign, multiply, strBase)
>     document.calc.tO.value = strOne;
>    // document.calc.tT.value = strTwo;
>    //  sign = "-";
>    //  subtract = strEval.split("-");
>    //  subWrap(sign, subtract, strBase);
>    //sign = "+";
>    //adder = strEval.split("+");
>    //addWrap(sign, adder, strBase);
> }
> 
> /* [MAIN PROGRAM] */
> /* [TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT] */
> /* [GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */
> 
> function fetchValues()
> {
> 
>    strEval = document.calc.eval.value;
>    strBase = document.calc.formbase.value;
>    document.calc.trash.value = "1";
>    base = parseInt(strBase);
>    parser(strEval, strBase);
> 
> 
> }
> 
> /* GLOBAL */
> var multiply = [];
> var subtract = [];
> var adder  = [];
> var arrOne = [];
> var arrTwo = [];
> // var addOne = [];
> // var addTwo = [];
> // var subOne = [];
> // var subTwo = [];
> var total = [];
> var result = [];
> var sum = [];
> 
> length = 0;
> strOne = "";
> strTwo = "";
> base = 10;
> temp = "";
> 
> </SCRIPT>
> 
> <HTML>
>  <body bgcolor="gold" onload="fetchValues()">
> <FORM name=calc onSubmit="fetchValues(); return false;">
> BASE <input type="text" name="formbase" value="10" size="4"><br>
> <input type=submit name="calc" value="Calc"><input type="text" name="eval" value="2213213219382198348348338*2382828248324834832" size="80"><br>
> RESULT<input type="text" name="result" value="" size="80"><br>
> Trash<input type="text" name="trash" value="" size="80"> <br>
> FIRSTVAL<input type="text" name="firstval" value="" size="80"><br>
> SECVAL<input type="text" name="secval" value="" size="80"><br>
> tONE Small<input type="text" name="tO" value="" size="80"><br>
> tTWO Big<input type="text" name="tT" value="" size="80"><br>
> Sum<input type="text" name="msum" value="" size="80"><br>
> <textarea name="comment" cols="80" rows="20" value=""></textarea>
> </FORM>
> </BODY>
> </HTML>

Seem ot work for anybase and anysize try base 2.

[toc] | [prev] | [next] | [standalone]


#27137

Fromjonas.thornvall@gmail.com
Date2015-03-27 13:48 -0700
Message-ID<24d203f8-88e4-4b81-8bdd-7173076f0f51@googlegroups.com>
In reply to#27135
Den fredag 27 mars 2015 kl. 17:42:28 UTC+1 skrev jonas.t...@gmail.com:
> I can say i had hope for less code, those checks of lenghts ain't pretty.
> 
> <SCRIPT LANGUAGE="Javascript">
> 
> /* MULTIPLY TWO VARIABLES */
> 
> function naiveMult(base, multOne, multTwo)
> {
>    document.calc.comment.value = "[Multiply] "+ multOne + sign + multTwo+" \n"
>  
>    total = [];
>    result = [];
>    total[0] = 0;
>    for (var i = 0; i < lengthTwo; i ++ )
>    {
>       multresult = [];
>       remainder = 0;
>       tempVal = 0;
>       tempDig = 0;
>       j = 0;
> 
>       if(i > 0)
>       {
>          for(zero = 0; zero < i; zero ++ )  multresult[zero] = 0;
>       }
> 
>       while(j < length)
>       {
>          tempVal = (multOne[i] * multTwo[j]) + 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)
>       {
>          multresult[j + i] = remainder;
>       }
>       document.calc.comment.value += "\n SubSUM= " + total;
>       document.calc.comment.value += "\n SubPROD= " + multresult;
>       // KOLLA NOGA
>       if (multresult.length >= total.length)
>       {
>          mlength = multresult.length;
>          mlengthTwo = total.length;
>       }
>       else
>       {
>          mlength = total.length;
>          mlengthTwo = multresult.length;
>       }
> 
>       for (k = 0; k < mlength; k ++ )
>       {
>          if (isNaN(multresult[k])) multresult[k] = 0;
>          if (isNaN(total[k])) total[k] = 0;
>       }
>       naiveAdd(base, mlength, total, multresult);
>       total = result;
>    }
>    document.calc.comment.value += "\nTOTAL PRDDUKT= " + total;
> }
> 
> 
> /* ADD TWO VARIABLES */
> 
> function naiveAdd(base, alength, addOne, addTwo)
> {
>    document.calc.comment.value += "\nAdd termOne+termTwo =\n" + addOne+"\n"  + addTwo + " + ";
>    addResult = [];
>    remainder = 0;
>    for (i = 0; i < alength; i ++ )
>    {
>       addResult[i] = addOne[i] + addTwo[i] + remainder;
>       document.calc.comment.value += "\n subAdd = " + addResult[i];
>       if (addResult[i] >= base)
>       {
>          addResult[i] = addResult[i] - base;
>          //document.calc.comment.value += "\n *IF* Addresult = " + addResult[i];
>          remainder = 1;
>       }
>       else
>       {
>          remainder = 0;
>       }
>    }
> 
>    if (remainder == 1) addResult[i ++ ] = 1;
>    else addResult.splice(i, 1);
>    result = addResult;
>    document.calc.comment.value += "\n SUBSUM= " + addResult;
>    return result;
> }
> 
> 
> 
> 
> /* SUBTRACT TWO VARIABLES */
> 
> function naiveSub(base, subOne, subTwo)
> {
>    result = [];
>    for (i = 0; i < length; i ++ )
>    {
>       j = 0;
> 
>       if (subOne[i] < subTwo[i])
>       {
>          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];
> 
>    }
> 
>    while (result[length - 1] == 0)
>    {
>       result.splice(length - 1, 1);
>       length += - 1;
>    }
> }
> 
> 
> 
> 
> 
> /* PREPARE STRING PAIR */
> 
> function strPrep(sign, strOne, strTwo)
> {
> 
>    arrOne = strOne.split("");
>    arrTwo = strTwo.split("");
>    arrOne.reverse();
>    arrTwo.reverse();
>    if (arrOne.length >= arrTwo.length)
>    {
>       length = arrOne.length;
>       lengthTwo = arrTwo.length;
>    }
>    else
>    {
>       length = arrTwo.length;
>       lengthTwo = arrOne.length;
>    }
>    for (i = 0; i < length; i ++ )
>    {
>       arrOne[i] = parseInt(arrOne[i]);
>       arrTwo[i] = parseInt(arrTwo[i]);
>       if (isNaN(arrOne[i])) arrOne[i] = 0;
>       if (isNaN(arrTwo[i])) arrTwo[i] = 0;
>    }
> }
> 
> /* MULTIPLY Container */
> 
> function multWrap(sign, multiply, strBase)
> {
>    tONE = parseInt(multiply[0]);
>    tTWO = parseInt(multiply[1]);
>    if(tONE <= tTWO)
>    {
>       arrOne = multiply[0];
>       arrTwo = multiply[1];
>    }
>    else
>    {
>       arrOne = multiply[1];
>       arrTwo = multiply[0];
>    }
>    strPrep(sign, arrOne, arrTwo);
>    naiveMult(base, arrOne, arrTwo);
>    document.calc.msum.value = sum;
>    out = total.reverse().toString();
>    document.calc.result.value = out;
> }
> 
> /* SUBTRACTION Container */
> 
> function subWrap(sign, strEval, strBase)
> {
>    tONE = parseInt(subtract[0]);
>    tTWO = parseInt(subtract[1]);
>    if(tONE < tTWO)
>    {
>       strOne = subtract[1];
>       strTwo = subtract[0];
>    }
>    else
>    {
>       strOne = subtract[0];
>       strTwo = subtract[1];
>    }
> 
> 
>    strPrep(sign, strOne, strTwo);
>    naiveSub(base, arrOne, arrTwo);
>    out = result.reverse().toString();
>    document.calc.result.value = out;
> }
> 
> /* ADDITION Container */
> 
> function addWrap(sign, strEval, strBase)
> {
>    strOne = adder[0];
>    strTwo = adder[1];
>    strPrep(sign, strOne, strTwo);
>    naiveAdd(base, length, arrOne, arrTwo);
>    out = result.reverse().toString();
>    document.calc.result.value = out;
> }
> 
> /* PARSE THE STRING */
> 
> function parser(strEval, strBase)
> {
> 
>    sign = "*";
>     multiply = strEval.split("*");
>    // document.calc.firstval.value = multiply[0];
>    // document.calc.secval.value = multiply[1];
>     multWrap(sign, multiply, strBase)
>     document.calc.tO.value = strOne;
>    // document.calc.tT.value = strTwo;
>    //  sign = "-";
>    //  subtract = strEval.split("-");
>    //  subWrap(sign, subtract, strBase);
>    //sign = "+";
>    //adder = strEval.split("+");
>    //addWrap(sign, adder, strBase);
> }
> 
> /* [MAIN PROGRAM] */
> /* [TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT] */
> /* [GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */
> 
> function fetchValues()
> {
> 
>    strEval = document.calc.eval.value;
>    strBase = document.calc.formbase.value;
>    document.calc.trash.value = "1";
>    base = parseInt(strBase);
>    parser(strEval, strBase);
> 
> 
> }
> 
> /* GLOBAL */
> var multiply = [];
> var subtract = [];
> var adder  = [];
> var arrOne = [];
> var arrTwo = [];
> // var addOne = [];
> // var addTwo = [];
> // var subOne = [];
> // var subTwo = [];
> var total = [];
> var result = [];
> var sum = [];
> 
> length = 0;
> strOne = "";
> strTwo = "";
> base = 10;
> temp = "";
> 
> </SCRIPT>
> 
> <HTML>
>  <body bgcolor="gold" onload="fetchValues()">
> <FORM name=calc onSubmit="fetchValues(); return false;">
> BASE <input type="text" name="formbase" value="10" size="4"><br>
> <input type=submit name="calc" value="Calc"><input type="text" name="eval" value="2213213219382198348348338*2382828248324834832" size="80"><br>
> RESULT<input type="text" name="result" value="" size="80"><br>
> Trash<input type="text" name="trash" value="" size="80"> <br>
> FIRSTVAL<input type="text" name="firstval" value="" size="80"><br>
> SECVAL<input type="text" name="secval" value="" size="80"><br>
> tONE Small<input type="text" name="tO" value="" size="80"><br>
> tTWO Big<input type="text" name="tT" value="" size="80"><br>
> Sum<input type="text" name="msum" value="" size="80"><br>
> <textarea name="comment" cols="80" rows="20" value=""></textarea>
> </FORM>
> </BODY>
> </HTML>

Honestly it is magneficient it totally rocks even in unary base ;)

[toc] | [prev] | [next] | [standalone]


#27138

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2015-03-28 11:15 +0000
Message-ID<g83dhadnudtndnn7rnjf823vd7ikgmgva4@4ax.com>
In reply to#27137
On Fri, 27 Mar 2015 13:48:35 -0700 (PDT), jonas.thornvall@gmail.com
wrote:

>Den fredag 27 mars 2015 kl. 17:42:28 UTC+1 skrev jonas.t...@gmail.com:
>> I can say i had hope for less code, those checks of lenghts ain't pretty.
>> 
>> <SCRIPT LANGUAGE="Javascript">
  <snip>
>> </HTML>
>
>Honestly it is magneficient it totally rocks even in unary base ;)

To complete this project you need to implement mixed radix arithmetic.
For instance : 
  pounds, shillings, and pence; 
  hundredweights, stones, pounds, and ounces; 
  yards, feet, and inches.

If you think it's difficult, remember that in Britain before 1970
pounds, shillings, and pence arithmetic had to be done by 9 year olds.
They were expected to be able to do addition and subtraction, but
multiplication and division only by simple, decimal, numbers.

  John

[toc] | [prev] | [next] | [standalone]


#27139

Fromjonas.thornvall@gmail.com
Date2015-03-28 07:07 -0700
Message-ID<a52f0b1e-1d41-4e47-a2d0-5b18328ba726@googlegroups.com>
In reply to#27138
Den lördag 28 mars 2015 kl. 12:15:15 UTC+1 skrev John Harris:
> On Fri, 27 Mar 2015 13:48:35 -0700 (PDT), jonas.thornvall@gmail.com
> wrote:
> 
> >Den fredag 27 mars 2015 kl. 17:42:28 UTC+1 skrev jonas.t...@gmail.com:
> >> I can say i had hope for less code, those checks of lenghts ain't pretty.
> >> 
> >> <SCRIPT LANGUAGE="Javascript">
>   <snip>
> >> </HTML>
> >
> >Honestly it is magneficient it totally rocks even in unary base ;)
> 
> To complete this project you need to implement mixed radix arithmetic.
> For instance : 
>   pounds, shillings, and pence; 
>   hundredweights, stones, pounds, and ounces; 
>   yards, feet, and inches.
> 
> If you think it's difficult, remember that in Britain before 1970
> pounds, shillings, and pence arithmetic had to be done by 9 year olds.
> They were expected to be able to do addition and subtraction, but
> multiplication and division only by simple, decimal, numbers.
> 
>   John

It already is mixed radix for anyone who understand to make two function calls. I am not sure it includes you.

[toc] | [prev] | [next] | [standalone]


#27140

Fromjonas.thornvall@gmail.com
Date2015-03-28 07:18 -0700
Message-ID<2984b329-b3c6-415d-9169-dade67cd85b3@googlegroups.com>
In reply to#27138
Den lördag 28 mars 2015 kl. 12:15:15 UTC+1 skrev John Harris:
> On Fri, 27 Mar 2015 13:48:35 -0700 (PDT), jonas.thornvall@gmail.com
> wrote:
> 
> >Den fredag 27 mars 2015 kl. 17:42:28 UTC+1 skrev jonas.t...@gmail.com:
> >> I can say i had hope for less code, those checks of lenghts ain't pretty.
> >> 
> >> <SCRIPT LANGUAGE="Javascript">
>   <snip>
> >> </HTML>
> >
> >Honestly it is magneficient it totally rocks even in unary base ;)
> 
> To complete this project you need to implement mixed radix arithmetic.
> For instance : 
>   pounds, shillings, and pence; 
>   hundredweights, stones, pounds, and ounces; 
>   yards, feet, and inches.
> 
> If you think it's difficult, remember that in Britain before 1970
> pounds, shillings, and pence arithmetic had to be done by 9 year olds.
> They were expected to be able to do addition and subtraction, but
> multiplication and division only by simple, decimal, numbers.
> 
>   John

Well John next step would be division, and then a parser preferable handlin paranthese clauses.

After that i will knot it in with the free radix base changer. Getting your operation automaticly translated into bases that support bignumb.

For example doing arithmetic in base 4294967295 which i beleive is the biggest base directly supported straight out of 32 bit implementations?

I think with a little optimisation and without printout it will handle multiplication of 1 million digit numbers in a breeze.

So that is my pet project.

[toc] | [prev] | [next] | [standalone]


#27141

Fromjonas.thornvall@gmail.com
Date2015-03-28 07:20 -0700
Message-ID<7f775ce1-b3a5-4a37-9f8d-71cf1e1f7a2b@googlegroups.com>
In reply to#27138
Den lördag 28 mars 2015 kl. 12:15:15 UTC+1 skrev John Harris:
> On Fri, 27 Mar 2015 13:48:35 -0700 (PDT), jonas.thornvall@gmail.com
> wrote:
> 
> >Den fredag 27 mars 2015 kl. 17:42:28 UTC+1 skrev jonas.t...@gmail.com:
> >> I can say i had hope for less code, those checks of lenghts ain't pretty.
> >> 
> >> <SCRIPT LANGUAGE="Javascript">
>   <snip>
> >> </HTML>
> >
> >Honestly it is magneficient it totally rocks even in unary base ;)
> 
> To complete this project you need to implement mixed radix arithmetic.
> For instance : 
>   pounds, shillings, and pence; 
>   hundredweights, stones, pounds, and ounces; 
>   yards, feet, and inches.
> 
> If you think it's difficult, remember that in Britain before 1970
> pounds, shillings, and pence arithmetic had to be done by 9 year olds.
> They were expected to be able to do addition and subtraction, but
> multiplication and division only by simple, decimal, numbers.
> 
>   John

It may be that i include doing the arithmetic in bijective bases.
http://jt.node365.se/bijbase.html

[toc] | [prev] | [next] | [standalone]


#27142

Fromjonas.thornvall@gmail.com
Date2015-03-29 10:22 -0700
Message-ID<79f31b1f-9bc4-4e00-b60d-03b9ac3476e6@googlegroups.com>
In reply to#27141
Den lördag 28 mars 2015 kl. 15:20:57 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 28 mars 2015 kl. 12:15:15 UTC+1 skrev John Harris:
> > On Fri, 27 Mar 2015 13:48:35 -0700 (PDT), jonas.thornvall@gmail.com
> > wrote:
> > 
> > >Den fredag 27 mars 2015 kl. 17:42:28 UTC+1 skrev jonas.t...@gmail.com:
> > >> I can say i had hope for less code, those checks of lenghts ain't pretty.
> > >> 
> > >> <SCRIPT LANGUAGE="Javascript">
> >   <snip>
> > >> </HTML>
> > >
> > >Honestly it is magneficient it totally rocks even in unary base ;)
> > 
> > To complete this project you need to implement mixed radix arithmetic.
> > For instance : 
> >   pounds, shillings, and pence; 
> >   hundredweights, stones, pounds, and ounces; 
> >   yards, feet, and inches.
> > 
> > If you think it's difficult, remember that in Britain before 1970
> > pounds, shillings, and pence arithmetic had to be done by 9 year olds.
> > They were expected to be able to do addition and subtraction, but
> > multiplication and division only by simple, decimal, numbers.
> > 
> >   John
> 
> It may be that i include doing the arithmetic in bijective bases.
> http://jt.node365.se/bijbase.html

Added some checks to see how you optimize, will add timings. It is also interesting to see how the number of integer operations change when the you use bigger and bigger bases.

Well one could had hoped for byte implementation in javascript. 
And you could do alot of optimization without any really danger for the generalizaton. But maybe using bitwiseoperations turn out to save some time compared with doing integer operations?

Can i do bitwise operations directly on an integer and will it be faster then adding two integers?

[toc] | [prev] | [next] | [standalone]


#27143

Fromjonas.thornvall@gmail.com
Date2015-03-29 10:24 -0700
Message-ID<b0cd5608-93ba-41fd-9ce5-b63ab4e3aeaa@googlegroups.com>
In reply to#27141
Den lördag 28 mars 2015 kl. 15:20:57 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 28 mars 2015 kl. 12:15:15 UTC+1 skrev John Harris:
> > On Fri, 27 Mar 2015 13:48:35 -0700 (PDT), jonas.thornvall@gmail.com
> > wrote:
> > 
> > >Den fredag 27 mars 2015 kl. 17:42:28 UTC+1 skrev jonas.t...@gmail.com:
> > >> I can say i had hope for less code, those checks of lenghts ain't pretty.
> > >> 
> > >> <SCRIPT LANGUAGE="Javascript">
> >   <snip>
> > >> </HTML>
> > >
> > >Honestly it is magneficient it totally rocks even in unary base ;)
> > 
> > To complete this project you need to implement mixed radix arithmetic.
> > For instance : 
> >   pounds, shillings, and pence; 
> >   hundredweights, stones, pounds, and ounces; 
> >   yards, feet, and inches.
> > 
> > If you think it's difficult, remember that in Britain before 1970
> > pounds, shillings, and pence arithmetic had to be done by 9 year olds.
> > They were expected to be able to do addition and subtraction, but
> > multiplication and division only by simple, decimal, numbers.
> > 
> >   John
> 
> It may be that i include doing the arithmetic in bijective bases.
> http://jt.node365.se/bijbase.html

<SCRIPT LANGUAGE="Javascript">

/* MULTIPLY TWO VARIABLES */

function naiveMult(base, multOne, multTwo)
{
   // document.calc.comment.value = "[Multiply] " + multOne + sign + multTwo + " \n"
   document.calc.comment.value = "";
   document.calc.comment.value += "FACTOR ONE " + lengthTwo + " digits \n"
   document.calc.comment.value += "FACTOR TWO " + length + " digits \n"

   total = [];
   result = [];
   total[0] = 0;
   for (var i = 0; i < lengthTwo; i ++ )
   {
      multresult = [];
      remainder = 0;
      tempVal = 0;
      tempDig = 0;
      j = 0;

      if(i > 0)
      {
         for(zero = 0; zero < i; zero ++ ) multresult[zero] = 0;
      }

      while(j < length)
      {
         intmult ++ ;
         tempVal = (multOne[i] * multTwo[j]) + 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)
      {
         multresult[j + i] = remainder;

      }
      // document.calc.comment.value += "\n SubSUM= " + total;
      // document.calc.comment.value += "\n SubPROD= " + multresult + "\n";
      // KOLLA NOGA
      if (multresult.length >= total.length)
      {
         mlength = multresult.length;
         mlengthTwo = total.length;
      }
      else
      {
         mlength = total.length;
         mlengthTwo = multresult.length;
      }

      for (k = 0; k < mlength; k ++ )
      {
         if (isNaN(multresult[k])) multresult[k] = 0;
         if (isNaN(total[k])) total[k] = 0;
      }
      naiveAdd(base, mlength, total, multresult);
      total = result;
   }

   // document.calc.comment.value += "\nTOTAL PRDDUKT= " + total;
}

/* ADD TWO VARIABLES */

function naiveAdd(base, alength, addOne, addTwo)
{
   // document.calc.comment.value += "\nAdd termOne+termTwo =\n" + addOne + "\n" + addTwo + " + ";
   addResult = [];
   remainder = 0;
   for (i = 0; i < alength; i ++ )
   {
      addResult[i] = addOne[i] + addTwo[i] + remainder;
      intadd ++ ;
      // document.calc.comment.value += "\n subAdd = " + addResult[i];
      if (addResult[i] >= base)
      {
         addResult[i] = addResult[i] - base;
         intadd ++ ;
         remainder = 1;
      }
      else
      {
         remainder = 0;
      }

      // document.calc.comment.value += " DigitValue = " + addResult[i];
      // document.calc.comment.value += " carryDigit = " + remainder;
   }

   if (remainder == 1) addResult[i ++ ] = 1;
   else addResult.splice(i, 1);
   result = addResult;

   return result;
}

/* SUBTRACT TWO VARIABLES */

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

      if (subOne[i] < subTwo[i])
      {
         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];

   }

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

/* PREPARE STRING PAIR */

function strPrep(sign, strOne, strTwo)
{

   arrOne = strOne.split("");
   arrTwo = strTwo.split("");
   arrOne.reverse();
   arrTwo.reverse();
   if (arrOne.length >= arrTwo.length)
   {
      length = arrOne.length;
      lengthTwo = arrTwo.length;
   }
   else
   {
      length = arrTwo.length;
      lengthTwo = arrOne.length;
   }
   for (i = 0; i < length; i ++ )
   {
      arrOne[i] = parseInt(arrOne[i]);
      arrTwo[i] = parseInt(arrTwo[i]);
      if (isNaN(arrOne[i])) arrOne[i] = 0;
      if (isNaN(arrTwo[i])) arrTwo[i] = 0;
   }
}

/* MULTIPLY Container */

function multWrap(sign, multiply, strBase)
{
   tONE = parseInt(multiply[0]);
   tTWO = parseInt(multiply[1]);
   if(tONE <= tTWO)
   {
      arrOne = multiply[0];
      arrTwo = multiply[1];
   }
   else
   {
      arrOne = multiply[1];
      arrTwo = multiply[0];
   }
   strPrep(sign, arrOne, arrTwo);
   naiveMult(base, arrOne, arrTwo);
   // document.calc.msum.value = sum;
   out = total.reverse().toString();
   document.calc.comment.value += "Integer ADD operations " + intadd + " \n"
   document.calc.comment.value += "Integer MULT operations " + intmult + " \n"
   document.calc.comment.value += "Product digits " + out.length + "\n";
   document.calc.comment.value += out;
}

/* SUBTRACTION Container */

function subWrap(sign, strEval, strBase)
{
   tONE = parseInt(subtract[0]);
   tTWO = parseInt(subtract[1]);
   if(tONE < tTWO)
   {
      strOne = subtract[1];
      strTwo = subtract[0];
   }
   else
   {
      strOne = subtract[0];
      strTwo = subtract[1];
   }

   strPrep(sign, strOne, strTwo);
   naiveSub(base, arrOne, arrTwo);

}

/* ADDITION Container */

function addWrap(sign, strEval, strBase)
{
   strOne = adder[0];
   strTwo = adder[1];
   strPrep(sign, strOne, strTwo);
   naiveAdd(base, length, arrOne, arrTwo);

}

/* PARSE THE STRING */

function parser(strEval, strBase)
{

   sign = "*";
   multiply = strEval.split("*");
   // document.calc.firstval.value = multiply[0];
   // document.calc.secval.value = multiply[1];
   multWrap(sign, multiply, strBase)
   // document.calc.tO.value = strOne;
   // document.calc.tT.value = strTwo;
   // sign = "-";
   // subtract = strEval.split("-");
   // subWrap(sign, subtract, strBase);
   // sign = "+";
   // adder = strEval.split("+");
   // addWrap(sign, adder, strBase);
}

/* [MAIN PROGRAM] */
/* [TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT] */
/* [GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */

function fetchValues()
{
   intmult = 0;
   intadd = 0;
   document.calc.eval.value;
   strEval = document.calc.eval.value;
   strBase = document.calc.formbase.value;
   // document.calc.trash.value = "1";
   parser(strEval, strBase);

}

/* GLOBAL */

var multiply = [];
var subtract = [];
var adder = [];
var arrOne = [];
var arrTwo = [];
// var addOne = [];
// var addTwo = [];
// var subOne = [];
// var subTwo = [];
var total = [];
var result = [];
var sum = [];

length = 0;
strOne = "";
strTwo = "";
base = 10;
temp = "";

</SCRIPT>

<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"/></head>
<body bgcolor="gold" onload="fetchValues()">
<H1>Big numbers</H1><P>
<FORM name=calc onSubmit="fetchValues(); return false;">
BASE <input type="text" name="formbase" value="10" size="4"><input type=submit name="calc" value="Calc"><br>
<textarea name="eval" cols="120" rows="20">9832479873298743298222222222222222222222222222274329873433985439857439448584395784397543754389759843759843589437598437598437587854374347327443275438754398759843754375438758437598435784375437054328478754375447543875435743754375435743857439854398543875438759843759843754375943754375843754375435743754387545875485758745984375843754375438574357437598435743875475957487543857574375439857475845749357435743985437584984375434375987435974398754343985984357498589874375949874987597498754987598749875987459874975987439759475974398754987598749754985494385438594359435943859438543854385438543985943859043859043589043859043590435904385904359043584385439589430589043589430588549385435984395843958439058439058439054839054890548905905439058439058490584390589043589043854305438594385904385045894385430943854903584958495843054385043854854038594085430543859043584390584059458504584985049859048540540985485405843085405845848504584054895980458439054980843439805043854054805804854390584354098588504305809874329847329843298*9832479873298743298743298734339854398574398584395784397543754389759843759843589437598437598437587854374347327443275438754398759843754375438758437598435784375437054328478754375447543875435743754375435743857439854398543875438759843759843754375943754375843754375435743754387545875485758745984375822222222222221133444444444444443754375438574357437598435743875475957487543857574375439857475845749357435743985437584984375434375987435974398754343985984357498589874375949874987597498754987598749875987459874975987439759475974398754987598749754985494385438594359435943859438543854385438543985943859043859043589043859043590435904385904359043584385439589430589043589430588549385435984395843958439058439058439054839054890548905905439058439058490584390589043589043854305438594385904385045894385430943854903584958495843054385043854854038594085430543859043584390584059458504584985049859048540540985485405843085405845848504584054895980458439054980843439805043854054805804854390584354098588504305809874329847329843298</textarea>
<!--
RESULT<input type="text" name="result" value="" size="80"><br>

Trash<input type="text" name="trash" value="" size="80"> <br>
FIRSTVAL<input type="text" name="firstval" value="" size="80"><br>
SECVAL<input type="text" name="secval" value="" size="80"><br>
tONE Small<input type="text" name="tO" value="" size="80"><br>
tTWO Big<input type="text" name="tT" value="" size="80"><br>
Sum<input type="text" name="msum" value="" size="80"><br>
-->
<textarea name="comment" cols="120" rows="20" value=""></textarea>
</FORM>
</body>
</html>

[toc] | [prev] | [next] | [standalone]


#27176

Fromjonas.thornvall@gmail.com
Date2015-04-02 13:25 -0700
Message-ID<ac19d2c4-56c8-4b90-ae20-19d06ab05fda@googlegroups.com>
In reply to#27143
Den söndag 29 mars 2015 kl. 19:24:27 UTC+2 skrev jonas.t...@gmail.com:
> Den lördag 28 mars 2015 kl. 15:20:57 UTC+1 skrev jonas.t...@gmail.com:
> > Den lördag 28 mars 2015 kl. 12:15:15 UTC+1 skrev John Harris:
> > > On Fri, 27 Mar 2015 13:48:35 -0700 (PDT), jonas.thornvall@gmail.com
> > > wrote:
> > > 
> > > >Den fredag 27 mars 2015 kl. 17:42:28 UTC+1 skrev jonas.t...@gmail.com:
> > > >> I can say i had hope for less code, those checks of lenghts ain't pretty.
> > > >> 
> > > >> <SCRIPT LANGUAGE="Javascript">
> > >   <snip>
> > > >> </HTML>
> > > >
> > > >Honestly it is magneficient it totally rocks even in unary base ;)
> > > 
> > > To complete this project you need to implement mixed radix arithmetic.
> > > For instance : 
> > >   pounds, shillings, and pence; 
> > >   hundredweights, stones, pounds, and ounces; 
> > >   yards, feet, and inches.
> > > 
> > > If you think it's difficult, remember that in Britain before 1970
> > > pounds, shillings, and pence arithmetic had to be done by 9 year olds.
> > > They were expected to be able to do addition and subtraction, but
> > > multiplication and division only by simple, decimal, numbers.
> > > 
> > >   John
> > 
> > It may be that i include doing the arithmetic in bijective bases.
> > http://jt.node365.se/bijbase.html
> 
> <SCRIPT LANGUAGE="Javascript">
> 
> /* MULTIPLY TWO VARIABLES */
> 
> function naiveMult(base, multOne, multTwo)
> {
>    // document.calc.comment.value = "[Multiply] " + multOne + sign + multTwo + " \n"
>    document.calc.comment.value = "";
>    document.calc.comment.value += "FACTOR ONE " + lengthTwo + " digits \n"
>    document.calc.comment.value += "FACTOR TWO " + length + " digits \n"
> 
>    total = [];
>    result = [];
>    total[0] = 0;
>    for (var i = 0; i < lengthTwo; i ++ )
>    {
>       multresult = [];
>       remainder = 0;
>       tempVal = 0;
>       tempDig = 0;
>       j = 0;
> 
>       if(i > 0)
>       {
>          for(zero = 0; zero < i; zero ++ ) multresult[zero] = 0;
>       }
> 
>       while(j < length)
>       {
>          intmult ++ ;
>          tempVal = (multOne[i] * multTwo[j]) + 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)
>       {
>          multresult[j + i] = remainder;
> 
>       }
>       // document.calc.comment.value += "\n SubSUM= " + total;
>       // document.calc.comment.value += "\n SubPROD= " + multresult + "\n";
>       // KOLLA NOGA
>       if (multresult.length >= total.length)
>       {
>          mlength = multresult.length;
>          mlengthTwo = total.length;
>       }
>       else
>       {
>          mlength = total.length;
>          mlengthTwo = multresult.length;
>       }
> 
>       for (k = 0; k < mlength; k ++ )
>       {
>          if (isNaN(multresult[k])) multresult[k] = 0;
>          if (isNaN(total[k])) total[k] = 0;
>       }
>       naiveAdd(base, mlength, total, multresult);
>       total = result;
>    }
> 
>    // document.calc.comment.value += "\nTOTAL PRDDUKT= " + total;
> }
> 
> /* ADD TWO VARIABLES */
> 
> function naiveAdd(base, alength, addOne, addTwo)
> {
>    // document.calc.comment.value += "\nAdd termOne+termTwo =\n" + addOne + "\n" + addTwo + " + ";
>    addResult = [];
>    remainder = 0;
>    for (i = 0; i < alength; i ++ )
>    {
>       addResult[i] = addOne[i] + addTwo[i] + remainder;
>       intadd ++ ;
>       // document.calc.comment.value += "\n subAdd = " + addResult[i];
>       if (addResult[i] >= base)
>       {
>          addResult[i] = addResult[i] - base;
>          intadd ++ ;
>          remainder = 1;
>       }
>       else
>       {
>          remainder = 0;
>       }
> 
>       // document.calc.comment.value += " DigitValue = " + addResult[i];
>       // document.calc.comment.value += " carryDigit = " + remainder;
>    }
> 
>    if (remainder == 1) addResult[i ++ ] = 1;
>    else addResult.splice(i, 1);
>    result = addResult;
> 
>    return result;
> }
> 
> /* SUBTRACT TWO VARIABLES */
> 
> function naiveSub(base, subOne, subTwo)
> {
>    result = [];
>    for (i = 0; i < length; i ++ )
>    {
>       j = 0;
> 
>       if (subOne[i] < subTwo[i])
>       {
>          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];
> 
>    }
> 
>    while (result[length - 1] == 0)
>    {
>       result.splice(length - 1, 1);
>       length += - 1;
>    }
> }
> 
> /* PREPARE STRING PAIR */
> 
> function strPrep(sign, strOne, strTwo)
> {
> 
>    arrOne = strOne.split("");
>    arrTwo = strTwo.split("");
>    arrOne.reverse();
>    arrTwo.reverse();
>    if (arrOne.length >= arrTwo.length)
>    {
>       length = arrOne.length;
>       lengthTwo = arrTwo.length;
>    }
>    else
>    {
>       length = arrTwo.length;
>       lengthTwo = arrOne.length;
>    }
>    for (i = 0; i < length; i ++ )
>    {
>       arrOne[i] = parseInt(arrOne[i]);
>       arrTwo[i] = parseInt(arrTwo[i]);
>       if (isNaN(arrOne[i])) arrOne[i] = 0;
>       if (isNaN(arrTwo[i])) arrTwo[i] = 0;
>    }
> }
> 
> /* MULTIPLY Container */
> 
> function multWrap(sign, multiply, strBase)
> {
>    tONE = parseInt(multiply[0]);
>    tTWO = parseInt(multiply[1]);
>    if(tONE <= tTWO)
>    {
>       arrOne = multiply[0];
>       arrTwo = multiply[1];
>    }
>    else
>    {
>       arrOne = multiply[1];
>       arrTwo = multiply[0];
>    }
>    strPrep(sign, arrOne, arrTwo);
>    naiveMult(base, arrOne, arrTwo);
>    // document.calc.msum.value = sum;
>    out = total.reverse().toString();
>    document.calc.comment.value += "Integer ADD operations " + intadd + " \n"
>    document.calc.comment.value += "Integer MULT operations " + intmult + " \n"
>    document.calc.comment.value += "Product digits " + out.length + "\n";
>    document.calc.comment.value += out;
> }
> 
> /* SUBTRACTION Container */
> 
> function subWrap(sign, strEval, strBase)
> {
>    tONE = parseInt(subtract[0]);
>    tTWO = parseInt(subtract[1]);
>    if(tONE < tTWO)
>    {
>       strOne = subtract[1];
>       strTwo = subtract[0];
>    }
>    else
>    {
>       strOne = subtract[0];
>       strTwo = subtract[1];
>    }
> 
>    strPrep(sign, strOne, strTwo);
>    naiveSub(base, arrOne, arrTwo);
> 
> }
> 
> /* ADDITION Container */
> 
> function addWrap(sign, strEval, strBase)
> {
>    strOne = adder[0];
>    strTwo = adder[1];
>    strPrep(sign, strOne, strTwo);
>    naiveAdd(base, length, arrOne, arrTwo);
> 
> }
> 
> /* PARSE THE STRING */
> 
> function parser(strEval, strBase)
> {
> 
>    sign = "*";
>    multiply = strEval.split("*");
>    // document.calc.firstval.value = multiply[0];
>    // document.calc.secval.value = multiply[1];
>    multWrap(sign, multiply, strBase)
>    // document.calc.tO.value = strOne;
>    // document.calc.tT.value = strTwo;
>    // sign = "-";
>    // subtract = strEval.split("-");
>    // subWrap(sign, subtract, strBase);
>    // sign = "+";
>    // adder = strEval.split("+");
>    // addWrap(sign, adder, strBase);
> }
> 
> /* [MAIN PROGRAM] */
> /* [TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT] */
> /* [GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */
> 
> function fetchValues()
> {
>    intmult = 0;
>    intadd = 0;
>    document.calc.eval.value;
>    strEval = document.calc.eval.value;
>    strBase = document.calc.formbase.value;
>    // document.calc.trash.value = "1";
>    parser(strEval, strBase);
> 
> }
> 
> /* GLOBAL */
> 
> var multiply = [];
> var subtract = [];
> var adder = [];
> var arrOne = [];
> var arrTwo = [];
> // var addOne = [];
> // var addTwo = [];
> // var subOne = [];
> // var subTwo = [];
> var total = [];
> var result = [];
> var sum = [];
> 
> length = 0;
> strOne = "";
> strTwo = "";
> base = 10;
> temp = "";
> 
> </SCRIPT>
> 
> <!DOCTYPE html>
> <html lang="en">
> <head><meta charset="utf-8"/></head>
> <body bgcolor="gold" onload="fetchValues()">
> <H1>Big numbers</H1><P>
> <FORM name=calc onSubmit="fetchValues(); return false;">
> BASE <input type="text" name="formbase" value="10" size="4"><input type=submit name="calc" value="Calc"><br>
> <textarea name="eval" cols="120" rows="20">9832479873298743298222222222222222222222222222274329873433985439857439448584395784397543754389759843759843589437598437598437587854374347327443275438754398759843754375438758437598435784375437054328478754375447543875435743754375435743857439854398543875438759843759843754375943754375843754375435743754387545875485758745984375843754375438574357437598435743875475957487543857574375439857475845749357435743985437584984375434375987435974398754343985984357498589874375949874987597498754987598749875987459874975987439759475974398754987598749754985494385438594359435943859438543854385438543985943859043859043589043859043590435904385904359043584385439589430589043589430588549385435984395843958439058439058439054839054890548905905439058439058490584390589043589043854305438594385904385045894385430943854903584958495843054385043854854038594085430543859043584390584059458504584985049859048540540985485405843085405845848504584054895980458439054980843439805043854054805804854390584354098588504305809874329847329843298*9832479873298743298743298734339854398574398584395784397543754389759843759843589437598437598437587854374347327443275438754398759843754375438758437598435784375437054328478754375447543875435743754375435743857439854398543875438759843759843754375943754375843754375435743754387545875485758745984375822222222222221133444444444444443754375438574357437598435743875475957487543857574375439857475845749357435743985437584984375434375987435974398754343985984357498589874375949874987597498754987598749875987459874975987439759475974398754987598749754985494385438594359435943859438543854385438543985943859043859043589043859043590435904385904359043584385439589430589043589430588549385435984395843958439058439058439054839054890548905905439058439058490584390589043589043854305438594385904385045894385430943854903584958495843054385043854854038594085430543859043584390584059458504584985049859048540540985485405843085405845848504584054895980458439054980843439805043854054805804854390584354098588504305809874329847329843298</textarea>
> <!--
> RESULT<input type="text" name="result" value="" size="80"><br>
> 
> Trash<input type="text" name="trash" value="" size="80"> <br>
> FIRSTVAL<input type="text" name="firstval" value="" size="80"><br>
> SECVAL<input type="text" name="secval" value="" size="80"><br>
> tONE Small<input type="text" name="tO" value="" size="80"><br>
> tTWO Big<input type="text" name="tT" value="" size="80"><br>
> Sum<input type="text" name="msum" value="" size="80"><br>
> -->
> <textarea name="comment" cols="120" rows="20" value=""></textarea>
> </FORM>
> </body>
> </html>

Elementary arithmetic in anybase.

<SCRIPT LANGUAGE="Javascript">

/* BASE CHANGER */

function newBase(number, base)
{
   number = parseInt(number);
   document.calc.expand.value += "BASE = " + 10 + " NUMBER = " + number + "\n";
   basestr = "";
   basemultip = 1;
   digit = 1;

   while (basemultip < number)
   {
      basemultip = basemultip * base;
      digit = digit + 1;
   }

   while (number > 0 && digit > 0)
   {
      i = 0;
      while (i * basemultip <= number && i < base)
      {
         j = i;
         i ++ ;
      }

      if (i > 0)
      {
         subtrahend = basemultip * j;
         number = number - subtrahend;
         basestr = basestr + j + ",";
      }

      else
      {
         basestr = basestr + 0 + ",";
      }
      basemultip = basemultip / base;
      digit -- ;
   }
   while (digit > 0)
   {
      basestr = basestr + 0 + ",";
      digit -- ;
   }

   basestr = basestr.substring(0, basestr.length - 1);
   newarr =  basestr.split(",");
   i = 0;
   while (newarr[i] == 0)
   {
      newarr.shift();
   }
   document.calc.expand.value += "BASE = "+base +" NUMBER = "+ newarr + " <-Array length = " + newarr.length + "\n";
   return newarr;
}


/* MULTIPLY TWO VARIABLES */

function naiveMult(base, multOne, multTwo)
{
   document.calc.comment.value += "[Multiply][BASE] = "+ base +" [TERMS] " + multOne + sign + multTwo + " \n"
   document.calc.comment.value += "FACTOR ONE " + lengthTwo + " digits  \n"
   document.calc.comment.value += "FACTOR TWO " + lengthOne + " digits \n"

   total = [];
   result = [];
   total[0] = 0;
   for (var i = 0; i < lengthTwo; i ++ )
   {
      multresult = [];
      remainder = 0;
      tempVal = 0;
      tempDig = 0;
      j = 0;

      if(i > 0)
      {
         for(zero = 0; zero < i; zero ++ ) multresult[zero] = 0;
      }

      while(j < lengthOne)
      {
         intmult ++ ;
         tempVal = (multOne[i] * multTwo[j]) + 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)
      {
         multresult[j + i] = remainder;
      }
      // document.calc.comment.value += "\n SubSUM= " + total;
      // document.calc.comment.value += "\n SubPROD= " + multresult + "\n";
      // KOLLA NOGA
      if (multresult.length >= total.length)
      {
         mlength = multresult.length;
         mlengthTwo = total.length;
      }
      else
      {
         mlength = total.length;
         mlengthTwo = multresult.length;
      }

      for (k = 0; k < mlength; k ++ )
      {
         if (isNaN(multresult[k])) multresult[k] = 0;
         if (isNaN(total[k])) total[k] = 0;
      }
      naiveAdd(base, mlength, total, multresult);
      total = result;
   }

   //  document.calc.comment.value += "\nTOTAL PRDDUKT= " + total;
}

/* ADD TWO VARIABLES */

function naiveAdd(base, alength, addOne, addTwo)
{
   //  document.calc.comment.value += "\nAdd termOne+termTwo =\n" + addOne + "\n" + addTwo + " + ";
   addResult = [];
   remainder = 0;
   for (i = 0; i < alength; i ++ )
   {
      intadd ++ ;
      addResult[i] = addOne[i] + addTwo[i] + remainder;
      //   document.calc.comment.value += "\n subAdd = " + addResult[i];
      if (addResult[i] >= base)
      {
         addResult[i] = addResult[i] - base;

         remainder = 1;
      }
      else
      {
         remainder = 0;
      }
      //   document.calc.comment.value += " DigitValue = " + addResult[i];
      //   document.calc.comment.value += " carryDigit = " + remainder;
   }

   if (remainder == 1) addResult[i ++ ] = 1;
   else addResult.splice(i, 1);
   result = addResult;

   return result;
}

/* SUBTRACT TWO VARIABLES */

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

      if (subOne[i] < subTwo[i])
      {
         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];

   }

   while (result[lengthOne - 1] == 0)
   {
      result.splice(lengthOne - 1, 1);
      lengthOne += - 1;
   }
}

/* PREPARE STRING PAIR */

function strPrep(sign, arrOne, arrTwo)
{
   arrOne.reverse();
   arrTwo.reverse();

   // document.calc.newbase.value += "arrOne " + arrOne + " \n"

   if (arrOne.length >= arrTwo.length)
   {
      lengthOne = arrOne.length;
      lengthTwo = arrTwo.length;
   }
   else
   {
      lengthOne = arrTwo.length;
      lengthTwo = arrOne.length;
   }
   for (i = 0; i < lengthOne; i ++ )
   {
      arrOne[i] = parseInt(arrOne[i]);
      arrTwo[i] = parseInt(arrTwo[i]);
      if (isNaN(arrOne[i])) arrOne[i] = 0;
      if (isNaN(arrTwo[i])) arrTwo[i] = 0;
   }
}

/* MULTIPLY Container */

function multWrap(sign, arrOne, arrTwo, base)
{

   strPrep(sign, arrOne, arrTwo);
   naiveMult(base, arrOne, arrTwo);
   document.calc.comment.value += sum;
   out = total.reverse().toString();
   document.calc.comment.value += "Integer ADD operations " + intadd + " \n"
   document.calc.comment.value += "Integer MULT operations " + intmult + " \n"
   document.calc.comment.value += "Product = " + out;
}

/* SUBTRACTION Container */

function subWrap(sign, strEval, strBase)
{
   tONE = parseInt(subtract[0]);
   tTWO = parseInt(subtract[1]);
   if(tONE < tTWO)
   {
      strOne = subtract[1];
      strTwo = subtract[0];
   }
   else
   {
      strOne = subtract[0];
      strTwo = subtract[1];
   }

   strPrep(sign, strOne, strTwo);
   naiveSub(base, arrOne, arrTwo);
   out = result.reverse().toString();
   //   document.calc.result.value = out;
}

/* ADDITION Container */

function addWrap(sign, strEval, strBase)
{
   strOne = adder[0];
   strTwo = adder[1];
   strPrep(sign, strOne, strTwo);
   naiveAdd(base, lengthOne, arrOne, arrTwo);
   out = result.reverse().toString();
   // document.calc.result.value = out;
}

/* PARSE THE STRING */

function parser(strEval, strBase)
{

   sign = "*";
   document.calc.comment.value = "";
   document.calc.expand.value = "";
   genArr = strEval.split("*");

   arrOne = newBase(genArr[0], base);
   arrTwo = newBase(genArr[1], base);
   document.calc.newbase.value = arrOne + sign + arrTwo;
   temp = document.calc.newbase.value;

   multWrap(sign, arrOne, arrTwo, base);



   // document.calc.firstval.value = multiply[0];
   // document.calc.secval.value = multiply[1];

   // document.calc.tO.value = strOne;
   // document.calc.tT.value = strTwo;
   // sign = "-";
   // subtract = strEval.split("-");
   // subWrap(sign, subtract, strBase);
   // sign = "+";
   // adder = strEval.split("+");
   // addWrap(sign, adder, strBase);
}

/* [MAIN PROGRAM] */
/* [TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT] */
/* [GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */

function fetchValues()
{
   intmult = 0;
   intadd = 0;
   strEval = document.calc.eval.value;
   strBase = document.calc.formbase.value;
   base = parseInt(strBase);
   parser(strEval, base);

}

/* GLOBAL */
var multiply = [];
var subtract = [];
var adder = [];
var arrOne = [];
var arrTwo = [];
var total = [];
var result = [];
var sum = [];

lengthOne = 0;
strOne = "";
strTwo = "";
base = 10;
temp = "";
sign = "";
</SCRIPT>

<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"/></head>
<body bgcolor="gold" onload="fetchValues()">
<H1>Big numbers</H1><P>
<FORM name=calc onSubmit="fetchValues(); return false;">
<B>
INPUT DEC-><input type=submit name="calc" value="Calc"> <input type="text" name="eval" value="204543456*20225673" size="60"><br>
BASE <input type="text" name="formbase" value="4096" size="10"><br>
This calculation<input type="text" name="newbase" value="" size="60"><br>
<FONT COLOR="white">
EVAL STATS.<br>
<textarea name="expand" rows="20" cols="120"></textarea><br>

<!--
RESULT<input type="text" name="result" value="" size="80"><br>
FIRSTVAL<input type="text" name="firstval" value="" size="80"><br>
SECVAL<input type="text" name="secval" value="" size="80"><br>
tONE Small<input type="text" name="tO" value="" size="80"><br>
tTWO Big<input type="text" name="tT" value="" size="80"><br>
Sum<input type="text" name="msum" value="" size="80"><br>
-->
RESULT</B><br>
<textarea name="comment" cols="120" rows="40" value=""></textarea>
</FORM>
</body>
</html>

[toc] | [prev] | [next] | [standalone]


#27177

Fromjonas.thornvall@gmail.com
Date2015-04-02 16:24 -0700
Message-ID<9d8378ed-1c4f-44d1-9da4-04e30308cd17@googlegroups.com>
In reply to#27135
Den fredag 27 mars 2015 kl. 17:42:28 UTC+1 skrev jonas.t...@gmail.com:
> I can say i had hope for less code, those checks of lenghts ain't pretty.
> 
> <SCRIPT LANGUAGE="Javascript">
> 
> /* MULTIPLY TWO VARIABLES */
> 
> function naiveMult(base, multOne, multTwo)
> {
>    document.calc.comment.value = "[Multiply] "+ multOne + sign + multTwo+" \n"
>  
>    total = [];
>    result = [];
>    total[0] = 0;
>    for (var i = 0; i < lengthTwo; i ++ )
>    {
>       multresult = [];
>       remainder = 0;
>       tempVal = 0;
>       tempDig = 0;
>       j = 0;
> 
>       if(i > 0)
>       {
>          for(zero = 0; zero < i; zero ++ )  multresult[zero] = 0;
>       }
> 
>       while(j < length)
>       {
>          tempVal = (multOne[i] * multTwo[j]) + 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)
>       {
>          multresult[j + i] = remainder;
>       }
>       document.calc.comment.value += "\n SubSUM= " + total;
>       document.calc.comment.value += "\n SubPROD= " + multresult;
>       // KOLLA NOGA
>       if (multresult.length >= total.length)
>       {
>          mlength = multresult.length;
>          mlengthTwo = total.length;
>       }
>       else
>       {
>          mlength = total.length;
>          mlengthTwo = multresult.length;
>       }
> 
>       for (k = 0; k < mlength; k ++ )
>       {
>          if (isNaN(multresult[k])) multresult[k] = 0;
>          if (isNaN(total[k])) total[k] = 0;
>       }
>       naiveAdd(base, mlength, total, multresult);
>       total = result;
>    }
>    document.calc.comment.value += "\nTOTAL PRDDUKT= " + total;
> }
> 
> 
> /* ADD TWO VARIABLES */
> 
> function naiveAdd(base, alength, addOne, addTwo)
> {
>    document.calc.comment.value += "\nAdd termOne+termTwo =\n" + addOne+"\n"  + addTwo + " + ";
>    addResult = [];
>    remainder = 0;
>    for (i = 0; i < alength; i ++ )
>    {
>       addResult[i] = addOne[i] + addTwo[i] + remainder;
>       document.calc.comment.value += "\n subAdd = " + addResult[i];
>       if (addResult[i] >= base)
>       {
>          addResult[i] = addResult[i] - base;
>          //document.calc.comment.value += "\n *IF* Addresult = " + addResult[i];
>          remainder = 1;
>       }
>       else
>       {
>          remainder = 0;
>       }
>    }
> 
>    if (remainder == 1) addResult[i ++ ] = 1;
>    else addResult.splice(i, 1);
>    result = addResult;
>    document.calc.comment.value += "\n SUBSUM= " + addResult;
>    return result;
> }
> 
> 
> 
> 
> /* SUBTRACT TWO VARIABLES */
> 
> function naiveSub(base, subOne, subTwo)
> {
>    result = [];
>    for (i = 0; i < length; i ++ )
>    {
>       j = 0;
> 
>       if (subOne[i] < subTwo[i])
>       {
>          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];
> 
>    }
> 
>    while (result[length - 1] == 0)
>    {
>       result.splice(length - 1, 1);
>       length += - 1;
>    }
> }
> 
> 
> 
> 
> 
> /* PREPARE STRING PAIR */
> 
> function strPrep(sign, strOne, strTwo)
> {
> 
>    arrOne = strOne.split("");
>    arrTwo = strTwo.split("");
>    arrOne.reverse();
>    arrTwo.reverse();
>    if (arrOne.length >= arrTwo.length)
>    {
>       length = arrOne.length;
>       lengthTwo = arrTwo.length;
>    }
>    else
>    {
>       length = arrTwo.length;
>       lengthTwo = arrOne.length;
>    }
>    for (i = 0; i < length; i ++ )
>    {
>       arrOne[i] = parseInt(arrOne[i]);
>       arrTwo[i] = parseInt(arrTwo[i]);
>       if (isNaN(arrOne[i])) arrOne[i] = 0;
>       if (isNaN(arrTwo[i])) arrTwo[i] = 0;
>    }
> }
> 
> /* MULTIPLY Container */
> 
> function multWrap(sign, multiply, strBase)
> {
>    tONE = parseInt(multiply[0]);
>    tTWO = parseInt(multiply[1]);
>    if(tONE <= tTWO)
>    {
>       arrOne = multiply[0];
>       arrTwo = multiply[1];
>    }
>    else
>    {
>       arrOne = multiply[1];
>       arrTwo = multiply[0];
>    }
>    strPrep(sign, arrOne, arrTwo);
>    naiveMult(base, arrOne, arrTwo);
>    document.calc.msum.value = sum;
>    out = total.reverse().toString();
>    document.calc.result.value = out;
> }
> 
> /* SUBTRACTION Container */
> 
> function subWrap(sign, strEval, strBase)
> {
>    tONE = parseInt(subtract[0]);
>    tTWO = parseInt(subtract[1]);
>    if(tONE < tTWO)
>    {
>       strOne = subtract[1];
>       strTwo = subtract[0];
>    }
>    else
>    {
>       strOne = subtract[0];
>       strTwo = subtract[1];
>    }
> 
> 
>    strPrep(sign, strOne, strTwo);
>    naiveSub(base, arrOne, arrTwo);
>    out = result.reverse().toString();
>    document.calc.result.value = out;
> }
> 
> /* ADDITION Container */
> 
> function addWrap(sign, strEval, strBase)
> {
>    strOne = adder[0];
>    strTwo = adder[1];
>    strPrep(sign, strOne, strTwo);
>    naiveAdd(base, length, arrOne, arrTwo);
>    out = result.reverse().toString();
>    document.calc.result.value = out;
> }
> 
> /* PARSE THE STRING */
> 
> function parser(strEval, strBase)
> {
> 
>    sign = "*";
>     multiply = strEval.split("*");
>    // document.calc.firstval.value = multiply[0];
>    // document.calc.secval.value = multiply[1];
>     multWrap(sign, multiply, strBase)
>     document.calc.tO.value = strOne;
>    // document.calc.tT.value = strTwo;
>    //  sign = "-";
>    //  subtract = strEval.split("-");
>    //  subWrap(sign, subtract, strBase);
>    //sign = "+";
>    //adder = strEval.split("+");
>    //addWrap(sign, adder, strBase);
> }
> 
> /* [MAIN PROGRAM] */
> /* [TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT] */
> /* [GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */
> 
> function fetchValues()
> {
> 
>    strEval = document.calc.eval.value;
>    strBase = document.calc.formbase.value;
>    document.calc.trash.value = "1";
>    base = parseInt(strBase);
>    parser(strEval, strBase);
> 
> 
> }
> 
> /* GLOBAL */
> var multiply = [];
> var subtract = [];
> var adder  = [];
> var arrOne = [];
> var arrTwo = [];
> // var addOne = [];
> // var addTwo = [];
> // var subOne = [];
> // var subTwo = [];
> var total = [];
> var result = [];
> var sum = [];
> 
> length = 0;
> strOne = "";
> strTwo = "";
> base = 10;
> temp = "";
> 
> </SCRIPT>
> 
> <HTML>
>  <body bgcolor="gold" onload="fetchValues()">
> <FORM name=calc onSubmit="fetchValues(); return false;">
> BASE <input type="text" name="formbase" value="10" size="4"><br>
> <input type=submit name="calc" value="Calc"><input type="text" name="eval" value="2213213219382198348348338*2382828248324834832" size="80"><br>
> RESULT<input type="text" name="result" value="" size="80"><br>
> Trash<input type="text" name="trash" value="" size="80"> <br>
> FIRSTVAL<input type="text" name="firstval" value="" size="80"><br>
> SECVAL<input type="text" name="secval" value="" size="80"><br>
> tONE Small<input type="text" name="tO" value="" size="80"><br>
> tTWO Big<input type="text" name="tT" value="" size="80"><br>
> Sum<input type="text" name="msum" value="" size="80"><br>
> <textarea name="comment" cols="80" rows="20" value=""></textarea>
> </FORM>
> </BODY>
> </HTML>
<SCRIPT LANGUAGE="Javascript">

/* BASE CHANGER */

function newBase(number, base)
{
   number = parseInt(number);
   document.calc.expand.value += "BASE = " + 10 + " NUMBER = " + number + "\n";
   basestr = "";
   basemultip = 1;
   digit = 1;

   while (basemultip < number)
   {
      basemultip = basemultip * base;
      digit = digit + 1;
   }

   while (number > 0 && digit > 0)
   {
      i = 0;
      while (i * basemultip <= number && i < base)
      {
         j = i;
         i ++ ;
      }

      if (i > 0)
      {
         subtrahend = basemultip * j;
         number = number - subtrahend;
         basestr = basestr + j + ",";
      }

      else
      {
         basestr = basestr + 0 + ",";
      }
      basemultip = basemultip / base;
      digit -- ;
   }
   while (digit > 0)
   {
      basestr = basestr + 0 + ",";
      digit -- ;
   }

   basestr = basestr.substring(0, basestr.length - 1);
   newarr =  basestr.split(",");
   i = 0;
   while (newarr[i] == 0)
   {
      newarr.shift();
   }
   sanityCheck(base,newarr);
 
   document.calc.expand.value += "BASE = " + base + " NUMBER = " + newarr + " <-Array length = " + newarr.length + "\n";
   return newarr;
}


/* MULTIPLY TWO VARIABLES */

function naiveMult(base, multOne, multTwo)
{
   document.calc.comment.value += "[Multiply][BASE] = " + base + " [TERMS] " + multOne + sign + multTwo + " \n"
   document.calc.comment.value += "FACTOR ONE " + lengthTwo + " digits  \n"
   document.calc.comment.value += "FACTOR TWO " + lengthOne + " digits \n"

   total = [];
   result = [];
   total[0] = 0;
   for (var i = 0; i < lengthTwo; i ++ )
   {
      multresult = [];
      remainder = 0;
      tempVal = 0;
      tempDig = 0;
      j = 0;

      if(i > 0)
      {
         for(zero = 0; zero < i; zero ++ ) multresult[zero] = 0;
      }

      while(j < lengthOne)
      {
         intmult ++ ;
         tempVal = (multOne[i] * multTwo[j]) + 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)
      {
         multresult[j + i] = remainder;
      }
      // document.calc.comment.value += "\n SubSUM= " + total;
      // document.calc.comment.value += "\n SubPROD= " + multresult + "\n";
      // KOLLA NOGA
      if (multresult.length >= total.length)
      {
         mlength = multresult.length;
         mlengthTwo = total.length;
      }
      else
      {
         mlength = total.length;
         mlengthTwo = multresult.length;
      }

      for (k = 0; k < mlength; k ++ )
      {
         if (isNaN(multresult[k])) multresult[k] = 0;
         if (isNaN(total[k])) total[k] = 0;
      }
      naiveAdd(base, mlength, total, multresult);
      total = result;
   }
  
   
}

/* ADD TWO VARIABLES */

function naiveAdd(base, alength, addOne, addTwo)
{
   //  document.calc.comment.value += "\nAdd termOne+termTwo =\n" + addOne + "\n" + addTwo + " + ";
   addResult = [];
   remainder = 0;
   for (i = 0; i < alength; i ++ )
   {
      intadd ++ ;
      addResult[i] = addOne[i] + addTwo[i] + remainder;
      //   document.calc.comment.value += "\n subAdd = " + addResult[i];
      if (addResult[i] >= base)
      {
         addResult[i] = addResult[i] - base;

         remainder = 1;
      }
      else
      {
         remainder = 0;
      }
      //   document.calc.comment.value += " DigitValue = " + addResult[i];
      //   document.calc.comment.value += " carryDigit = " + remainder;
   }

   if (remainder == 1) addResult[i ++ ] = 1;
   else addResult.splice(i, 1);
   result = addResult;

   return result;
}

/* SUBTRACT TWO VARIABLES */

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

      if (subOne[i] < subTwo[i])
      {
         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];

   }

   while (result[lengthOne - 1] == 0)
   {
      result.splice(lengthOne - 1, 1);
      lengthOne += - 1;
   }
}

/* PREPARE STRING PAIR */

function strPrep(sign, arrOne, arrTwo)
{
   arrOne.reverse();
   arrTwo.reverse();

   // document.calc.newbase.value += "arrOne " + arrOne + " \n"

   if (arrOne.length >= arrTwo.length)
   {
      lengthOne = arrOne.length;
      lengthTwo = arrTwo.length;
   }
   else
   {
      lengthOne = arrTwo.length;
      lengthTwo = arrOne.length;
   }
   for (i = 0; i < lengthOne; i ++ )
   {
      arrOne[i] = parseInt(arrOne[i]);
      arrTwo[i] = parseInt(arrTwo[i]);
      if (isNaN(arrOne[i])) arrOne[i] = 0;
      if (isNaN(arrTwo[i])) arrTwo[i] = 0;
   }
}

/* MULTIPLY Container */

function multWrap(sign, arrOne, arrTwo, base)
{

   strPrep(sign, arrOne, arrTwo);
   
   naiveMult(base, arrOne, arrTwo);
   document.calc.comment.value += sum;
   out = total.reverse();
   sanityCheck(base,out);
   document.calc.comment.value += "Integer ADD operations " + intadd + " \n"
   document.calc.comment.value += "Integer MULT operations " + intmult + " \n"
  
   document.calc.comment.value += "Product = " + out;
}

/* SUBTRACTION Container */

function subWrap(sign, strEval, strBase)
{
   tONE = parseInt(subtract[0]);
   tTWO = parseInt(subtract[1]);
   if(tONE < tTWO)
   {
      strOne = subtract[1];
      strTwo = subtract[0];
   }
   else
   {
      strOne = subtract[0];
      strTwo = subtract[1];
   }

   strPrep(sign, strOne, strTwo);
   naiveSub(base, arrOne, arrTwo);
   out = result.reverse().toString();
   //   document.calc.result.value = out;
}

/* ADDITION Container */

function addWrap(sign, strEval, strBase)
{
   strOne = adder[0];
   strTwo = adder[1];
   strPrep(sign, strOne, strTwo);
   naiveAdd(base, lengthOne, arrOne, arrTwo);
   out = result.reverse().toString();
   // document.calc.result.value = out;
}

/* PARSE THE STRING */

function parser(strEval, strBase)
{

   sign = "*";
   document.calc.comment.value = "";
   document.calc.expand.value = "";
   genArr = strEval.split("*");

   arrOne = newBase(genArr[0], base);

   arrTwo = newBase(genArr[1], base);
   document.calc.newbase.value = arrOne + sign + arrTwo;
   temp = document.calc.newbase.value;

   multWrap(sign, arrOne, arrTwo, base);



   // document.calc.firstval.value = multiply[0];
   // document.calc.secval.value = multiply[1];

   // document.calc.tO.value = strOne;
   // document.calc.tT.value = strTwo;
   // sign = "-";
   // subtract = strEval.split("-");
   // subWrap(sign, subtract, strBase);
   // sign = "+";
   // adder = strEval.split("+");
   // addWrap(sign, adder, strBase);
}

/* [MAIN PROGRAM] */
/* [TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT] */
/* [GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */

function sanityCheck(bmult, input)
{  totsane=0;
   document.calc.expand.value += "[SANITY CHECK] BASE "+bmult+" \n";
   btemp=1;
   for(x = input.length-1;x >=0; x--)
   {
      sane = btemp * input[x];
      
      document.calc.expand.value += "PartProd = "+btemp+"*"+input[x]+"="+ sane + " \n";
      totsane+=sane
      btemp=bmult*btemp;
   }
   document.calc.expand.value += "TotProd = "+totsane+" \n";
}




function fetchValues()
{
   intmult = 0;
   intadd = 0;
   strEval = document.calc.eval.value;
   strBase = document.calc.formbase.value;
   base = parseInt(strBase);
   parser(strEval, base);

}

/* GLOBAL */
var multiply = [];
var subtract = [];
var adder = [];
var arrOne = [];
var arrTwo = [];
var total = [];
var result = [];
var sum = [];

lengthOne = 0;
strOne = "";
strTwo = "";
base = 10;
temp = "";
sign = "";
</SCRIPT>

<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"/></head>
<body bgcolor="gold" onload="fetchValues()">
<H1>Big numbers</H1><P>
<FORM name=calc onSubmit="fetchValues(); return false;">
<B>
INPUT DEC-><input type=submit name="calc" value="Calc"> <input type="text" name="eval" value="12345*123456" size="60"><br>
BASE <input type="text" name="formbase" value="10" size="10"><br>
This calculation<input type="text" name="newbase" value="" size="60"><br>
<FONT COLOR="white">
EVAL STATS.<br>
<textarea name="expand" rows="20" cols="120"></textarea><br>

<!--
RESULT<input type="text" name="result" value="" size="80"><br>
FIRSTVAL<input type="text" name="firstval" value="" size="80"><br>
SECVAL<input type="text" name="secval" value="" size="80"><br>
tONE Small<input type="text" name="tO" value="" size="80"><br>
tTWO Big<input type="text" name="tT" value="" size="80"><br>
Sum<input type="text" name="msum" value="" size="80"><br>
-->
RESULT</B><br>
<textarea name="comment" cols="120" rows="40" value=""></textarea>
</FORM>
</body>
</html>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web