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


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

Overflow, integers and strings

Started byjonas.thornvall@gmail.com
First post2015-04-30 00:11 -0700
Last post2015-04-30 10:52 -0700
Articles 20 on this page of 105 — 9 participants

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


Contents

  Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 00:11 -0700
    Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 01:29 -0700
      Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 03:31 -0700
      Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 03:33 -0700
        Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-04-30 13:38 +0200
          Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 08:20 -0700
            Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-04-30 20:37 +0200
              Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 23:46 -0700
                Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 01:01 -0700
                  Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 01:47 -0700
                  Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-01 10:50 +0200
                    Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 02:04 -0700
                  Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-01 11:08 +0100
                    Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 04:43 -0700
                      Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 04:46 -0700
                Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-01 10:48 +0200
                  Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-01 10:58 +0100
                    Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-01 14:34 +0200
                      Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-01 14:47 +0200
                      Re: Overflow, integers and strings "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-01 15:04 +0200
                        Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 06:18 -0700
                          Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 06:24 -0700
                        Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 06:49 -0700
                          Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 07:00 -0700
                            Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 07:17 -0700
                            Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 07:21 -0700
                              Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 07:37 -0700
                                Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 11:27 -0700
                                  Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 12:34 -0700
                        Re: Overflow, integers and strings Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-01 22:42 +0200
                          Re: Overflow, integers and strings "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-01 23:37 +0200
                            Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-02 00:19 +0200
                              Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-02 05:50 -0700
                                Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-02 14:21 +0100
                                Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-03 04:30 -0700
                                  Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-03 07:12 -0700
                                    Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-03 16:45 +0100
                                      Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-03 09:17 -0700
                                      Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-03 09:22 -0700
                                        Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 03:38 -0700
                                          Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-04 14:01 +0100
                                            Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 06:41 -0700
                                              Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-04 15:39 +0100
                                                Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 07:48 -0700
                                            Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 06:49 -0700
                                              Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 07:25 -0700
                                                Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 08:26 -0700
                                                  Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 08:59 -0700
                                                    Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-05 00:59 +0100
                                              Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-04 15:28 +0100
                                                Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 07:34 -0700
                                                  Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-04 15:41 +0100
                                                    Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 07:52 -0700
                                                      Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 08:03 -0700
                                                    Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 17:56 +0200
                                                      Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 09:04 -0700
                                                        Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 09:21 -0700
                                                          Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 09:38 -0700
                                                        Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 21:07 +0200
                                                          Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 12:25 -0700
                                                            Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 21:43 +0200
                                                          Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 12:46 -0700
                                                            Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 22:11 +0200
                                                          Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 12:53 -0700
                                                            Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 22:23 +0200
                                                              Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 13:40 -0700
                                                                Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 23:12 +0200
                                                          Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 13:06 -0700
                                                            Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 22:39 +0200
                                                              Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 13:42 -0700
                                                            Re: Overflow, integers and strings Scott Sauyet <scott.sauyet@gmail.com> - 2015-05-04 13:40 -0700
                                                              Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 13:47 -0700
                                                                Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 23:30 +0200
                                                                  Re: Overflow, integers and strings "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-04 23:33 +0200
                                                                  Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 15:53 -0700
                                                              Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 13:51 -0700
                                                                Re: Overflow, integers and strings Scott Sauyet <scott.sauyet@gmail.com> - 2015-05-04 18:00 -0700
                                                                  Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 18:22 -0700
                                                                    Re: Overflow, integers and strings Scott Sauyet <scott.sauyet@gmail.com> - 2015-05-04 21:38 -0700
                                                                  Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 18:37 -0700
                                                              Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 14:00 -0700
                                                      Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 09:19 -0700
                                                        Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 21:24 +0200
                                                      Re: Overflow, integers and strings wolfgang zeidler <invalid@invalid.invalid> - 2015-05-04 21:37 +0200
                                                        Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 21:56 +0200
                                                          Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 13:21 -0700
                                                          Re: Overflow, integers and strings wolfgang zeidler <invalid@invalid.invalid> - 2015-05-04 22:38 +0200
                                                            Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 22:46 +0200
                                                              Re: Overflow, integers and strings "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-04 22:57 +0200
                                                                Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 23:17 +0200
                                                                Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-05 00:45 +0100
                                                                  Re: Overflow, integers and strings "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-06 00:01 +0200
                                                                Re: Overflow, integers and strings Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-05 08:14 +0200
                                                                  Re: Overflow, integers and strings "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-05 23:54 +0200
                                                              Re: Overflow, integers and strings wolfgang zeidler <invalid@invalid.invalid> - 2015-05-04 23:18 +0200
                                                                Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 23:53 +0200
                                                                  Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 16:06 -0700
                                                                    Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-05 10:26 +0200
                                                                  Re: Overflow, integers and strings Dr J R Stockton <reply1500@merlyn.demon.co.uk.invalid> - 2015-05-05 19:59 +0100
                                                Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 07:40 -0700
                                                  Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-05 01:10 +0100
                  Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 05:03 -0700
          Re: Overflow, integers and strings Ben Bacarisse <ben.lists@bsb.me.uk> - 2015-04-30 20:36 +0100
            Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 13:08 -0700
    Re: Overflow, integers and strings Scott Sauyet <scott.sauyet@gmail.com> - 2015-04-30 10:52 -0700

Page 2 of 6 — ← Prev page 1 [2] 3 4 5 6  Next page →


#27456

Fromjonas.thornvall@gmail.com
Date2015-05-01 06:18 -0700
Message-ID<ae0781a3-cd0f-410c-9aed-4d3f60e80c30@googlegroups.com>
In reply to#27455
Den fredag 1 maj 2015 kl. 15:04:34 UTC+2 skrev Christoph M. Becker:
> Evertjan. wrote:
> 
> > 'this' works:
> 
> Not necessarily:
> 
>     function test() {
> 
>       var a = [1], b = [2];
> 
>       function arraySwap(x,y) {
>         var tempArrName = this[y];
>         this[y] = this[x];
>         this[x] = tempArrName;
>       }
> 
>       console.log(a, b);
>       arraySwap(a, b);
>       console.log(a, b);
>     }
> 
> -- 
> Christoph M. Becker

Now i pass the original global arrays as parameters and i can see it do not work but why doesn't it work?

Have i got something wrong about variables and passing function parameters?

<SCRIPT LANGUAGE="Javascript">

// Compare numerical size of two "reversed"arrays and their indexes
// If A < B returns true otherwise false

function arrCompareSmaller(A, B)
{  
  var AA=A.length;
  var BB=B.length;
   if(AA > BB) return false;
   if(BB < AA) 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 arraySwap(subOne, subTwo) { // 2 different arrays expected
   var tempArrName = subOne;
   subTwo = subOne;
   subOne = tempArrName;
   // the name tempArrName will be garbagecollected automagically
};

function arraySwapIfSmaller(subOne, subTwo) { // arrays by reference
   if (arrCompareSmaller(subOne, subTwo))
      arraySwap(subOne, subTwo);
}; 



//Prepare arrays for subtraction

function prepSubt(subOne,subTwo){

//If subOne smaller need to swap arrays
//Function naiveSub need to be fed bigger-smaller, parser keep track sign
arraySwapIfSmaller(subOne,subTwo);

//Check length of biggest array store bigArr
if(subOne.length<subTwo.length) {
          shortArr = subOne.length;   bigArr = subTwo.length;
          } else {bigArr = subOne.length; shortArr = subTwo.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;
   } 
document.calc.outbase.value += subOne+ " - \n";
document.calc.outbase.value += subTwo+ "\n";
}

/* SUBTRACT TWO VARIABLES */
/* THE NOTATION BASE TEN, SO FOR INDEX VALUES IF BASE 2000, DIGITVALUE IN INDEX RANGE = (0-1999)*/
/* VARIABLES base INTEGER, subOne, subTwo  ARRAYS WITH INDEX VALUES REVERSED */
/* A SINGLE "DIGITPLACE/VALUE" OF BASE, HOLD IN EACH INDEX */

function naiveSub(base, subOne, subTwo)
{
   prepSubt(subOne,subTwo);
   result=[];
   for (i = 0; i < shortArr; i ++ )
   {
      j = 0;
    // document.write(subOne[i],"-",subTwo[i],"First<br>"); 
      if (subOne[i] < subTwo[i])
      {  
         intCarry ++ ;
         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++;
  //  document.write(subOne[i],"-",subTwo[i],"=",result[i],"<br>"); 
  }
   while(i<bigArr) { result[i]=subOne[i];i++;}
   while (result[bigArr - 1] == 0)
   {
      result.splice(bigArr - 1, 1);
      bigArr += - 1;
   }
   return result;
}

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

function fetchVal()
{
   document.calc.result.value = "";
   document.calc.stats.value = "";
   document.calc.outbase.value = "";
   strEval = document.calc.eval.value;
   base = document.calc.formbase.value;
   base = parseInt(base);
}

/* SPLIT OPERAND PAIR AND DECIDE OPERATION */

function parsePair()
{
   genArr = strEval.split("-");
   return genArr;
}

/* IF BASE 10 NO CONVERSION NEEDED "ELSE" DO CONVERSION TO NEWBASE */

function ifBaseTen()
{
   if(base != 10)
   {
      arrOne = newbase(genArr[0], base);
      tempA = arrOne;
      arrTwo = newbase(genArr[1], base);
      tempB = arrTwo;
   }
   else
   {
      arrOne = genArr[0].split("").reverse();
      tempA = arrOne;
      arrTwo = genArr[1].split("").reverse();
      tempB = arrTwo;
   }
}

/* RESULT OUT TO FORM */

function resOutForm()
{

   document.calc.stats.value += "INT 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 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 */




// Arrays holding digitplaces
arrOne = [];
arrTwo = [];
subOne = [];
subTwo = [];
tempA = [];
tempB = [];
tempArr = [];

/* MAIN FUNCTION */

function MAIN()
{  
   // Counters, adds, subtraction, multiplications
intMult = 0;
intAdd = 0;
intSub = 0;
intCarry = 0;
// Length of the operands
bigArr = 0;
shortArr = 0;
//  Fetched variables from form base and string to evaluate
base = 0;
strEval = "";
   fetchVal();
   genArr = parsePair();
   ifBaseTen();
   out = naiveSub(base, arrOne, arrTwo);
   resOutForm();
}
</SCRIPT>


<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"/></head>
<body bgcolor="gold" onload="MAIN()">
<H3>ARITHMETICS IN ANYBASE USING BIG NUMBERS</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="10" size="10"><br><BR>
Evaluate <input type="text" name="eval" value="3313-3315" size="300"><br>
Base conv.(rev arrays)<input type="text" name="outbase" value="" size="60"><br>
<FONT COLOR="gray">
RESULT<br>
<textarea name="result" rows="20" cols="120"></textarea><br>
STATUS</B><br>
<textarea name="stats" cols="120" rows="40" value=""></textarea>
</FORM>
</body>
</html>

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


#27457

Fromjonas.thornvall@gmail.com
Date2015-05-01 06:24 -0700
Message-ID<e62d3171-2c67-4679-a64a-3c90f806946d@googlegroups.com>
In reply to#27456
Den fredag 1 maj 2015 kl. 15:18:18 UTC+2 skrev jonas.t...@gmail.com:
> Den fredag 1 maj 2015 kl. 15:04:34 UTC+2 skrev Christoph M. Becker:
> > Evertjan. wrote:
> > 
> > > 'this' works:
> > 
> > Not necessarily:
> > 
> >     function test() {
> > 
> >       var a = [1], b = [2];
> > 
> >       function arraySwap(x,y) {
> >         var tempArrName = this[y];
> >         this[y] = this[x];
> >         this[x] = tempArrName;
> >       }
> > 
> >       console.log(a, b);
> >       arraySwap(a, b);
> >       console.log(a, b);
> >     }
> > 
> > -- 
> > Christoph M. Becker
> 
> Now i pass the original global arrays as parameters and i can see it do not work but why doesn't it work?
> 
> Have i got something wrong about variables and passing function parameters?
> 
> <SCRIPT LANGUAGE="Javascript">
> 
> // Compare numerical size of two "reversed"arrays and their indexes
> // If A < B returns true otherwise false
> 
> function arrCompareSmaller(A, B)
> {  
>   var AA=A.length;
>   var BB=B.length;
>    if(AA > BB) return false;
>    if(BB < AA) 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 arraySwap(subOne, subTwo) { // 2 different arrays expected
>    var tempArrName = subOne;
>    subTwo = subOne;
>    subOne = tempArrName;
>    // the name tempArrName will be garbagecollected automagically
> };
> 
> function arraySwapIfSmaller(subOne, subTwo) { // arrays by reference
>    if (arrCompareSmaller(subOne, subTwo))
>       arraySwap(subOne, subTwo);
> }; 
> 
> 
> 
> //Prepare arrays for subtraction
> 
> function prepSubt(subOne,subTwo){
> 
> //If subOne smaller need to swap arrays
> //Function naiveSub need to be fed bigger-smaller, parser keep track sign
> arraySwapIfSmaller(subOne,subTwo);
> 
> //Check length of biggest array store bigArr
> if(subOne.length<subTwo.length) {
>           shortArr = subOne.length;   bigArr = subTwo.length;
>           } else {bigArr = subOne.length; shortArr = subTwo.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;
>    } 
> document.calc.outbase.value += subOne+ " - \n";
> document.calc.outbase.value += subTwo+ "\n";
> }
> 
> /* SUBTRACT TWO VARIABLES */
> /* THE NOTATION BASE TEN, SO FOR INDEX VALUES IF BASE 2000, DIGITVALUE IN INDEX RANGE = (0-1999)*/
> /* VARIABLES base INTEGER, subOne, subTwo  ARRAYS WITH INDEX VALUES REVERSED */
> /* A SINGLE "DIGITPLACE/VALUE" OF BASE, HOLD IN EACH INDEX */
> 
> function naiveSub(base, subOne, subTwo)
> {
>    prepSubt(subOne,subTwo);
>    result=[];
>    for (i = 0; i < shortArr; i ++ )
>    {
>       j = 0;
>     // document.write(subOne[i],"-",subTwo[i],"First<br>"); 
>       if (subOne[i] < subTwo[i])
>       {  
>          intCarry ++ ;
>          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++;
>   //  document.write(subOne[i],"-",subTwo[i],"=",result[i],"<br>"); 
>   }
>    while(i<bigArr) { result[i]=subOne[i];i++;}
>    while (result[bigArr - 1] == 0)
>    {
>       result.splice(bigArr - 1, 1);
>       bigArr += - 1;
>    }
>    return result;
> }
> 
> /* RESET FORM FIELDS, FETCH BASE AND STRING TO EVALUATE */
> 
> function fetchVal()
> {
>    document.calc.result.value = "";
>    document.calc.stats.value = "";
>    document.calc.outbase.value = "";
>    strEval = document.calc.eval.value;
>    base = document.calc.formbase.value;
>    base = parseInt(base);
> }
> 
> /* SPLIT OPERAND PAIR AND DECIDE OPERATION */
> 
> function parsePair()
> {
>    genArr = strEval.split("-");
>    return genArr;
> }
> 
> /* IF BASE 10 NO CONVERSION NEEDED "ELSE" DO CONVERSION TO NEWBASE */
> 
> function ifBaseTen()
> {
>    if(base != 10)
>    {
>       arrOne = newbase(genArr[0], base);
>       tempA = arrOne;
>       arrTwo = newbase(genArr[1], base);
>       tempB = arrTwo;
>    }
>    else
>    {
>       arrOne = genArr[0].split("").reverse();
>       tempA = arrOne;
>       arrTwo = genArr[1].split("").reverse();
>       tempB = arrTwo;
>    }
> }
> 
> /* RESULT OUT TO FORM */
> 
> function resOutForm()
> {
> 
>    document.calc.stats.value += "INT 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 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 */
> 
> 
> 
> 
> // Arrays holding digitplaces
> arrOne = [];
> arrTwo = [];
> subOne = [];
> subTwo = [];
> tempA = [];
> tempB = [];
> tempArr = [];
> 
> /* MAIN FUNCTION */
> 
> function MAIN()
> {  
>    // Counters, adds, subtraction, multiplications
> intMult = 0;
> intAdd = 0;
> intSub = 0;
> intCarry = 0;
> // Length of the operands
> bigArr = 0;
> shortArr = 0;
> //  Fetched variables from form base and string to evaluate
> base = 0;
> strEval = "";
>    fetchVal();
>    genArr = parsePair();
>    ifBaseTen();
>    out = naiveSub(base, arrOne, arrTwo);
>    resOutForm();
> }
> </SCRIPT>
> 
> 
> <!DOCTYPE html>
> <html lang="en">
> <head><meta charset="utf-8"/></head>
> <body bgcolor="gold" onload="MAIN()">
> <H3>ARITHMETICS IN ANYBASE USING BIG NUMBERS</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="10" size="10"><br><BR>
> Evaluate <input type="text" name="eval" value="3313-3315" size="300"><br>
> Base conv.(rev arrays)<input type="text" name="outbase" value="" size="60"><br>
> <FONT COLOR="gray">
> RESULT<br>
> <textarea name="result" rows="20" cols="120"></textarea><br>
> STATUS</B><br>
> <textarea name="stats" cols="120" rows="40" value=""></textarea>
> </FORM>
> </body>
> </html>

Sorry about the mixup subOne subTwo, but it still does not work

function arraySwap(subOne, subTwo) { // 2 different arrays expected
   var tempArrName = subOne;
   subOne = subTwo;
   subTwo = tempArrName;
   // the name tempArrName will be garbagecollected automagically
};

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


#27458

Fromjonas.thornvall@gmail.com
Date2015-05-01 06:49 -0700
Message-ID<4e581e04-7354-4c78-be80-46e7fac3f165@googlegroups.com>
In reply to#27455
Den fredag 1 maj 2015 kl. 15:04:34 UTC+2 skrev Christoph M. Becker:
> Evertjan. wrote:
> 
> > 'this' works:
> 
> Not necessarily:
> 
>     function test() {
> 
>       var a = [1], b = [2];
> 
>       function arraySwap(x,y) {
>         var tempArrName = this[y];
>         this[y] = this[x];
>         this[x] = tempArrName;
>       }
> 
>       console.log(a, b);
>       arraySwap(a, b);
>       console.log(a, b);
>     }
> 
> -- 
> Christoph M. Becker

For some reason Evertjan code do not swap the arrays at least not the way i try to do it. My original code do really swap the arrays but give the same bezerk answer when you try do arithmetics upon them.

function prepSubt(subOne,subTwo){

//If subOne smaller need to swap arrays
//Function naiveSub need to be fed bigger-smaller, parser keep track sign

if (arrCompare(subOne,subTwo)) {tempArr=subOne;subOne=subTwo;subTwo=tempArr;}

//Check length of biggest array store bigArr
if(subOne.length<subTwo.length) {
          shortArr = subOne.length;   bigArr = subTwo.length;
          } else {bigArr = subOne.length; shortArr = subTwo.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;
   } 
document.calc.outbase.value += subOne+ " - \n";
document.calc.outbase.value += subTwo+ "\n";
}

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


#27459

Fromjonas.thornvall@gmail.com
Date2015-05-01 07:00 -0700
Message-ID<88fff497-3346-4e37-8e12-87a8ad6019dd@googlegroups.com>
In reply to#27458
Den fredag 1 maj 2015 kl. 15:49:46 UTC+2 skrev jonas.t...@gmail.com:
> Den fredag 1 maj 2015 kl. 15:04:34 UTC+2 skrev Christoph M. Becker:
> > Evertjan. wrote:
> > 
> > > 'this' works:
> > 
> > Not necessarily:
> > 
> >     function test() {
> > 
> >       var a = [1], b = [2];
> > 
> >       function arraySwap(x,y) {
> >         var tempArrName = this[y];
> >         this[y] = this[x];
> >         this[x] = tempArrName;
> >       }
> > 
> >       console.log(a, b);
> >       arraySwap(a, b);
> >       console.log(a, b);
> >     }
> > 
> > -- 
> > Christoph M. Becker
> 
> For some reason Evertjan code do not swap the arrays at least not the way i try to do it. My original code do really swap the arrays but give the same bezerk answer when you try do arithmetics upon them.
> 
> function prepSubt(subOne,subTwo){
> 
> //If subOne smaller need to swap arrays
> //Function naiveSub need to be fed bigger-smaller, parser keep track sign
> 
> if (arrCompare(subOne,subTwo)) {tempArr=subOne;subOne=subTwo;subTwo=tempArr;}
> 
> //Check length of biggest array store bigArr
> if(subOne.length<subTwo.length) {
>           shortArr = subOne.length;   bigArr = subTwo.length;
>           } else {bigArr = subOne.length; shortArr = subTwo.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;
>    } 
> document.calc.outbase.value += subOne+ " - \n";
> document.calc.outbase.value += subTwo+ "\n";
> }

This is the operations that do take place using subOne and subTwo in naiveSub, 

13-5=8
10-1=9
12-3=9
12-3=9

As you can see subOne was correctly swapped *into* subTwo.
But i am totally clueless about what going on with the subTwo, because both values print out perectly fine in form.  Just before call to naiveSub.

document.calc.outbase.value += subOne+ " - \n";
document.calc.outbase.value += subTwo+ "\n";

So there must happen something to the format why does the values suddenly double digit, is it appended string values?

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


#27460

Fromjonas.thornvall@gmail.com
Date2015-05-01 07:17 -0700
Message-ID<0a644df5-5072-4b87-b193-cd9971365155@googlegroups.com>
In reply to#27459
Den fredag 1 maj 2015 kl. 16:00:33 UTC+2 skrev jonas.t...@gmail.com:
> Den fredag 1 maj 2015 kl. 15:49:46 UTC+2 skrev jonas.t...@gmail.com:
> > Den fredag 1 maj 2015 kl. 15:04:34 UTC+2 skrev Christoph M. Becker:
> > > Evertjan. wrote:
> > > 
> > > > 'this' works:
> > > 
> > > Not necessarily:
> > > 
> > >     function test() {
> > > 
> > >       var a = [1], b = [2];
> > > 
> > >       function arraySwap(x,y) {
> > >         var tempArrName = this[y];
> > >         this[y] = this[x];
> > >         this[x] = tempArrName;
> > >       }
> > > 
> > >       console.log(a, b);
> > >       arraySwap(a, b);
> > >       console.log(a, b);
> > >     }
> > > 
> > > -- 
> > > Christoph M. Becker
> > 
> > For some reason Evertjan code do not swap the arrays at least not the way i try to do it. My original code do really swap the arrays but give the same bezerk answer when you try do arithmetics upon them.
> > 
> > function prepSubt(subOne,subTwo){
> > 
> > //If subOne smaller need to swap arrays
> > //Function naiveSub need to be fed bigger-smaller, parser keep track sign
> > 
> > if (arrCompare(subOne,subTwo)) {tempArr=subOne;subOne=subTwo;subTwo=tempArr;}
> > 
> > //Check length of biggest array store bigArr
> > if(subOne.length<subTwo.length) {
> >           shortArr = subOne.length;   bigArr = subTwo.length;
> >           } else {bigArr = subOne.length; shortArr = subTwo.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;
> >    } 
> > document.calc.outbase.value += subOne+ " - \n";
> > document.calc.outbase.value += subTwo+ "\n";
> > }
> 
> This is the operations that do take place using subOne and subTwo in naiveSub, 
> 
> 13-5=8
> 10-1=9
> 12-3=9
> 12-3=9
> 
> As you can see subOne was correctly swapped *into* subTwo.
> But i am totally clueless about what going on with the subTwo, because both values print out perectly fine in form.  Just before call to naiveSub.
> 
> document.calc.outbase.value += subOne+ " - \n";
> document.calc.outbase.value += subTwo+ "\n";
> 
> So there must happen something to the format why does the values suddenly double digit, is it appended string values?

i can see i am tired i switched values in Janeverts function, but that was not the problem.

// Compare numerical size of two "reversed"arrays and their indexes
// If A < B returns true otherwise 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;
}

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


#27461

Fromjonas.thornvall@gmail.com
Date2015-05-01 07:21 -0700
Message-ID<e2d1dff7-0112-4819-9c5c-2b9acb34be1b@googlegroups.com>
In reply to#27459
Den fredag 1 maj 2015 kl. 16:00:33 UTC+2 skrev jonas.t...@gmail.com:
> Den fredag 1 maj 2015 kl. 15:49:46 UTC+2 skrev jonas.t...@gmail.com:
> > Den fredag 1 maj 2015 kl. 15:04:34 UTC+2 skrev Christoph M. Becker:
> > > Evertjan. wrote:
> > > 
> > > > 'this' works:
> > > 
> > > Not necessarily:
> > > 
> > >     function test() {
> > > 
> > >       var a = [1], b = [2];
> > > 
> > >       function arraySwap(x,y) {
> > >         var tempArrName = this[y];
> > >         this[y] = this[x];
> > >         this[x] = tempArrName;
> > >       }
> > > 
> > >       console.log(a, b);
> > >       arraySwap(a, b);
> > >       console.log(a, b);
> > >     }
> > > 
> > > -- 
> > > Christoph M. Becker
> > 
> > For some reason Evertjan code do not swap the arrays at least not the way i try to do it. My original code do really swap the arrays but give the same bezerk answer when you try do arithmetics upon them.
> > 
> > function prepSubt(subOne,subTwo){
> > 
> > //If subOne smaller need to swap arrays
> > //Function naiveSub need to be fed bigger-smaller, parser keep track sign
> > 
> > if (arrCompare(subOne,subTwo)) {tempArr=subOne;subOne=subTwo;subTwo=tempArr;}
> > 
> > //Check length of biggest array store bigArr
> > if(subOne.length<subTwo.length) {
> >           shortArr = subOne.length;   bigArr = subTwo.length;
> >           } else {bigArr = subOne.length; shortArr = subTwo.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;
> >    } 
> > document.calc.outbase.value += subOne+ " - \n";
> > document.calc.outbase.value += subTwo+ "\n";
> > }
> 
> This is the operations that do take place using subOne and subTwo in naiveSub, 
> 
> 13-5=8
> 10-1=9
> 12-3=9
> 12-3=9
> 
> As you can see subOne was correctly swapped *into* subTwo.
> But i am totally clueless about what going on with the subTwo, because both values print out perectly fine in form.  Just before call to naiveSub.
> 
> document.calc.outbase.value += subOne+ " - \n";
> document.calc.outbase.value += subTwo+ "\n";
> 
> So there must happen something to the format why does the values suddenly double digit, is it appended string values?

Well it seem i may have to swap each index in a loop. 

<SCRIPT LANGUAGE="Javascript">

// Compare numerical size of two "reversed"arrays and their indexes
// If A < B returns true otherwise 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;
}


//Prepare arrays for subtraction

function prepSubt(subOne,subTwo){

//If subOne smaller need to swap arrays
//Function naiveSub need to be fed bigger-smaller, parser keep track sign
res=arrCompare(subOne,subTwo)
if (res) {tempArr=subOne;subOne=subTwo;subTwo=tempArr;}
document.calc.stats.value+=res+"\n"; 
//Check length of biggest array store bigArr
if(subOne.length<subTwo.length) {
          shortArr = subOne.length;   bigArr = subTwo.length;
          } else {bigArr = subOne.length; shortArr = subTwo.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;
   } 
document.calc.outbase.value += subOne+ " - \n";
document.calc.outbase.value += subTwo+ "\n";
}

/* SUBTRACT TWO VARIABLES */
/* THE NOTATION BASE TEN, SO FOR INDEX VALUES IF BASE 2000, DIGITVALUE IN INDEX RANGE = (0-1999)*/
/* VARIABLES base INTEGER, subOne, subTwo  ARRAYS WITH INDEX VALUES REVERSED */
/* A SINGLE "DIGITPLACE/VALUE" OF BASE, HOLD IN EACH INDEX */

function naiveSub(base, subOne, subTwo)
{
   prepSubt(subOne,subTwo);
   result=[];
   for (i = 0; i < shortArr; i ++ )
   {
      j = 0;
  
      if (subOne[i] < subTwo[i])
      {  
         intCarry ++ ;
         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++;
document.calc.stats.value+=subOne[i]+"-"+subTwo[i]+"="+result[i]+"\n"; 
  }
   while(i<bigArr) { result[i]=subOne[i];i++;}
   while (result[bigArr - 1] == 0)
   {
      result.splice(bigArr - 1, 1);
      bigArr += - 1;
   }
   return result;
}

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

function fetchVal()
{
   document.calc.result.value = "";
   document.calc.stats.value = "";
   document.calc.outbase.value = "";
   strEval = document.calc.eval.value;
   base = document.calc.formbase.value;
   base = parseInt(base);
}

/* SPLIT OPERAND PAIR AND DECIDE OPERATION */

function parsePair()
{
   genArr = strEval.split("-");
   return genArr;
}

/* IF BASE 10 NO CONVERSION NEEDED "ELSE" DO CONVERSION TO NEWBASE */

function ifBaseTen()
{
   if(base != 10)
   {
      arrOne = newbase(genArr[0], base);
      tempA = arrOne;
      arrTwo = newbase(genArr[1], base);
      tempB = arrTwo;
   }
   else
   {
      arrOne = genArr[0].split("").reverse();
      tempA = arrOne;
      arrTwo = genArr[1].split("").reverse();
      tempB = arrTwo;
   }
}

/* RESULT OUT TO FORM */

function resOutForm()
{

   document.calc.stats.value += "INT 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 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 */




// Arrays holding digitplaces
arrOne = [];
arrTwo = [];
subOne = [];
subTwo = [];
tempA = [];
tempB = [];
tempArr = [];

/* MAIN FUNCTION */

function MAIN()
{  
   // Counters, adds, subtraction, multiplications
intMult = 0;
intAdd = 0;
intSub = 0;
intCarry = 0;
// Length of the operands
bigArr = 0;
shortArr = 0;
//  Fetched variables from form base and string to evaluate
base = 0;
strEval = "";
   fetchVal();
   genArr = parsePair();
   ifBaseTen();
   out = naiveSub(base, arrOne, arrTwo);
   resOutForm();
}
</SCRIPT>


<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"/></head>
<body bgcolor="gold" onload="MAIN()">
<H3>ARITHMETICS IN ANYBASE USING BIG NUMBERS</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="10" size="10"><br><BR>
Evaluate <input type="text" name="eval" value="3313-3315" size="300"><br>
Base conv.(rev arrays)<input type="text" name="outbase" value="" size="60"><br>
<FONT COLOR="gray">
RESULT<br>
<textarea name="result" rows="20" cols="120"></textarea><br>
STATUS</B><br>
<textarea name="stats" cols="120" rows="40" value=""></textarea>
</FORM>
</body>
</html>

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


#27462

Fromjonas.thornvall@gmail.com
Date2015-05-01 07:37 -0700
Message-ID<938328a1-92ff-438c-93db-2b2343986712@googlegroups.com>
In reply to#27461
Den fredag 1 maj 2015 kl. 16:21:19 UTC+2 skrev jonas.t...@gmail.com:
> Den fredag 1 maj 2015 kl. 16:00:33 UTC+2 skrev jonas.t...@gmail.com:
> > Den fredag 1 maj 2015 kl. 15:49:46 UTC+2 skrev jonas.t...@gmail.com:
> > > Den fredag 1 maj 2015 kl. 15:04:34 UTC+2 skrev Christoph M. Becker:
> > > > Evertjan. wrote:
> > > > 
> > > > > 'this' works:
> > > > 
> > > > Not necessarily:
> > > > 
> > > >     function test() {
> > > > 
> > > >       var a = [1], b = [2];
> > > > 
> > > >       function arraySwap(x,y) {
> > > >         var tempArrName = this[y];
> > > >         this[y] = this[x];
> > > >         this[x] = tempArrName;
> > > >       }
> > > > 
> > > >       console.log(a, b);
> > > >       arraySwap(a, b);
> > > >       console.log(a, b);
> > > >     }
> > > > 
> > > > -- 
> > > > Christoph M. Becker
> > > 
> > > For some reason Evertjan code do not swap the arrays at least not the way i try to do it. My original code do really swap the arrays but give the same bezerk answer when you try do arithmetics upon them.
> > > 
> > > function prepSubt(subOne,subTwo){
> > > 
> > > //If subOne smaller need to swap arrays
> > > //Function naiveSub need to be fed bigger-smaller, parser keep track sign
> > > 
> > > if (arrCompare(subOne,subTwo)) {tempArr=subOne;subOne=subTwo;subTwo=tempArr;}
> > > 
> > > //Check length of biggest array store bigArr
> > > if(subOne.length<subTwo.length) {
> > >           shortArr = subOne.length;   bigArr = subTwo.length;
> > >           } else {bigArr = subOne.length; shortArr = subTwo.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;
> > >    } 
> > > document.calc.outbase.value += subOne+ " - \n";
> > > document.calc.outbase.value += subTwo+ "\n";
> > > }
> > 
> > This is the operations that do take place using subOne and subTwo in naiveSub, 
> > 
> > 13-5=8
> > 10-1=9
> > 12-3=9
> > 12-3=9
> > 
> > As you can see subOne was correctly swapped *into* subTwo.
> > But i am totally clueless about what going on with the subTwo, because both values print out perectly fine in form.  Just before call to naiveSub.
> > 
> > document.calc.outbase.value += subOne+ " - \n";
> > document.calc.outbase.value += subTwo+ "\n";
> > 
> > So there must happen something to the format why does the values suddenly double digit, is it appended string values?
> 
> Well it seem i may have to swap each index in a loop. 
> 
> <SCRIPT LANGUAGE="Javascript">
> 
> // Compare numerical size of two "reversed"arrays and their indexes
> // If A < B returns true otherwise 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;
> }
> 
> 
> //Prepare arrays for subtraction
> 
> function prepSubt(subOne,subTwo){
> 
> //If subOne smaller need to swap arrays
> //Function naiveSub need to be fed bigger-smaller, parser keep track sign
> res=arrCompare(subOne,subTwo)
> if (res) {tempArr=subOne;subOne=subTwo;subTwo=tempArr;}
> document.calc.stats.value+=res+"\n"; 
> //Check length of biggest array store bigArr
> if(subOne.length<subTwo.length) {
>           shortArr = subOne.length;   bigArr = subTwo.length;
>           } else {bigArr = subOne.length; shortArr = subTwo.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;
>    } 
> document.calc.outbase.value += subOne+ " - \n";
> document.calc.outbase.value += subTwo+ "\n";
> }
> 
> /* SUBTRACT TWO VARIABLES */
> /* THE NOTATION BASE TEN, SO FOR INDEX VALUES IF BASE 2000, DIGITVALUE IN INDEX RANGE = (0-1999)*/
> /* VARIABLES base INTEGER, subOne, subTwo  ARRAYS WITH INDEX VALUES REVERSED */
> /* A SINGLE "DIGITPLACE/VALUE" OF BASE, HOLD IN EACH INDEX */
> 
> function naiveSub(base, subOne, subTwo)
> {
>    prepSubt(subOne,subTwo);
>    result=[];
>    for (i = 0; i < shortArr; i ++ )
>    {
>       j = 0;
>   
>       if (subOne[i] < subTwo[i])
>       {  
>          intCarry ++ ;
>          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++;
> document.calc.stats.value+=subOne[i]+"-"+subTwo[i]+"="+result[i]+"\n"; 
>   }
>    while(i<bigArr) { result[i]=subOne[i];i++;}
>    while (result[bigArr - 1] == 0)
>    {
>       result.splice(bigArr - 1, 1);
>       bigArr += - 1;
>    }
>    return result;
> }
> 
> /* RESET FORM FIELDS, FETCH BASE AND STRING TO EVALUATE */
> 
> function fetchVal()
> {
>    document.calc.result.value = "";
>    document.calc.stats.value = "";
>    document.calc.outbase.value = "";
>    strEval = document.calc.eval.value;
>    base = document.calc.formbase.value;
>    base = parseInt(base);
> }
> 
> /* SPLIT OPERAND PAIR AND DECIDE OPERATION */
> 
> function parsePair()
> {
>    genArr = strEval.split("-");
>    return genArr;
> }
> 
> /* IF BASE 10 NO CONVERSION NEEDED "ELSE" DO CONVERSION TO NEWBASE */
> 
> function ifBaseTen()
> {
>    if(base != 10)
>    {
>       arrOne = newbase(genArr[0], base);
>       tempA = arrOne;
>       arrTwo = newbase(genArr[1], base);
>       tempB = arrTwo;
>    }
>    else
>    {
>       arrOne = genArr[0].split("").reverse();
>       tempA = arrOne;
>       arrTwo = genArr[1].split("").reverse();
>       tempB = arrTwo;
>    }
> }
> 
> /* RESULT OUT TO FORM */
> 
> function resOutForm()
> {
> 
>    document.calc.stats.value += "INT 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 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 */
> 
> 
> 
> 
> // Arrays holding digitplaces
> arrOne = [];
> arrTwo = [];
> subOne = [];
> subTwo = [];
> tempA = [];
> tempB = [];
> tempArr = [];
> 
> /* MAIN FUNCTION */
> 
> function MAIN()
> {  
>    // Counters, adds, subtraction, multiplications
> intMult = 0;
> intAdd = 0;
> intSub = 0;
> intCarry = 0;
> // Length of the operands
> bigArr = 0;
> shortArr = 0;
> //  Fetched variables from form base and string to evaluate
> base = 0;
> strEval = "";
>    fetchVal();
>    genArr = parsePair();
>    ifBaseTen();
>    out = naiveSub(base, arrOne, arrTwo);
>    resOutForm();
> }
> </SCRIPT>
> 
> 
> <!DOCTYPE html>
> <html lang="en">
> <head><meta charset="utf-8"/></head>
> <body bgcolor="gold" onload="MAIN()">
> <H3>ARITHMETICS IN ANYBASE USING BIG NUMBERS</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="10" size="10"><br><BR>
> Evaluate <input type="text" name="eval" value="3313-3315" size="300"><br>
> Base conv.(rev arrays)<input type="text" name="outbase" value="" size="60"><br>
> <FONT COLOR="gray">
> RESULT<br>
> <textarea name="result" rows="20" cols="120"></textarea><br>
> STATUS</B><br>
> <textarea name="stats" cols="120" rows="40" value=""></textarea>
> </FORM>
> </body>
> </html>

I've decided to skip the approach all together it see impossible to know where the values originate. I will instead compare strlen and characters in string already when read from form to decide which one bigger and smaller.

And *after* put into correct array, but i still wonder what is goin on with the arrays. Because content is correct after swap, until you call naiveSub and try do do arithmetics. 

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


#27464

Fromjonas.thornvall@gmail.com
Date2015-05-01 11:27 -0700
Message-ID<e39d1b87-6802-44d1-98ee-cd937049b998@googlegroups.com>
In reply to#27462
Den fredag 1 maj 2015 kl. 16:37:27 UTC+2 skrev jonas.t...@gmail.com:
> Den fredag 1 maj 2015 kl. 16:21:19 UTC+2 skrev jonas.t...@gmail.com:
> > Den fredag 1 maj 2015 kl. 16:00:33 UTC+2 skrev jonas.t...@gmail.com:
> > > Den fredag 1 maj 2015 kl. 15:49:46 UTC+2 skrev jonas.t...@gmail.com:
> > > > Den fredag 1 maj 2015 kl. 15:04:34 UTC+2 skrev Christoph M. Becker:
> > > > > Evertjan. wrote:
> > > > > 
> > > > > > 'this' works:
> > > > > 
> > > > > Not necessarily:
> > > > > 
> > > > >     function test() {
> > > > > 
> > > > >       var a = [1], b = [2];
> > > > > 
> > > > >       function arraySwap(x,y) {
> > > > >         var tempArrName = this[y];
> > > > >         this[y] = this[x];
> > > > >         this[x] = tempArrName;
> > > > >       }
> > > > > 
> > > > >       console.log(a, b);
> > > > >       arraySwap(a, b);
> > > > >       console.log(a, b);
> > > > >     }
> > > > > 
> > > > > -- 
> > > > > Christoph M. Becker
> > > > 
> > > > For some reason Evertjan code do not swap the arrays at least not the way i try to do it. My original code do really swap the arrays but give the same bezerk answer when you try do arithmetics upon them.
> > > > 
> > > > function prepSubt(subOne,subTwo){
> > > > 
> > > > //If subOne smaller need to swap arrays
> > > > //Function naiveSub need to be fed bigger-smaller, parser keep track sign
> > > > 
> > > > if (arrCompare(subOne,subTwo)) {tempArr=subOne;subOne=subTwo;subTwo=tempArr;}
> > > > 
> > > > //Check length of biggest array store bigArr
> > > > if(subOne.length<subTwo.length) {
> > > >           shortArr = subOne.length;   bigArr = subTwo.length;
> > > >           } else {bigArr = subOne.length; shortArr = subTwo.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;
> > > >    } 
> > > > document.calc.outbase.value += subOne+ " - \n";
> > > > document.calc.outbase.value += subTwo+ "\n";
> > > > }
> > > 
> > > This is the operations that do take place using subOne and subTwo in naiveSub, 
> > > 
> > > 13-5=8
> > > 10-1=9
> > > 12-3=9
> > > 12-3=9
> > > 
> > > As you can see subOne was correctly swapped *into* subTwo.
> > > But i am totally clueless about what going on with the subTwo, because both values print out perectly fine in form.  Just before call to naiveSub.
> > > 
> > > document.calc.outbase.value += subOne+ " - \n";
> > > document.calc.outbase.value += subTwo+ "\n";
> > > 
> > > So there must happen something to the format why does the values suddenly double digit, is it appended string values?
> > 
> > Well it seem i may have to swap each index in a loop. 
> > 
> > <SCRIPT LANGUAGE="Javascript">
> > 
> > // Compare numerical size of two "reversed"arrays and their indexes
> > // If A < B returns true otherwise 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;
> > }
> > 
> > 
> > //Prepare arrays for subtraction
> > 
> > function prepSubt(subOne,subTwo){
> > 
> > //If subOne smaller need to swap arrays
> > //Function naiveSub need to be fed bigger-smaller, parser keep track sign
> > res=arrCompare(subOne,subTwo)
> > if (res) {tempArr=subOne;subOne=subTwo;subTwo=tempArr;}
> > document.calc.stats.value+=res+"\n"; 
> > //Check length of biggest array store bigArr
> > if(subOne.length<subTwo.length) {
> >           shortArr = subOne.length;   bigArr = subTwo.length;
> >           } else {bigArr = subOne.length; shortArr = subTwo.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;
> >    } 
> > document.calc.outbase.value += subOne+ " - \n";
> > document.calc.outbase.value += subTwo+ "\n";
> > }
> > 
> > /* SUBTRACT TWO VARIABLES */
> > /* THE NOTATION BASE TEN, SO FOR INDEX VALUES IF BASE 2000, DIGITVALUE IN INDEX RANGE = (0-1999)*/
> > /* VARIABLES base INTEGER, subOne, subTwo  ARRAYS WITH INDEX VALUES REVERSED */
> > /* A SINGLE "DIGITPLACE/VALUE" OF BASE, HOLD IN EACH INDEX */
> > 
> > function naiveSub(base, subOne, subTwo)
> > {
> >    prepSubt(subOne,subTwo);
> >    result=[];
> >    for (i = 0; i < shortArr; i ++ )
> >    {
> >       j = 0;
> >   
> >       if (subOne[i] < subTwo[i])
> >       {  
> >          intCarry ++ ;
> >          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++;
> > document.calc.stats.value+=subOne[i]+"-"+subTwo[i]+"="+result[i]+"\n"; 
> >   }
> >    while(i<bigArr) { result[i]=subOne[i];i++;}
> >    while (result[bigArr - 1] == 0)
> >    {
> >       result.splice(bigArr - 1, 1);
> >       bigArr += - 1;
> >    }
> >    return result;
> > }
> > 
> > /* RESET FORM FIELDS, FETCH BASE AND STRING TO EVALUATE */
> > 
> > function fetchVal()
> > {
> >    document.calc.result.value = "";
> >    document.calc.stats.value = "";
> >    document.calc.outbase.value = "";
> >    strEval = document.calc.eval.value;
> >    base = document.calc.formbase.value;
> >    base = parseInt(base);
> > }
> > 
> > /* SPLIT OPERAND PAIR AND DECIDE OPERATION */
> > 
> > function parsePair()
> > {
> >    genArr = strEval.split("-");
> >    return genArr;
> > }
> > 
> > /* IF BASE 10 NO CONVERSION NEEDED "ELSE" DO CONVERSION TO NEWBASE */
> > 
> > function ifBaseTen()
> > {
> >    if(base != 10)
> >    {
> >       arrOne = newbase(genArr[0], base);
> >       tempA = arrOne;
> >       arrTwo = newbase(genArr[1], base);
> >       tempB = arrTwo;
> >    }
> >    else
> >    {
> >       arrOne = genArr[0].split("").reverse();
> >       tempA = arrOne;
> >       arrTwo = genArr[1].split("").reverse();
> >       tempB = arrTwo;
> >    }
> > }
> > 
> > /* RESULT OUT TO FORM */
> > 
> > function resOutForm()
> > {
> > 
> >    document.calc.stats.value += "INT 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 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 */
> > 
> > 
> > 
> > 
> > // Arrays holding digitplaces
> > arrOne = [];
> > arrTwo = [];
> > subOne = [];
> > subTwo = [];
> > tempA = [];
> > tempB = [];
> > tempArr = [];
> > 
> > /* MAIN FUNCTION */
> > 
> > function MAIN()
> > {  
> >    // Counters, adds, subtraction, multiplications
> > intMult = 0;
> > intAdd = 0;
> > intSub = 0;
> > intCarry = 0;
> > // Length of the operands
> > bigArr = 0;
> > shortArr = 0;
> > //  Fetched variables from form base and string to evaluate
> > base = 0;
> > strEval = "";
> >    fetchVal();
> >    genArr = parsePair();
> >    ifBaseTen();
> >    out = naiveSub(base, arrOne, arrTwo);
> >    resOutForm();
> > }
> > </SCRIPT>
> > 
> > 
> > <!DOCTYPE html>
> > <html lang="en">
> > <head><meta charset="utf-8"/></head>
> > <body bgcolor="gold" onload="MAIN()">
> > <H3>ARITHMETICS IN ANYBASE USING BIG NUMBERS</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="10" size="10"><br><BR>
> > Evaluate <input type="text" name="eval" value="3313-3315" size="300"><br>
> > Base conv.(rev arrays)<input type="text" name="outbase" value="" size="60"><br>
> > <FONT COLOR="gray">
> > RESULT<br>
> > <textarea name="result" rows="20" cols="120"></textarea><br>
> > STATUS</B><br>
> > <textarea name="stats" cols="120" rows="40" value=""></textarea>
> > </FORM>
> > </body>
> > </html>
> 
> I've decided to skip the approach all together it see impossible to know where the values originate. I will instead compare strlen and characters in string already when read from form to decide which one bigger and smaller.
> 
> And *after* put into correct array, but i still wonder what is goin on with the arrays. Because content is correct after swap, until you call naiveSub and try do do arithmetics.

A hint someone? I read that arr.slice() make a copy so i tried this. But it simply do not work.

if (res) {var tempArr=subOne.slice();subOne=[];subOne=subTwo.slice();subTwo=[];subTwo=tempArr.slice();}

How can 2000-1999 IN Arrays 0,0,0,2 and 9,9,9,1 giva another result then 1999-2000 swao, IN Arrays 0,0,0,2 and 9,9,9,1 after calling naiveSub with them as parameters?

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


#27465

Fromjonas.thornvall@gmail.com
Date2015-05-01 12:34 -0700
Message-ID<14e0a231-8ac5-412c-b86e-e1384872a8ce@googlegroups.com>
In reply to#27464
Den fredag 1 maj 2015 kl. 20:27:49 UTC+2 skrev jonas.t...@gmail.com:
> Den fredag 1 maj 2015 kl. 16:37:27 UTC+2 skrev jonas.t...@gmail.com:
> > Den fredag 1 maj 2015 kl. 16:21:19 UTC+2 skrev jonas.t...@gmail.com:
> > > Den fredag 1 maj 2015 kl. 16:00:33 UTC+2 skrev jonas.t...@gmail.com:
> > > > Den fredag 1 maj 2015 kl. 15:49:46 UTC+2 skrev jonas.t...@gmail.com:
> > > > > Den fredag 1 maj 2015 kl. 15:04:34 UTC+2 skrev Christoph M. Becker:
> > > > > > Evertjan. wrote:
> > > > > > 
> > > > > > > 'this' works:
> > > > > > 
> > > > > > Not necessarily:
> > > > > > 
> > > > > >     function test() {
> > > > > > 
> > > > > >       var a = [1], b = [2];
> > > > > > 
> > > > > >       function arraySwap(x,y) {
> > > > > >         var tempArrName = this[y];
> > > > > >         this[y] = this[x];
> > > > > >         this[x] = tempArrName;
> > > > > >       }
> > > > > > 
> > > > > >       console.log(a, b);
> > > > > >       arraySwap(a, b);
> > > > > >       console.log(a, b);
> > > > > >     }
> > > > > > 
> > > > > > -- 
> > > > > > Christoph M. Becker
> > > > > 
> > > > > For some reason Evertjan code do not swap the arrays at least not the way i try to do it. My original code do really swap the arrays but give the same bezerk answer when you try do arithmetics upon them.
> > > > > 
> > > > > function prepSubt(subOne,subTwo){
> > > > > 
> > > > > //If subOne smaller need to swap arrays
> > > > > //Function naiveSub need to be fed bigger-smaller, parser keep track sign
> > > > > 
> > > > > if (arrCompare(subOne,subTwo)) {tempArr=subOne;subOne=subTwo;subTwo=tempArr;}
> > > > > 
> > > > > //Check length of biggest array store bigArr
> > > > > if(subOne.length<subTwo.length) {
> > > > >           shortArr = subOne.length;   bigArr = subTwo.length;
> > > > >           } else {bigArr = subOne.length; shortArr = subTwo.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;
> > > > >    } 
> > > > > document.calc.outbase.value += subOne+ " - \n";
> > > > > document.calc.outbase.value += subTwo+ "\n";
> > > > > }
> > > > 
> > > > This is the operations that do take place using subOne and subTwo in naiveSub, 
> > > > 
> > > > 13-5=8
> > > > 10-1=9
> > > > 12-3=9
> > > > 12-3=9
> > > > 
> > > > As you can see subOne was correctly swapped *into* subTwo.
> > > > But i am totally clueless about what going on with the subTwo, because both values print out perectly fine in form.  Just before call to naiveSub.
> > > > 
> > > > document.calc.outbase.value += subOne+ " - \n";
> > > > document.calc.outbase.value += subTwo+ "\n";
> > > > 
> > > > So there must happen something to the format why does the values suddenly double digit, is it appended string values?
> > > 
> > > Well it seem i may have to swap each index in a loop. 
> > > 
> > > <SCRIPT LANGUAGE="Javascript">
> > > 
> > > // Compare numerical size of two "reversed"arrays and their indexes
> > > // If A < B returns true otherwise 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;
> > > }
> > > 
> > > 
> > > //Prepare arrays for subtraction
> > > 
> > > function prepSubt(subOne,subTwo){
> > > 
> > > //If subOne smaller need to swap arrays
> > > //Function naiveSub need to be fed bigger-smaller, parser keep track sign
> > > res=arrCompare(subOne,subTwo)
> > > if (res) {tempArr=subOne;subOne=subTwo;subTwo=tempArr;}
> > > document.calc.stats.value+=res+"\n"; 
> > > //Check length of biggest array store bigArr
> > > if(subOne.length<subTwo.length) {
> > >           shortArr = subOne.length;   bigArr = subTwo.length;
> > >           } else {bigArr = subOne.length; shortArr = subTwo.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;
> > >    } 
> > > document.calc.outbase.value += subOne+ " - \n";
> > > document.calc.outbase.value += subTwo+ "\n";
> > > }
> > > 
> > > /* SUBTRACT TWO VARIABLES */
> > > /* THE NOTATION BASE TEN, SO FOR INDEX VALUES IF BASE 2000, DIGITVALUE IN INDEX RANGE = (0-1999)*/
> > > /* VARIABLES base INTEGER, subOne, subTwo  ARRAYS WITH INDEX VALUES REVERSED */
> > > /* A SINGLE "DIGITPLACE/VALUE" OF BASE, HOLD IN EACH INDEX */
> > > 
> > > function naiveSub(base, subOne, subTwo)
> > > {
> > >    prepSubt(subOne,subTwo);
> > >    result=[];
> > >    for (i = 0; i < shortArr; i ++ )
> > >    {
> > >       j = 0;
> > >   
> > >       if (subOne[i] < subTwo[i])
> > >       {  
> > >          intCarry ++ ;
> > >          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++;
> > > document.calc.stats.value+=subOne[i]+"-"+subTwo[i]+"="+result[i]+"\n"; 
> > >   }
> > >    while(i<bigArr) { result[i]=subOne[i];i++;}
> > >    while (result[bigArr - 1] == 0)
> > >    {
> > >       result.splice(bigArr - 1, 1);
> > >       bigArr += - 1;
> > >    }
> > >    return result;
> > > }
> > > 
> > > /* RESET FORM FIELDS, FETCH BASE AND STRING TO EVALUATE */
> > > 
> > > function fetchVal()
> > > {
> > >    document.calc.result.value = "";
> > >    document.calc.stats.value = "";
> > >    document.calc.outbase.value = "";
> > >    strEval = document.calc.eval.value;
> > >    base = document.calc.formbase.value;
> > >    base = parseInt(base);
> > > }
> > > 
> > > /* SPLIT OPERAND PAIR AND DECIDE OPERATION */
> > > 
> > > function parsePair()
> > > {
> > >    genArr = strEval.split("-");
> > >    return genArr;
> > > }
> > > 
> > > /* IF BASE 10 NO CONVERSION NEEDED "ELSE" DO CONVERSION TO NEWBASE */
> > > 
> > > function ifBaseTen()
> > > {
> > >    if(base != 10)
> > >    {
> > >       arrOne = newbase(genArr[0], base);
> > >       tempA = arrOne;
> > >       arrTwo = newbase(genArr[1], base);
> > >       tempB = arrTwo;
> > >    }
> > >    else
> > >    {
> > >       arrOne = genArr[0].split("").reverse();
> > >       tempA = arrOne;
> > >       arrTwo = genArr[1].split("").reverse();
> > >       tempB = arrTwo;
> > >    }
> > > }
> > > 
> > > /* RESULT OUT TO FORM */
> > > 
> > > function resOutForm()
> > > {
> > > 
> > >    document.calc.stats.value += "INT 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 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 */
> > > 
> > > 
> > > 
> > > 
> > > // Arrays holding digitplaces
> > > arrOne = [];
> > > arrTwo = [];
> > > subOne = [];
> > > subTwo = [];
> > > tempA = [];
> > > tempB = [];
> > > tempArr = [];
> > > 
> > > /* MAIN FUNCTION */
> > > 
> > > function MAIN()
> > > {  
> > >    // Counters, adds, subtraction, multiplications
> > > intMult = 0;
> > > intAdd = 0;
> > > intSub = 0;
> > > intCarry = 0;
> > > // Length of the operands
> > > bigArr = 0;
> > > shortArr = 0;
> > > //  Fetched variables from form base and string to evaluate
> > > base = 0;
> > > strEval = "";
> > >    fetchVal();
> > >    genArr = parsePair();
> > >    ifBaseTen();
> > >    out = naiveSub(base, arrOne, arrTwo);
> > >    resOutForm();
> > > }
> > > </SCRIPT>
> > > 
> > > 
> > > <!DOCTYPE html>
> > > <html lang="en">
> > > <head><meta charset="utf-8"/></head>
> > > <body bgcolor="gold" onload="MAIN()">
> > > <H3>ARITHMETICS IN ANYBASE USING BIG NUMBERS</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="10" size="10"><br><BR>
> > > Evaluate <input type="text" name="eval" value="3313-3315" size="300"><br>
> > > Base conv.(rev arrays)<input type="text" name="outbase" value="" size="60"><br>
> > > <FONT COLOR="gray">
> > > RESULT<br>
> > > <textarea name="result" rows="20" cols="120"></textarea><br>
> > > STATUS</B><br>
> > > <textarea name="stats" cols="120" rows="40" value=""></textarea>
> > > </FORM>
> > > </body>
> > > </html>
> > 
> > I've decided to skip the approach all together it see impossible to know where the values originate. I will instead compare strlen and characters in string already when read from form to decide which one bigger and smaller.
> > 
> > And *after* put into correct array, but i still wonder what is goin on with the arrays. Because content is correct after swap, until you call naiveSub and try do do arithmetics.
> 
> A hint someone? I read that arr.slice() make a copy so i tried this. But it simply do not work.
> 
> if (res) {var tempArr=subOne.slice();subOne=[];subOne=subTwo.slice();subTwo=[];subTwo=tempArr.slice();}
> 
> How can 2000-1999 IN Arrays 0,0,0,2 and 9,9,9,1 giva another result then 1999-2000 swao, IN Arrays 0,0,0,2 and 9,9,9,1 after calling naiveSub with them as parameters?

I admit i have no idea how javascript arrays their references and objects work. But i now do the swap on the originals and it works.

<SCRIPT LANGUAGE="Javascript">

// Compare numerical size of two "reversed"arrays and their indexes
// If A < B returns true otherwise 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;
}


// Prepare arrays for subtraction

function prepSubt(subOne, subTwo)
{

   // If subOne smaller need to swap arrays
   // Function naiveSub need to be fed bigger - smaller, parser keep track sign

  
   // Check length of biggest array store bigArr
   if(subOne.length < subTwo.length)
   {
      shortArr = subOne.length;
      bigArr = subTwo.length;
   }
   else
   {
      bigArr = subOne.length;
      shortArr = subTwo.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;
   }
   document.calc.outbase.value += subOne + " - \n";
   document.calc.outbase.value += subTwo + "\n";
}

/* SUBTRACT TWO VARIABLES */
/* THE NOTATION BASE TEN, SO FOR INDEX VALUES IF BASE 2000, DIGITVALUE IN INDEX RANGE = (0 - 1999) */
/* VARIABLES base INTEGER, subOne, subTwo  ARRAYS WITH INDEX VALUES REVERSED */
/* A SINGLE "DIGITPLACE/VALUE" OF BASE, HOLD IN EACH INDEX */

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

      if (subOne[i] < subTwo[i])
      {
         document.calc.stats.value += "Digit " + i + " subOne less than subTwo digit\n";
         intBorrow ++ ;
         subOne[i] += base;
         j = i;
         while (subOne[j + 1] == 0)
         {
            subOne[j + 1] += base - 1;
            document.calc.stats.value += "Borrow 1 from digit=" + (j + 1) + " Remainder=" + subOne[j + 1] + "\n";
            j ++ ;

         }
         subOne[j + 1] += - 1;
         document.calc.stats.value += "Borrow 1 from digit=" + (j + 1) + " Remainder=" + subOne[j + 1] + "\n";
      }

      j = 0;
      result[i] = subOne[i] - subTwo[i];
      intSub ++ ;
      document.calc.stats.value += "Subtraction digit=" + i + " " + subOne[i] + "-" + subTwo[i] + "=" + result[i] + "\n";
   }
   while(i < bigArr)
   {
      result[i] = subOne[i];
      i ++ ;
   }
   while (result[bigArr - 1] == 0)
   {
      result.splice(bigArr - 1, 1);
      bigArr += - 1;
   }
   return result;
}

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

function fetchVal()
{
   document.calc.result.value = "";
   document.calc.stats.value = "";
   document.calc.outbase.value = "";
   strEval = document.calc.eval.value;
   base = document.calc.formbase.value;
   base = parseInt(base);
}

/* SPLIT OPERAND PAIR AND DECIDE OPERATION */

function parsePair()
{
   genArr = strEval.split("-");
   return genArr;
}

/* IF BASE 10 NO CONVERSION NEEDED "ELSE" DO CONVERSION TO NEWBASE */

function ifBaseTen()
{
   if(base != 10)
   {
      arrOne = newbase(genArr[0], base);
      tempA = arrOne;
      arrTwo = newbase(genArr[1], base);
      tempB = arrTwo;
   }
   else
   {
      arrOne = genArr[0].split("").reverse();
      tempA = arrOne;
      arrTwo = genArr[1].split("").reverse();
      tempB = arrTwo;
   }
   res = arrCompare(arrOne, arrTwo)
   if (res)
   {
      document.calc.stats.value += "Swap! First term smaller=" + res + "\n";
      var tempArr = arrOne.slice();
      arrOne = [];
      arrOne = arrTwo.slice();
      arrTwo = [];
      arrTwo = tempArr.slice();
   }
}

/* RESULT OUT TO FORM */

function resOutForm()
{

   document.calc.stats.value += "INT 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 Carry operations = " + intBorrow + "\n";
   tempC = out.reverse();
   if(base < 11)
   {
      tempC = tempC.toString();
      tempC = tempC.replace(/,/g, '')
   }
   document.calc.result.value += " = " + tempC + "\n";
}


/* GLOBAL VARIABLES */

// Arrays holding digitplaces
arrOne = [];
arrTwo = [];
tempA = [];
tempB = [];


/* MAIN FUNCTION */

function MAIN()
{
   // Counters, adds, subtraction, multiplications
   intMult = 0;
   intAdd = 0;
   intSub = 0;
   intBorrow = 0;
   // Length of the operands
   bigArr = 0;
   shortArr = 0;
   //  Fetched variables from form base and string to evaluate
   base = 0;
   strEval = "";
   fetchVal();
   genArr = parsePair();
   ifBaseTen();
   out = naiveSub(base, arrOne, arrTwo);
   resOutForm();
}
</SCRIPT>

<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"/></head>
<body bgcolor="gold" onload="MAIN()">
<H3>ARITHMETICS IN ANYBASE USING BIG NUMBERS</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="10" size="10"><br><BR>
Evaluate <input type="text" name="eval" value="2000-1999" size="300"><br>
Base conv.(rev arrays)<input type="text" name="outbase" value="" size="60"><br>
<FONT COLOR="gray">
RESULT<br>
<textarea name="result" rows="20" cols="120"></textarea><br>
STATUS</B><br>
<textarea name="stats" cols="120" rows="40" value=""></textarea>
</FORM>
</body>
</html>

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


#27466

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-05-01 22:42 +0200
Message-ID<1997740.h7VUlrh2Em@PointedEars.de>
In reply to#27455
Christoph M. Becker wrote:

> Evertjan. wrote:
>> 'this' works:
> 
> Not necessarily:
> 
>     function test() {
> 
>       var a = [1], b = [2];
> 
>       function arraySwap(x,y) {
>         var tempArrName = this[y];
>         this[y] = this[x];
>         this[x] = tempArrName;
>       }
> 
>       console.log(a, b);
>       arraySwap(a, b);
>       console.log(a, b);
>     }

Also, “this” is undefined in arraySwap() if that function is called in 
strict mode.

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

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


#27467

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-05-01 23:37 +0200
Message-ID<mi0rm4$d4a$1@solani.org>
In reply to#27466
Thomas 'PointedEars' Lahn wrote:

> Christoph M. Becker wrote:
> 
>>       function arraySwap(x,y) {
>>         var tempArrName = this[y];
>>         this[y] = this[x];
>>         this[x] = tempArrName;
>>       }
> 
> Also, “this” is undefined in arraySwap() if that function is called in 
> strict mode.

Yes, indeed.  strict mode prevents such dangerous constructs where the
global object would be (inadvertently?) modified.

It seems to me that a generally correct arraySwap() could be written by
interchanging the array *elemtents*, but I'd rather avoid such a
function at all.

-- 
Christoph M. Becker

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


#27468

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-05-02 00:19 +0200
Message-ID<XnsA48E35CB52B7eejj99@194.109.133.246>
In reply to#27467
"Christoph M. Becker" <cmbecker69@arcor.de> wrote on 01 mei 2015 in
comp.lang.javascript: 

> Thomas 'PointedEars' Lahn wrote:
> 
>> Christoph M. Becker wrote:
>> 
>>>       function arraySwap(x,y) {
>>>         var tempArrName = this[y];
>>>         this[y] = this[x];
>>>         this[x] = tempArrName;
>>>       }
>> 
>> Also, ƒ othisƒ   is undefined in arraySwap() if that function is called
>> in strict mode.
> 
> Yes, indeed.  strict mode prevents such dangerous constructs where the
> global object would be (inadvertently?) modified.
> 
> It seems to me that a generally correct arraySwap() could be written by
> interchanging the array *elemtents*, but I'd rather avoid such a
> function at all.

I would prefer not to copy large arrays 3 times for each swap,
if I where the OP, quod non.

Perhaps the OP could use indirect pointers to the arrays?

what about window[x] ??

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

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


#27469

Fromjonas.thornvall@gmail.com
Date2015-05-02 05:50 -0700
Message-ID<72e44d34-7f4d-4d32-8304-e73688132274@googlegroups.com>
In reply to#27468
Den lördag 2 maj 2015 kl. 00:19:54 UTC+2 skrev Evertjan.:
> "Christoph M. Becker" <cmbecker69@arcor.de> wrote on 01 mei 2015 in
> comp.lang.javascript: 
> 
> > Thomas 'PointedEars' Lahn wrote:
> > 
> >> Christoph M. Becker wrote:
> >> 
> >>>       function arraySwap(x,y) {
> >>>         var tempArrName = this[y];
> >>>         this[y] = this[x];
> >>>         this[x] = tempArrName;
> >>>       }
> >> 
> >> Also, � othis�   is undefined in arraySwap() if that function is called
> >> in strict mode.
> > 
> > Yes, indeed.  strict mode prevents such dangerous constructs where the
> > global object would be (inadvertently?) modified.
> > 
> > It seems to me that a generally correct arraySwap() could be written by
> > interchanging the array *elemtents*, but I'd rather avoid such a
> > function at all.
> 
> I would prefer not to copy large arrays 3 times for each swap,
> if I where the OP, quod non.
> 
> Perhaps the OP could use indirect pointers to the arrays?
> 
> what about window[x] ??
> 
> -- 
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

This works called from main and will do for now.

function checkArrayOrder()
{
   res = arrCompare(arrOne, arrTwo)
   if (res)
   {
      document.calc.stats.value += "Swap! First term smaller=" + res + "\n";
      var tempArr = arrOne.slice();
      arrOne = [];
      arrOne = arrTwo.slice();
      arrTwo = [];
      arrTwo = tempArr.slice();
   }
   
}

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


#27470

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-05-02 14:21 +0100
Message-ID<87383f5d4u.fsf@bsb.me.uk>
In reply to#27469
jonas.thornvall@gmail.com writes:
<snip>
> This works called from main and will do for now.
>
> function checkArrayOrder()
> {
>    res = arrCompare(arrOne, arrTwo)
>    if (res)
>    {
>       document.calc.stats.value += "Swap! First term smaller=" + res + "\n";
>       var tempArr = arrOne.slice();
>       arrOne = [];
>       arrOne = arrTwo.slice();
>       arrTwo = [];
>       arrTwo = tempArr.slice();
>    }
> }

Please don't do that.  Write the key function (it's subtract, I think)
so that it expect the arrays to be in the right order.  Then call it
with them in the right order from a wrapper function:

  function subtractLesserFromGreater(lesser, greater) { ... }

  function subtract(a, b)
  {
        return arrLessThan(a, b) ? subtractLesserFromGreater(a, b)
                                 : subtractLesserFromGreater(b, a);
  }

I've changed the name of arrCompare since arrCompare does not give
enough information.

-- 
Ben.

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


#27473

Fromjonas.thornvall@gmail.com
Date2015-05-03 04:30 -0700
Message-ID<3770518e-e91f-4368-9a69-cbd2f2461343@googlegroups.com>
In reply to#27469
Den lördag 2 maj 2015 kl. 14:50:40 UTC+2 skrev jonas.t...@gmail.com:
> Den lördag 2 maj 2015 kl. 00:19:54 UTC+2 skrev Evertjan.:
> > "Christoph M. Becker" <cmbecker69@arcor.de> wrote on 01 mei 2015 in
> > comp.lang.javascript: 
> > 
> > > Thomas 'PointedEars' Lahn wrote:
> > > 
> > >> Christoph M. Becker wrote:
> > >> 
> > >>>       function arraySwap(x,y) {
> > >>>         var tempArrName = this[y];
> > >>>         this[y] = this[x];
> > >>>         this[x] = tempArrName;
> > >>>       }
> > >> 
> > >> Also, � othis�   is undefined in arraySwap() if that function is called
> > >> in strict mode.
> > > 
> > > Yes, indeed.  strict mode prevents such dangerous constructs where the
> > > global object would be (inadvertently?) modified.
> > > 
> > > It seems to me that a generally correct arraySwap() could be written by
> > > interchanging the array *elemtents*, but I'd rather avoid such a
> > > function at all.
> > 
> > I would prefer not to copy large arrays 3 times for each swap,
> > if I where the OP, quod non.
> > 
> > Perhaps the OP could use indirect pointers to the arrays?
> > 
> > what about window[x] ??
> > 
> > -- 
> > Evertjan.
> > The Netherlands.
> > (Please change the x'es to dots in my emailaddress)
> 
> This works called from main and will do for now.
> 
> function checkArrayOrder()
> {
>    res = arrCompare(arrOne, arrTwo)
>    if (res)
>    {
>       document.calc.stats.value += "Swap! First term smaller=" + res + "\n";
>       var tempArr = arrOne.slice();
>       arrOne = [];
>       arrOne = arrTwo.slice();
>       arrTwo = [];
>       arrTwo = tempArr.slice();
>    }
>    
> }

Still only unsigned operands and no parser, so only feed pair +,-,*, but better structure to the script.

(At some Point the operands will be signed, and use factorial/decimal digit places within base)

But now i will implement baseconversion upon the arrays and then the arithmetic should work automaticly using that base. 

After baseconversion done. Add, subtract should hold in bases upto integer/2 and multiplication in upto base SQRT integer.

One or two Days more.

<SCRIPT LANGUAGE="Javascript">

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

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

function naiveMult(base, multOne, multTwo) 
{
   var total = []; 
   var addResult = []; 
   total[0] = 0; 

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

      if(i > 0) 
      { 
         for(zero = 0; zero < i; zero ++ ) multresult[zero] = 0; 
      } 
     
      while(j < bigArr) 
      { 
         intMult++; 
         tempVal = (multOne[j] * multTwo[i]) + remainder; 
         // document.calc.stats.value += tempVal +"=" + multOne[j] +"*" + multTwo[i]+"+"+remainder+"\n";
         remainder = 0; 

         if (tempVal >= base) 
         { 
            tempDig = tempVal % base; 
            
            remainder = (tempVal - tempDig) / base; 
           
            multresult[j + i] = tempDig; 
         //    document.calc.stats.value += "rm= "+1+" index "+(j+i)+" digit = "+tempDig+"\n";
         } 
         else 
         { 
         //   document.calc.stats.value += "no rm index "+(j+i)+" digit = "+tempVal+"\n";
            multresult[j + i] = tempVal; 
         } 
         j ++ ; 
      } 

      if(remainder != 0) 
      { 
         //Remainder after last digit multiplication 
         multresult[j + i] = remainder; 
      } 
      //      document.calc.stats.value += total+" + "+multresult+"\n";
      smallAddArr=multresult.length;
      bigAddArr=total.length;
      addResult=naiveAdd(base, multresult, total,smallAddArr,bigAddArr); 
      //     document.calc.stats.value += "addResult = "+addResult+"\n";
      total=addResult; 
   } 
   return total; 
} 



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

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

   for (i = 0; i < shortloop; 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 < longloop)
   {
      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;
}

/* FIX THIS LATER */

function prepSubt(subOne, subTwo)
{

   // 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])
      {
//  document.calc.stats.value += "Digit " + i + " subOne less than subTwo digit\n";
         intBorrow ++ ;
         subOne[i] += base;
         j = i;
         while (subOne[j + 1] == 0)
         {
            subOne[j + 1] += base - 1;
//   document.calc.stats.value += "Borrow 1 from digit=" + (j + 1) + " Remainder=" + subOne[j + 1] + "\n";
            j ++ ;

         }
         subOne[j + 1] += - 1;
//         document.calc.stats.value += "Borrow 1 from digit=" + (j + 1) + " Remainder=" + subOne[j + 1] + "\n";
      }

      j = 0;
      result[i] = subOne[i] - subTwo[i];
      intSub ++ ;
//  document.calc.stats.value += "Subtraction digit=" + i + " " + subOne[i] + "-" + subTwo[i] + "=" + result[i] + "\n";
   }
   while(i < bigArr)
   {
      result[i] = subOne[i];
      i ++ ;
   }
   while (result[bigArr - 1] == 0)
   {
      result.splice(bigArr - 1, 1);
      bigArr += - 1;
   }
   return result;
}

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

function fetchVal()
{
   document.calc.result.value = "";
   document.calc.stats.value = "";
   document.calc.outbase.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()
{
   document.calc.outbase.value +=" arrOne = "+ arrOne;
   document.calc.outbase.value +=" arrTwo = "+ arrTwo;
   if (operation == "-")
   {
      document.calc.stats.value += "[OPERATION SUBTRACT] - \n";
      out = naiveSub(base, arrOne, arrTwo);
   }
   else if (operation == "+")
   {
      document.calc.stats.value += "[OPERATION ADD] + \n";
      out = naiveAdd(base, arrOne, arrTwo,smallArr,bigArr);
   }
   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 = newbase(genArr[0], base);
   tempA = arrOne;
   arrTwo = newbase(genArr[1], base);
   tempB = arrTwo;
}

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

/*  IF (arrOne < arrTwo) SWAP THE ARRAYS SO THE BIGGER TERM STORED IN WITHIN (arrOne) */

function checkArrayOrder()
{
   res = arrCompare(arrOne, arrTwo)
   if (res)
   {
      document.calc.stats.value += "Swap arrays! First array operand smaller=" + res + "\n";
      var tempArr = arrOne.slice();
      arrOne = [];
      arrOne = arrTwo.slice();
      arrTwo = [];
      arrTwo = tempArr.slice();
   }
   

}

/* 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()
{
   /* TIMER START */
   var start = new Date().getTime();  //Start time the opeation
   /* 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;
   //Reset variables from form 
   base = 0;
   strEval = "";
   fetchVal();                  // Get stringsvalues from form
   /* INIT END */
   
   /* MAIN FUNCTION */
   genArr = checkOperation();   // 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

   }
  
   checkArrayOrder();           // Arrange order of arrays, if arrOne < arrTwo swap the arrays
   bigArr = arrOne.length;     // Get array lengths 
   smallArr = arrTwo.length;            
   performOperation();         // Do the arithmetic operation stored in sign, upon operand pair
   resOutForm();               // Printout result
   var end = new Date().getTime(); //End time the operation
   var time = end - start;
   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>ARITHMETICS IN ANYBASE USING BIG NUMBERS</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="10" size="10"><br><BR>
Evaluate <input type="text" name="eval" value="21*1151" size="300"><br>
New base content arrays.(rev)<input type="text" name="outbase" value="" size="60"><br>
<FONT COLOR="gray">
RESULT<br>
<textarea name="result" rows="20" cols="120"></textarea><br>
STATUS</B><br>
<textarea name="stats" cols="120" rows="40" value=""></textarea>
</FORM>
</body>
</html>

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


#27474

Fromjonas.thornvall@gmail.com
Date2015-05-03 07:12 -0700
Message-ID<a446b186-9ac4-410d-846d-3b2406a0bfd2@googlegroups.com>
In reply to#27473
Den söndag 3 maj 2015 kl. 13:30:27 UTC+2 skrev jonas.t...@gmail.com:
> Den lördag 2 maj 2015 kl. 14:50:40 UTC+2 skrev jonas.t...@gmail.com:
> > Den lördag 2 maj 2015 kl. 00:19:54 UTC+2 skrev Evertjan.:
> > > "Christoph M. Becker" <cmbecker69@arcor.de> wrote on 01 mei 2015 in
> > > comp.lang.javascript: 
> > > 
> > > > Thomas 'PointedEars' Lahn wrote:
> > > > 
> > > >> Christoph M. Becker wrote:
> > > >> 
> > > >>>       function arraySwap(x,y) {
> > > >>>         var tempArrName = this[y];
> > > >>>         this[y] = this[x];
> > > >>>         this[x] = tempArrName;
> > > >>>       }
> > > >> 
> > > >> Also, � othis�   is undefined in arraySwap() if that function is called
> > > >> in strict mode.
> > > > 
> > > > Yes, indeed.  strict mode prevents such dangerous constructs where the
> > > > global object would be (inadvertently?) modified.
> > > > 
> > > > It seems to me that a generally correct arraySwap() could be written by
> > > > interchanging the array *elemtents*, but I'd rather avoid such a
> > > > function at all.
> > > 
> > > I would prefer not to copy large arrays 3 times for each swap,
> > > if I where the OP, quod non.
> > > 
> > > Perhaps the OP could use indirect pointers to the arrays?
> > > 
> > > what about window[x] ??
> > > 
> > > -- 
> > > Evertjan.
> > > The Netherlands.
> > > (Please change the x'es to dots in my emailaddress)
> > 
> > This works called from main and will do for now.
> > 
> > function checkArrayOrder()
> > {
> >    res = arrCompare(arrOne, arrTwo)
> >    if (res)
> >    {
> >       document.calc.stats.value += "Swap! First term smaller=" + res + "\n";
> >       var tempArr = arrOne.slice();
> >       arrOne = [];
> >       arrOne = arrTwo.slice();
> >       arrTwo = [];
> >       arrTwo = tempArr.slice();
> >    }
> >    
> > }
> 
> Still only unsigned operands and no parser, so only feed pair +,-,*, but better structure to the script.
> 
> (At some Point the operands will be signed, and use factorial/decimal digit places within base)
> 
> But now i will implement baseconversion upon the arrays and then the arithmetic should work automaticly using that base. 
> 
> After baseconversion done. Add, subtract should hold in bases upto integer/2 and multiplication in upto base SQRT integer.
> 
> One or two Days more.
> 
> <SCRIPT LANGUAGE="Javascript">
> 
> /* 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;
> }
> 
> /* MULTIPLY THE TWO VARIABLES STORED IN MULTONE AND MULTTWO USING THE CHOSEN BASE */ 
> 
> function naiveMult(base, multOne, multTwo) 
> {
>    var total = []; 
>    var addResult = []; 
>    total[0] = 0; 
> 
>    for (var i = 0; i < smallArr; i ++ ) 
>    { 
>       
>       multresult = []; 
>       remainder = 0; 
>       tempVal = 0; 
>       tempDig = 0; 
>       j = 0; 
> 
>       if(i > 0) 
>       { 
>          for(zero = 0; zero < i; zero ++ ) multresult[zero] = 0; 
>       } 
>      
>       while(j < bigArr) 
>       { 
>          intMult++; 
>          tempVal = (multOne[j] * multTwo[i]) + remainder; 
>          // document.calc.stats.value += tempVal +"=" + multOne[j] +"*" + multTwo[i]+"+"+remainder+"\n";
>          remainder = 0; 
> 
>          if (tempVal >= base) 
>          { 
>             tempDig = tempVal % base; 
>             
>             remainder = (tempVal - tempDig) / base; 
>            
>             multresult[j + i] = tempDig; 
>          //    document.calc.stats.value += "rm= "+1+" index "+(j+i)+" digit = "+tempDig+"\n";
>          } 
>          else 
>          { 
>          //   document.calc.stats.value += "no rm index "+(j+i)+" digit = "+tempVal+"\n";
>             multresult[j + i] = tempVal; 
>          } 
>          j ++ ; 
>       } 
> 
>       if(remainder != 0) 
>       { 
>          //Remainder after last digit multiplication 
>          multresult[j + i] = remainder; 
>       } 
>       //      document.calc.stats.value += total+" + "+multresult+"\n";
>       smallAddArr=multresult.length;
>       bigAddArr=total.length;
>       addResult=naiveAdd(base, multresult, total,smallAddArr,bigAddArr); 
>       //     document.calc.stats.value += "addResult = "+addResult+"\n";
>       total=addResult; 
>    } 
>    return total; 
> } 
> 
> 
> 
> /* ADD TWO THE TWO VARIABLES STORED IN ADDONE AND ADDTWO USING CHOSEN BASE */
> 
> function naiveAdd(base, addOne, addTwo,shortloop,longloop)
> {
>     
>    addResult = [];
>    remainder = 0;
> 
>    for (i = 0; i < shortloop; 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 < longloop)
>    {
>       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;
> }
> 
> /* FIX THIS LATER */
> 
> function prepSubt(subOne, subTwo)
> {
> 
>    // 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])
>       {
> //  document.calc.stats.value += "Digit " + i + " subOne less than subTwo digit\n";
>          intBorrow ++ ;
>          subOne[i] += base;
>          j = i;
>          while (subOne[j + 1] == 0)
>          {
>             subOne[j + 1] += base - 1;
> //   document.calc.stats.value += "Borrow 1 from digit=" + (j + 1) + " Remainder=" + subOne[j + 1] + "\n";
>             j ++ ;
> 
>          }
>          subOne[j + 1] += - 1;
> //         document.calc.stats.value += "Borrow 1 from digit=" + (j + 1) + " Remainder=" + subOne[j + 1] + "\n";
>       }
> 
>       j = 0;
>       result[i] = subOne[i] - subTwo[i];
>       intSub ++ ;
> //  document.calc.stats.value += "Subtraction digit=" + i + " " + subOne[i] + "-" + subTwo[i] + "=" + result[i] + "\n";
>    }
>    while(i < bigArr)
>    {
>       result[i] = subOne[i];
>       i ++ ;
>    }
>    while (result[bigArr - 1] == 0)
>    {
>       result.splice(bigArr - 1, 1);
>       bigArr += - 1;
>    }
>    return result;
> }
> 
> /* RESET FORM FIELDS, FETCH BASE AND STRING TO EVALUATE */
> 
> function fetchVal()
> {
>    document.calc.result.value = "";
>    document.calc.stats.value = "";
>    document.calc.outbase.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()
> {
>    document.calc.outbase.value +=" arrOne = "+ arrOne;
>    document.calc.outbase.value +=" arrTwo = "+ arrTwo;
>    if (operation == "-")
>    {
>       document.calc.stats.value += "[OPERATION SUBTRACT] - \n";
>       out = naiveSub(base, arrOne, arrTwo);
>    }
>    else if (operation == "+")
>    {
>       document.calc.stats.value += "[OPERATION ADD] + \n";
>       out = naiveAdd(base, arrOne, arrTwo,smallArr,bigArr);
>    }
>    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 = newbase(genArr[0], base);
>    tempA = arrOne;
>    arrTwo = newbase(genArr[1], base);
>    tempB = arrTwo;
> }
> 
> /* 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;
> }
> 
> /*  IF (arrOne < arrTwo) SWAP THE ARRAYS SO THE BIGGER TERM STORED IN WITHIN (arrOne) */
> 
> function checkArrayOrder()
> {
>    res = arrCompare(arrOne, arrTwo)
>    if (res)
>    {
>       document.calc.stats.value += "Swap arrays! First array operand smaller=" + res + "\n";
>       var tempArr = arrOne.slice();
>       arrOne = [];
>       arrOne = arrTwo.slice();
>       arrTwo = [];
>       arrTwo = tempArr.slice();
>    }
>    
> 
> }
> 
> /* 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()
> {
>    /* TIMER START */
>    var start = new Date().getTime();  //Start time the opeation
>    /* 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;
>    //Reset variables from form 
>    base = 0;
>    strEval = "";
>    fetchVal();                  // Get stringsvalues from form
>    /* INIT END */
>    
>    /* MAIN FUNCTION */
>    genArr = checkOperation();   // 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
> 
>    }
>   
>    checkArrayOrder();           // Arrange order of arrays, if arrOne < arrTwo swap the arrays
>    bigArr = arrOne.length;     // Get array lengths 
>    smallArr = arrTwo.length;            
>    performOperation();         // Do the arithmetic operation stored in sign, upon operand pair
>    resOutForm();               // Printout result
>    var end = new Date().getTime(); //End time the operation
>    var time = end - start;
>    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>ARITHMETICS IN ANYBASE USING BIG NUMBERS</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="10" size="10"><br><BR>
> Evaluate <input type="text" name="eval" value="21*1151" size="300"><br>
> New base content arrays.(rev)<input type="text" name="outbase" value="" size="60"><br>
> <FONT COLOR="gray">
> RESULT<br>
> <textarea name="result" rows="20" cols="120"></textarea><br>
> STATUS</B><br>
> <textarea name="stats" cols="120" rows="40" value=""></textarea>
> </FORM>
> </body>
> </html>

If someone wants to change newbase into a bignumb function all the necessary functions there. For a skilled programmer probably no more than 10 lines of code.

<SCRIPT LANGUAGE="Javascript">

/*CONVERT A DECIMAL NUMBER INTO ANYBASE */ 

function newbase(decNumber,base){ 
var out =[]; 
digits=1; 
digmult=1; 
while(digmult*base<=decNumber){ 
        digmult=digmult*base 
        digits++; 
} 
digsave=digmult; 
while(decNumber>0 || digits>=0){ 
        loop=1; 
        digit=0; 
               for (digit=0;digit<=base;digit++) { 
        if((digit+1)*digmult>decNumber)break; 
               } 
        out[digits]=digit; 
        digmult=digmult*digit; 
        decNumber=decNumber-digmult; 
        digsave=digsave/base; 
        digmult=digsave; 
        digits--; 
        } 
if(out[0]==0) out.splice(0,1); 
return out; 
} 

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

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

function naiveMult(base, multOne, multTwo)
{
   var total = [];
   var addResult = [];
   total[0] = 0;

   for (var i = 0; i < smallArr; i ++ )
   {

      multresult = [];
      remainder = 0;
      tempVal = 0;
      tempDig = 0;
      j = 0;

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

      while(j < bigArr)
      {
         intMult ++ ;
         tempVal = (multOne[j] * multTwo[i]) + remainder;
         // document.calc.stats.value += tempVal + "=" + multOne[j] + "*" + multTwo[i] + "+" + remainder + "\n";
         remainder = 0;

         if (tempVal >= base)
         {
            tempDig = tempVal % base;

            remainder = (tempVal - tempDig) / base;

            multresult[j + i] = tempDig;
            //    document.calc.stats.value += "rm= " + 1 + " index " + (j + i) + " digit = " + tempDig + "\n";
         }
         else
         {
            //   document.calc.stats.value += "no rm index " + (j + i) + " digit = " + tempVal + "\n";
            multresult[j + i] = tempVal;
         }
         j ++ ;
      }

      if(remainder != 0)
      {
         // Remainder after last digit multiplication
         multresult[j + i] = remainder;
      }
      //      document.calc.stats.value += total + " + " + multresult + "\n";
      smallAddArr = multresult.length;
      bigAddArr = total.length;
      addResult = naiveAdd(base, multresult, total, smallAddArr, bigAddArr);
      //     document.calc.stats.value += "addResult = " + addResult + "\n";
      total = addResult;
   }
   return total;
}



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

function naiveAdd(base, addOne, addTwo, shortloop, longloop)
{

   addResult = [];
   remainder = 0;

   for (i = 0; i < shortloop; 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 < longloop)
   {
      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;
}

/* FIX THIS LATER */

function prepSubt(subOne, subTwo)
{

   // 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])
      {
         //  document.calc.stats.value += "Digit " + i + " subOne less than subTwo digit\n";
         intBorrow ++ ;
         subOne[i] += base;
         j = i;
         while (subOne[j + 1] == 0)
         {
            subOne[j + 1] += base - 1;
            //   document.calc.stats.value += "Borrow 1 from digit=" + (j + 1) + " Remainder=" + subOne[j + 1] + "\n";
            j ++ ;

         }
         subOne[j + 1] += - 1;
         //         document.calc.stats.value += "Borrow 1 from digit=" + (j + 1) + " Remainder=" + subOne[j + 1] + "\n";
      }

      j = 0;
      result[i] = subOne[i] - subTwo[i];
      intSub ++ ;
      //  document.calc.stats.value += "Subtraction digit=" + i + " " + subOne[i] + "-" + subTwo[i] + "=" + result[i] + "\n";
   }
   while(i < bigArr)
   {
      result[i] = subOne[i];
      i ++ ;
   }
   while (result[bigArr - 1] == 0)
   {
      result.splice(bigArr - 1, 1);
      bigArr += - 1;
   }
   return result;
}

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

function fetchVal()
{
   document.calc.result.value = "";
   document.calc.stats.value = "";
   document.calc.outbase.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 = naiveSub(base, arrOne, arrTwo);
   }
   else if (operation == "+")
   {
      document.calc.stats.value += "[OPERATION ADD] + \n";
      out = naiveAdd(base, arrOne, arrTwo, smallArr, bigArr);
   }
   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 = newbase(genArr[0], base);
   tempA = arrOne;
   arrTwo = newbase(genArr[1], base);
   tempB = arrTwo;
}

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

/*  IF (arrOne < arrTwo) SWAP THE ARRAYS SO THE BIGGER TERM STORED WITHIN (arrOne) */

function checkArrayOrder()
{
   res = arrCompare(arrOne, arrTwo)
   if (res)
   {
      document.calc.stats.value += "Swap arrays! First array operand smaller=" + res + "\n";
      var tempArr = arrOne.slice();
      arrOne = [];
      arrOne = arrTwo.slice();
      arrTwo = [];
      arrTwo = tempArr.slice();
   }


}

/* 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()
{
   /* TIMER START */
   var start = new Date().getTime();
   // Start time the opeation
   /* 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 */

   /* MAIN FUNCTION */
   genArr = checkOperation();
   // 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

   }

   checkArrayOrder();
   // Arrange order of arrays, if arrOne < arrTwo swap the arrays
   bigArr = arrOne.length;
   // Get array lengths
   smallArr = arrTwo.length;
   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.outbase.value += baseStr;
   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>ARITHMETICS IN ANYBASE USING BIG NUMBERS</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="2" size="10"><br><BR>
Evaluate "base 10" <input type="text" name="eval" value="21*1151" size="300"><br>
New base content arrays.(rev)<input type="text" name="outbase" value="" size="60"><br>
<FONT COLOR="gray">
RESULT<br>
<textarea name="result" rows="20" cols="120"></textarea><br>
STATUS</B><br>
<textarea name="stats" cols="120" rows="40" value=""></textarea>
</FORM>
</body>
</html>

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


#27475

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-05-03 16:45 +0100
Message-ID<87lhh5zmuy.fsf@bsb.me.uk>
In reply to#27474
jonas.thornvall@gmail.com writes:
<snip>

Why did you quote over 450 lines of a previous post?  Please trim you
quotes.

> If someone wants to change newbase into a bignumb function all the
> necessary functions there. For a skilled programmer probably no more
> than 10 lines of code.
>
> <SCRIPT LANGUAGE="Javascript">
>
> /*CONVERT A DECIMAL NUMBER INTO ANYBASE */ 
>
> function newbase(decNumber,base){

'decNumber' is not a decimal number.  Both the name and the comment are
wrong.

> var out =[]; 
> digits=1;

global

> digmult=1;

global

> while(digmult*base<=decNumber){ 
>         digmult=digmult*base 
>         digits++; 
> } 
> digsave=digmult;

global

> while(decNumber>0 || digits>=0){ 
>         loop=1;

global

>         digit=0;

global

>                for (digit=0;digit<=base;digit++) { 
>         if((digit+1)*digmult>decNumber)break; 
>                } 
>         out[digits]=digit; 
>         digmult=digmult*digit; 
>         decNumber=decNumber-digmult; 
>         digsave=digsave/base; 
>         digmult=digsave; 
>         digits--; 
>         } 
> if(out[0]==0) out.splice(0,1); 
> return out; 
> }

What about

  function newbase(num, base)
  {
      var digits = [];
      do {
          digits.push(num % base);
          num = Math.floor(num / base);
      } while (num);
      return digits;
  }

?  This is the usual way to to it.

-- 
Ben.

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


#27476

Fromjonas.thornvall@gmail.com
Date2015-05-03 09:17 -0700
Message-ID<033ac334-3ecd-4c66-bd8d-d39910fbfcf8@googlegroups.com>
In reply to#27475
Den söndag 3 maj 2015 kl. 17:45:14 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
> <snip>
> 
> Why did you quote over 450 lines of a previous post?  Please trim you
> quotes.
> 
> > If someone wants to change newbase into a bignumb function all the
> > necessary functions there. For a skilled programmer probably no more
> > than 10 lines of code.
> >
> > <SCRIPT LANGUAGE="Javascript">
> >
> > /*CONVERT A DECIMAL NUMBER INTO ANYBASE */ 
> >
> > function newbase(decNumber,base){
> 
> 'decNumber' is not a decimal number.  Both the name and the comment are
> wrong.
> 
> > var out =[]; 
> > digits=1;
> 
> global
> 
> > digmult=1;
> 
> global
> 
> > while(digmult*base<=decNumber){ 
> >         digmult=digmult*base 
> >         digits++; 
> > } 
> > digsave=digmult;
> 
> global
> 
> > while(decNumber>0 || digits>=0){ 
> >         loop=1;
> 
> global
> 
> >         digit=0;
> 
> global
> 
> >                for (digit=0;digit<=base;digit++) { 
> >         if((digit+1)*digmult>decNumber)break; 
> >                } 
> >         out[digits]=digit; 
> >         digmult=digmult*digit; 
> >         decNumber=decNumber-digmult; 
> >         digsave=digsave/base; 
> >         digmult=digsave; 
> >         digits--; 
> >         } 
> > if(out[0]==0) out.splice(0,1); 
> > return out; 
> > }
> 
> What about
> 
>   function newbase(num, base)
>   {
>       var digits = [];
>       do {
>           digits.push(num % base);
>           num = Math.floor(num / base);
>       } while (num);
>       return digits;
>   }
> 
> ?  This is the usual way to to it.
> 
> -- 
> Ben.

I know exactly what to do the problem is to get it work, first base and decnumber must be read intos array. Then replace the for loops into conditional loops that break on arrCompare. The adds and multiplication must use the big numb functions.

That is what i try todo, transform the function above into doing bignumb baseconversion. The function above work fine with its minor flaws, but howto get it convert form strings into other bases without cause overflow is not that easy.

All the functions are there to do it, but it is still a hairy problem to convert it into bignumb conversion.

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


#27477

Fromjonas.thornvall@gmail.com
Date2015-05-03 09:22 -0700
Message-ID<9e56ec52-91b5-4e3e-9206-fd970fa69254@googlegroups.com>
In reply to#27475
Den söndag 3 maj 2015 kl. 17:45:14 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
> <snip>
> 
> Why did you quote over 450 lines of a previous post?  Please trim you
> quotes.
> 
> > If someone wants to change newbase into a bignumb function all the
> > necessary functions there. For a skilled programmer probably no more
> > than 10 lines of code.
> >
> > <SCRIPT LANGUAGE="Javascript">
> >
> > /*CONVERT A DECIMAL NUMBER INTO ANYBASE */ 
> >
> > function newbase(decNumber,base){
> 
> 'decNumber' is not a decimal number.  Both the name and the comment are
> wrong.
> 
> > var out =[]; 
> > digits=1;
> 
> global
> 
> > digmult=1;
> 
> global
> 
> > while(digmult*base<=decNumber){ 
> >         digmult=digmult*base 
> >         digits++; 
> > } 
> > digsave=digmult;
> 
> global
> 
> > while(decNumber>0 || digits>=0){ 
> >         loop=1;
> 
> global
> 
> >         digit=0;
> 
> global
> 
> >                for (digit=0;digit<=base;digit++) { 
> >         if((digit+1)*digmult>decNumber)break; 
> >                } 
> >         out[digits]=digit; 
> >         digmult=digmult*digit; 
> >         decNumber=decNumber-digmult; 
> >         digsave=digsave/base; 
> >         digmult=digsave; 
> >         digits--; 
> >         } 
> > if(out[0]==0) out.splice(0,1); 
> > return out; 
> > }
> 
> What about
> 
>   function newbase(num, base)
>   {
>       var digits = [];
>       do {
>           digits.push(num % base);
>           num = Math.floor(num / base);
>       } while (num);
>       return digits;
>   }
> 
> ?  This is the usual way to to it.
> 
> -- 
> Ben.

I need into todo bignumbers so i need to use the function i constructed and arrays holding the number and base rather than integers.

One also need to use arrcompare to know when to break loop.

I can write the pseudocode.... But i can not get it working 10 minutes and i post the pseudocode.

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


#27478

Fromjonas.thornvall@gmail.com
Date2015-05-04 03:38 -0700
Message-ID<2a1bdab8-bc0e-4a82-bce5-37c151ba4b47@googlegroups.com>
In reply to#27477
Den söndag 3 maj 2015 kl. 18:22:22 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 3 maj 2015 kl. 17:45:14 UTC+2 skrev Ben Bacarisse:
> > jonas.thornvall@gmail.com writes:
> > <snip>
> > 
> > Why did you quote over 450 lines of a previous post?  Please trim you
> > quotes.
> > 
> > > If someone wants to change newbase into a bignumb function all the
> > > necessary functions there. For a skilled programmer probably no more
> > > than 10 lines of code.
> > >
> > > <SCRIPT LANGUAGE="Javascript">
> > >
> > > /*CONVERT A DECIMAL NUMBER INTO ANYBASE */ 
> > >
> > > function newbase(decNumber,base){
> > 
> > 'decNumber' is not a decimal number.  Both the name and the comment are
> > wrong.
> > 
> > > var out =[]; 
> > > digits=1;
> > 
> > global
> > 
> > > digmult=1;
> > 
> > global
> > 
> > > while(digmult*base<=decNumber){ 
> > >         digmult=digmult*base 
> > >         digits++; 
> > > } 
> > > digsave=digmult;
> > 
> > global
> > 
> > > while(decNumber>0 || digits>=0){ 
> > >         loop=1;
> > 
> > global
> > 
> > >         digit=0;
> > 
> > global
> > 
> > >                for (digit=0;digit<=base;digit++) { 
> > >         if((digit+1)*digmult>decNumber)break; 
> > >                } 
> > >         out[digits]=digit; 
> > >         digmult=digmult*digit; 
> > >         decNumber=decNumber-digmult; 
> > >         digsave=digsave/base; 
> > >         digmult=digsave; 
> > >         digits--; 
> > >         } 
> > > if(out[0]==0) out.splice(0,1); 
> > > return out; 
> > > }
> > 
> > What about
> > 
> >   function newbase(num, base)
> >   {
> >       var digits = [];
> >       do {
> >           digits.push(num % base);
> >           num = Math.floor(num / base);
> >       } while (num);
> >       return digits;
> >   }
> > 
> > ?  This is the usual way to to it.
> > 
> > -- 
> > Ben.
> 
> I need into todo bignumbers so i need to use the function i constructed and arrays holding the number and base rather than integers.
> 
> One also need to use arrcompare to know when to break loop.
> 
> I can write the pseudocode.... But i can not get it working 10 minutes and i post the pseudocode.

This is only an outline / pseudocoe for how the base conversion would look with bignumb implementation will look. You have to check the arrays so they contain intergers and not chars, that is probably what fail in first loop.

But the key to get it into bignumb is to move, base into an Array, have the loops break on arrayCompare, and use the naiveAdd, naiveSub and naiveMult. 

function newbase(decArr, base)
{

   i = 0;
   tempBase = base.toString();
   baseArr = tempBase.split("").reverse();

   var out = [];
   digits = 1;
   digmult = [];
   oldigmult = [];
   digmult[0] = 1;
   loop = true;
   digits = 0;
   while(loop == true)
   {
      oldigmult[digits] = digmult;
      digmult = naiveMult(10, baseArr, digmult);
      loop = arrCompare(digmult, decArr);
      digits ++ ;
   }

   while(digits >= 0)
   {
      digit = 0;
      while (looptwo == true)
      {
         dArr = ("" + digit).split("");
         digRes = naiveMult(10, dArr, digmult);
         loopTwo = arrCompare(decArr, digRes);
         digit ++
      }
      out[digits] = digit;
      digmult = naiveMult(10,digmult,dArr);
      decArr = naiveSub(10,decArr,digmult);
      digmult = oldigmult[digits -- ];
      }
   if(out[0] == 0) out.splice(0, 1);
   return out;
}

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


Page 2 of 6 — ← Prev page 1 [2] 3 4 5 6  Next page →

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


csiph-web