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


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

Input operands base when used in operation .

Started byjonas.thornvall@gmail.com
First post2015-04-15 09:23 -0700
Last post2015-04-18 18:34 +0200
Articles 20 — 5 participants

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


Contents

  Input operands base when used in operation . jonas.thornvall@gmail.com - 2015-04-15 09:23 -0700
    Re: Input operands base when used in operation . Scott Sauyet <scott.sauyet@gmail.com> - 2015-04-15 13:38 -0700
      Re: Input operands base when used in operation . jonas.thornvall@gmail.com - 2015-04-16 01:27 -0700
        Re: Input operands base when used in operation . jonas.thornvall@gmail.com - 2015-04-16 03:16 -0700
          Re: Input operands base when used in operation . jonas.thornvall@gmail.com - 2015-04-16 03:22 -0700
          Re: Input operands base when used in operation . Scott Sauyet <scott.sauyet@gmail.com> - 2015-04-21 08:39 -0700
            Re: Input operands base when used in operation . Dr J R Stockton <reply1500@merlyn.demon.co.uk.invalid> - 2015-04-22 16:28 +0100
              Re: Input operands base when used in operation . Scott Sauyet <scott.sauyet@gmail.com> - 2015-04-23 04:17 -0700
                Re: Input operands base when used in operation . Dr J R Stockton <reply1500@merlyn.demon.co.uk.invalid> - 2015-04-25 22:09 +0100
                  Re: Input operands base when used in operation . Scott Sauyet <scott.sauyet@gmail.com> - 2015-04-27 09:51 -0700
                    Re: Input operands base when used in operation . jonas.thornvall@gmail.com - 2015-04-28 05:05 -0700
                      Re: Input operands base when used in operation . Scott Sauyet <scott.sauyet@gmail.com> - 2015-04-30 08:45 -0700
                        Re: Input operands base when used in operation . jonas.thornvall@gmail.com - 2015-04-30 11:17 -0700
                          Re: Input operands base when used in operation . Scott Sauyet <scott.sauyet@gmail.com> - 2015-04-30 13:38 -0700
                            Re: Input operands base when used in operation . jonas.thornvall@gmail.com - 2015-04-30 23:23 -0700
              Re: Input operands base when used in operation . jonas.thornvall@gmail.com - 2015-04-24 03:10 -0700
                Re: Input operands base when used in operation . Scott Sauyet <scott.sauyet@gmail.com> - 2015-04-27 09:45 -0700
            Re: Input operands base when used in operation . jonas.thornvall@gmail.com - 2015-04-24 02:54 -0700
    Re: Input operands base when used in operation . JJ <jj4public@vfemail.net> - 2015-04-18 23:17 +0700
      Re: Input operands base when used in operation . "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-04-18 18:34 +0200

#27381 — Input operands base when used in operation .

Fromjonas.thornvall@gmail.com
Date2015-04-15 09:23 -0700
SubjectInput operands base when used in operation .
Message-ID<ba0f7789-1c59-4d5e-ab11-9cd2484a651e@googlegroups.com>
Is there a way to specify that the operands is not base 10? Or do one have to program it? If so which bases are supported. 

[toc] | [next] | [standalone]


#27382

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-04-15 13:38 -0700
Message-ID<0489a128-71a9-4d2a-b747-c08679028a6b@googlegroups.com>
In reply to#27381
jonas.thornvall@gmail.com wrote:

> Is there a way to specify that the operands is not base 10? Or do 
> one have to program it? If so which bases are supported.

What is this supposed to mean?

Operands don't have bases.  If you mean that the collections of characters in source code or some other input format represents a number using one base or another, then there are straightforward rules for most languages.  (Javascript is slightly complicated by the changing nature of octal representations in source code in different specification versions.)  But the simplest rule I can think of is to assume that your data is base 10 unless you have a reason to suspect otherwise.  :-)

  -- Scott

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


#27387

Fromjonas.thornvall@gmail.com
Date2015-04-16 01:27 -0700
Message-ID<d64663f6-684f-4818-b66b-018466140e33@googlegroups.com>
In reply to#27382
Den onsdag 15 april 2015 kl. 22:38:54 UTC+2 skrev Scott Sauyet:
> jonas.thornvall@gmail.com wrote:
> 
> > Is there a way to specify that the operands is not base 10? Or do 
> > one have to program it? If so which bases are supported.
> 
> What is this supposed to mean?
> 
> Operands don't have bases.  If you mean that the collections of characters in source code or some other input format represents a number using one base or another, then there are straightforward rules for most languages.  (Javascript is slightly complicated by the changing nature of octal representations in source code in different specification versions.)  But the simplest rule I can think of is to assume that your data is base 10 unless you have a reason to suspect otherwise.  :-)
> 
>   -- Scott

Well in the end a base is the interpretation of digitvalues, now a byte or an integer is only a trivial grouping of binaries. But the digitplace is the interpretation rules of that *grouping*, and that is dependent upon the base.

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


#27388

Fromjonas.thornvall@gmail.com
Date2015-04-16 03:16 -0700
Message-ID<83e4afe9-d41b-4ccc-90b9-2450ae909eb7@googlegroups.com>
In reply to#27387
Den torsdag 16 april 2015 kl. 10:27:50 UTC+2 skrev jonas.t...@gmail.com:
> Den onsdag 15 april 2015 kl. 22:38:54 UTC+2 skrev Scott Sauyet:
> > jonas.thornvall@gmail.com wrote:
> > 
> > > Is there a way to specify that the operands is not base 10? Or do 
> > > one have to program it? If so which bases are supported.
> > 
> > What is this supposed to mean?
> > 
> > Operands don't have bases.  If you mean that the collections of characters in source code or some other input format represents a number using one base or another, then there are straightforward rules for most languages.  (Javascript is slightly complicated by the changing nature of octal representations in source code in different specification versions.)  But the simplest rule I can think of is to assume that your data is base 10 unless you have a reason to suspect otherwise.  :-)
> > 
> >   -- Scott
> 
> Well in the end a base is the interpretation of digitvalues, now a byte or an integer is only a trivial grouping of binaries. But the digitplace is the interpretation rules of that *grouping*, and that is dependent upon the base.

I understad you are a very good programmer Scott i've tried to outline the changes needed to take my base conversion into bignumbs i already have the necessary functions but i get stuck.


I show you the plain conversion function and then the one to take it to bignumb. I have a compare function that compare two arrays *work*, and i have two arithmetic bignumb functions that takes arrays as arguments. The conversion need bignumb add, mult and subt which i've implemented. I find it easy to implement plain ideas, but very hard to adjust them dealing with arguments and arrays.

Plain Version function.

//CONVERT A DECIMAL NUMBER INTO ANYBASE 
function newbaseWORK(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; 

}

My attempt making it use the bignumb functions and compare.

// CONVERT A DECIMAL BIGNUMB INTO ANYBASE
function newbase(decNumber, base)
{ 
   var arrArr=[];
   var out = [];
   digits=1;
   digitsold = 1;
   var digmult = [];
   digmult[0] = 1;
   ibase = base.split("").reverse();
   loop = true;
  
   //Finding digit value nearest the number where "base^x<number"
   while(loop == true)
   {  arrArr[digits]=digmult;                    //Save the instance of multiple
      digitsold = digits;                        //Cashing the old digit 
      digmultold = digmult;                      //Cashing the old multiple
      digmult = naiveMult(ibase, digmult, base); // multiply multiple * base 
      loop = arrCompare(digmult, decNumber, loop);//Check if less than number
      digits ++ ;                                 //Add up the index for arrArr
      if(loop == false){digmult = digmultold;digits=digitsold;} //If digmult >number break (fetch old values)
   }
    
   aRemainder = true;   
   stillDigits = true;
   var digsave = [];

   while(aRemainder == true || digits >= 0)
   {
      moredigits = true;
      var digit = [];
      digit[0] = 0;
      var nrOne = [];
      nrOne[0] = 1;

      //Here i try to find the digits from biggest multiple to smallest using multiples of digmult
  
     while (moredigits == true)
      {
         //Save the old digmult and digit (if false)
         digitold = digit;
         digmultold=digsave;
         //Add one to digit
         digit = naiveAdd(base, digit, nrOne);
         //Doing the multiplication with the new digit 
         digsave = naiveMult(base, digmult, digit);
         //Check if multiple bigger than the number
         moredigits = arrCompare(digsave, decNumber);
         //If bigger than we use the old multiple and digit
         if(moredigits == false)
         {
            digit = digitold;
            digsave=digmultold;
         }
      }
   //Saving the digit
   out[digits] = digit;
   //One less digit
   digits -- ;
   //Getting the next smaller multiple from array (base^x)
   digmult = arrArr[digits];
   //Subtract from the number, (digsave)=digit*(digmult)    
   // digmult=base^x for the digitvalue   
   decNumber = naiveSub(base, decNumber, digsave);
   }
   //Removing any leading zero probably not necessary
   if(out[0] == 0) out.splice(0, 1);
   return out;
}

How could it get so complicated?
 


 

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


#27389

Fromjonas.thornvall@gmail.com
Date2015-04-16 03:22 -0700
Message-ID<54acc687-ffd1-4cdb-969d-d1014261ab0d@googlegroups.com>
In reply to#27388
Den torsdag 16 april 2015 kl. 12:16:18 UTC+2 skrev jonas.t...@gmail.com:
> Den torsdag 16 april 2015 kl. 10:27:50 UTC+2 skrev jonas.t...@gmail.com:
> > Den onsdag 15 april 2015 kl. 22:38:54 UTC+2 skrev Scott Sauyet:
> > > jonas.thornvall@gmail.com wrote:
> > > 
> > > > Is there a way to specify that the operands is not base 10? Or do 
> > > > one have to program it? If so which bases are supported.
> > > 
> > > What is this supposed to mean?
> > > 
> > > Operands don't have bases.  If you mean that the collections of characters in source code or some other input format represents a number using one base or another, then there are straightforward rules for most languages.  (Javascript is slightly complicated by the changing nature of octal representations in source code in different specification versions.)  But the simplest rule I can think of is to assume that your data is base 10 unless you have a reason to suspect otherwise.  :-)
> > > 
> > >   -- Scott
> > 
> > Well in the end a base is the interpretation of digitvalues, now a byte or an integer is only a trivial grouping of binaries. But the digitplace is the interpretation rules of that *grouping*, and that is dependent upon the base.
> 
> I understad you are a very good programmer Scott i've tried to outline the changes needed to take my base conversion into bignumbs i already have the necessary functions but i get stuck.
> 
> 
> I show you the plain conversion function and then the one to take it to bignumb. I have a compare function that compare two arrays *work*, and i have two arithmetic bignumb functions that takes arrays as arguments. The conversion need bignumb add, mult and subt which i've implemented. I find it easy to implement plain ideas, but very hard to adjust them dealing with arguments and arrays.
> 
> Plain Version function.
> 
> //CONVERT A DECIMAL NUMBER INTO ANYBASE 
> function newbaseWORK(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; 
> 
> }
> 
> My attempt making it use the bignumb functions and compare.
> 
> // CONVERT A DECIMAL BIGNUMB INTO ANYBASE
> function newbase(decNumber, base)
> { 
>    var arrArr=[];
>    var out = [];
>    digits=1;
>    digitsold = 1;
>    var digmult = [];
>    digmult[0] = 1;
>    ibase = base.split("").reverse();
>    loop = true;
>   
>    //Finding digit value nearest the number where "base^x<number"
>    while(loop == true)
>    {  arrArr[digits]=digmult;                    //Save the instance of multiple
>       digitsold = digits;                        //Cashing the old digit 
>       digmultold = digmult;                      //Cashing the old multiple
>       digmult = naiveMult(ibase, digmult, base); // multiply multiple * base 
>       loop = arrCompare(digmult, decNumber, loop);//Check if less than number
>       digits ++ ;                                 //Add up the index for arrArr
>       if(loop == false){digmult = digmultold;digits=digitsold;} //If digmult >number break (fetch old values)
>    }
>     
>    aRemainder = true;   
>    stillDigits = true;
>    var digsave = [];
> 
>    while(aRemainder == true || digits >= 0)
>    {
>       moredigits = true;
>       var digit = [];
>       digit[0] = 0;
>       var nrOne = [];
>       nrOne[0] = 1;
> 
>       //Here i try to find the digits from biggest multiple to smallest using multiples of digmult
>   
>      while (moredigits == true)
>       {
>          //Save the old digmult and digit (if false)
>          digitold = digit;
>          digmultold=digsave;
>          //Add one to digit
>          digit = naiveAdd(base, digit, nrOne);
>          //Doing the multiplication with the new digit 
>          digsave = naiveMult(base, digmult, digit);
>          //Check if multiple bigger than the number
>          moredigits = arrCompare(digsave, decNumber);
>          //If bigger than we use the old multiple and digit
>          if(moredigits == false)
>          {
>             digit = digitold;
>             digsave=digmultold;
>          }
>       }
>    //Saving the digit
>    out[digits] = digit;
>    //One less digit
>    digits -- ;
>    //Getting the next smaller multiple from array (base^x)
>    digmult = arrArr[digits];
>    //Subtract from the number, (digsave)=digit*(digmult)    
>    // digmult=base^x for the digitvalue   
>    decNumber = naiveSub(base, decNumber, digsave);
>    }
>    //Removing any leading zero probably not necessary
>    if(out[0] == 0) out.splice(0, 1);
>    return out;
> }
> 
> How could it get so complicated?

Here with the functions it use, it is close but it does not work.
I think the layout is correct but something goes wrong.

<SCRIPT LANGUAGE="Javascript">

// CONVERT A DECIMAL BIGNUMB INTO ANYBASE
function newbase(decNumber, base)
{
   var arrArr=[];
   var out = [];
   digits=1;
   digitsold = 1;
   var digmult = [];
   digmult[0] = 1;
   ibase = base.split("").reverse();
   loop = true;
 
   //Finding digit value nearest the number where "base^x<number"
   while(loop == true)
   {  arrArr[digits]=digmult;                    //Save the instance of multiple
      digitsold = digits;                        //Cashing the old digit
      digmultold = digmult;                      //Cashing the old multiple
      digmult = naiveMult(ibase, digmult, base); // multiply multiple * base
      loop = arrCompare(digmult, decNumber, loop);//Check if less than number
      digits ++ ;                                 //Add up the index for arrArr
      if(loop == false){digmult = digmultold;digits=digitsold;} //If digmult >number break (fetch old values)
   }
   
   aRemainder = true;  
   stillDigits = true;
   var digsave = [];

   while(aRemainder == true || digits >= 0)
   {
      moredigits = true;
      var digit = [];
      digit[0] = 0;
      var nrOne = [];
      nrOne[0] = 1;

      //Here i try to find the digits from biggest multiple to smallest using multiples of digmult
 
     while (moredigits == true)
      {
         //Save the old digmult and digit (if false)
         digitold = digit;
         digmultold=digsave;
         //Add one to digit
         digit = naiveAdd(base, digit, nrOne);
         //Doing the multiplication with the new digit
         digsave = naiveMult(base, digmult, digit);
         //Check if multiple bigger than the number
         moredigits = arrCompare(digsave, decNumber);
         //If bigger than we use the old multiple and digit
         if(moredigits == false)
         {
            digit = digitold;
            digsave=digmultold;
         }
      }
   //Saving the digit
   out[digits] = digit;
   //One less digit
   digits -- ;
   //Getting the next smaller multiple from array (base^x)
   digmult = arrArr[digits];
   //Subtract from the number, (digsave)=digit*(digmult)    
   // digmult=base^x for the digitvalue  
   decNumber = naiveSub(base, decNumber, digsave);
   }
   //Removing any leading zero probably not necessary
   if(out[0] == 0) out.splice(0, 1);
   return out;
} 


// If A < B returns loop = true otherwise loop = false
function arrCompare(A, B, smaller)
{
  

   if(A.length > B.length)
   {
      smaller = false;
   }
   else if(A.length == B.length)
   {
      for(i = A.length - 1; i >= 0; i -- )
      {
         if(A[i] <= B[i])
         {
            smaller = true;
         }
         else
         {
            smaller = false;
         }
      }

   }
   return smaller;
}

/* MULTIPLY TWO VARIABLES */ 

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

   //Check which array bigger minimize multiplications 
   if (multOne.length>multTwo.length){ mshort=multOne.length; mlong=multTwo.length;} 
       else { mshort=multOne.length; mlong=multTwo.length;} 

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

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

      while(j < mlong) 
      { 
         intMult++; 
            
         tempVal = (multOne[i] * multTwo[j]) + remainder; 
         remainder = 0; 

         if (tempVal >= base) 
         { 
            tempDig = tempVal % base; 
            remainder = (tempVal - tempDig) / base; 
            multresult[j + i] = tempDig; 
         } 
         else 
         { 
            multresult[j + i] = tempVal; 
         } 
         j ++ ; 
      } 

      if(remainder != 0) 
      { 
         multresult[j + i] = remainder; 
      } 
      addResult=naiveAdd(base, total, multresult); 
      total=addResult; 
   } 
   return total; 
} 

/* SUBTRACT TWO VARIABLES */ 

function naiveSub(base, subOne, subTwo) 
{ 
      
   subOne.reverse();
   subTwo.reverse();
   lengthOne=subOne.length;
   lengthTwo=subTwo.length;
   for (i = 0; i < lengthOne; 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;
   }


   result = []; 
   for (i = 0; i < lengthOne; i ++ ) 
   { 
      j = 0; 

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

         } 
         subOne[j + 1] += - 1; 
      } 
      j = 0; 
      result[i] = subOne[i] - subTwo[i]; 

   } 

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

/* ADD TWO VARIABLES */ 

function naiveAdd(base, arrOne, arrTwo) 
{ 
    document.calc.stats.value+="\n *I AM ADDING* "+arrOne[0]+" + "+arrTwo[0];
   shortArr=[]; 
   longArr=[]; 
   addResult = []; 
   remainder = 0; 
   //Find shortest and longest array 
   if (arrOne.length >= arrTwo.length) {shortArr = arrTwo; longArr = arrOne;} 
            else  {shortArr = arrOne;longArr = arrTwo;} 
   for (i = 0; i < shortArr.length; i ++ ) 
   { 
      intAdd ++ ; 
      arrOne[i] = parseInt(arrOne[i]); 
      arrTwo[i] = parseInt(arrTwo[i]); 
      if (isNaN(arrOne[i])) arrOne[i] = 0; 
      if (isNaN(arrTwo[i])) arrTwo[i] = 0; 
      addResult[i] = arrOne[i] + arrTwo[i] + remainder; 
      
      if (addResult[i] >= base) 
      { 
         addResult[i] = addResult[i] - base; 
         remainder = 1; 
      } 
      else 
      { 
         remainder = 0; 
      } 
   } 
   //Copying values from the shorter string 
   while(i<longArr.length) {longArr[i]=parseInt(longArr[i]);addResult[i]=longArr[i]+remainder; remainder=0; i++;} 
   //If strings of equal lenght but there is a remainder; 
   if (remainder == 1) addResult[i++] = 1; 
   else addResult.splice(i, 1); 
    document.calc.stats.value+=" = "+ addResult +"\n";
   return addResult; 
} 

/* MAIN */ 
function fetchValues() 
{ 
intMult=0; 
intAdd=0; 
x=1; 
y=1; 
document.calc.result.value=""; 
document.calc.stats.value=""; 
document.calc.outbase.value=""; 
strEval = document.calc.eval.value; 
base = document.calc.formbase.value; 
genArr = strEval.split("*"); 
if(base!=10){
arrOne=newbase(genArr[0].split("").reverse(),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;
}
out=naiveMult(base,arrOne,arrTwo); 
document.calc.outbase.value +=tempA.reverse()+" * \n"; 
document.calc.outbase.value +=tempB.reverse()+"\n"; 
document.calc.stats.value +="Multiplications = "+intMult+"\n"; 
document.calc.stats.value +="Addition = "+intAdd+"\n"; 
tempC=out.reverse();
if(base<11){tempC=tempC.toString();
tempC=tempC.replace(/,/g,'')
}

document.calc.result.value +=" Product = " +tempC+"\n"; 
} 
</SCRIPT> 
<!DOCTYPE html> 
<html lang="en"> 
<head><meta charset="utf-8"/></head> 
<body bgcolor="gold" onload="fetchValues()"> 
<H1>Big numbers</H1><P> 
<FORM name=calc onSubmit="fetchValues(); return false;"> 
<B>INPUT DEC-><input type=submit name="calc" value="Calc"> <input type="text" name="eval" value="25*24" size="300"><br> 
BASE <input type="text" name="formbase" value="9" size="10"><br> 
This calculation<input type="text" name="outbase" value="" size="60"><br> 
<FONT COLOR="white"> 
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]


#27395

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-04-21 08:39 -0700
Message-ID<5aee3d05-e13b-40c1-845b-2db2eb48eb47@googlegroups.com>
In reply to#27388
jonas.thornvall@gmail.com wrote wrote:

> I show you the plain conversion function and then the one to take it to 
> bignumb. I have a compare function that compare two arrays *work*, and i 
> have two arithmetic bignumb functions that takes arrays as arguments. The 
> conversion need bignumb add, mult and subt which i've implemented. I find 
> it easy to implement plain ideas, but very hard to adjust them dealing 
> with arguments and arrays. 
> 
> [ ... much code removed ... ]
> 
> How could it get so complicated?

I don't know.  

I've not  really been following your adventures in bignum very carefully.
They end up with what is to me walls of unreadable code, without any
useful explanation or challenging questions.

Because I have done something similar before in another language, I'm at
least somewhat curious, and would actually *like* to find your work
interesting.

If I understand correctly, you want a function to convert a number 
specified in one base into another.  That's fine.  But these are not
standard Javascript numbers, but your implementations of bignums.
How are these represented?  Are they arrays of integers representing
"digits" of your numbers?  If so, is the most significant or least significant digit first?  Do these represent just integers, fractions, 
fixed "decimals", or some other format?

Without understanding this, I'm not likely to dig into code I find
poorly formatted in order to find problems, nor to offer my own 
alternatives.

But I am curious, and might if I did understand your requirements
better, be willing to spend some time trying my own hand at the
issue.  (The emphasis is on _might_!)

  -- Scott

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


#27402

FromDr J R Stockton <reply1500@merlyn.demon.co.uk.invalid>
Date2015-04-22 16:28 +0100
Message-ID<hQygPZh047NVFwy4@invalid.uk.co.demon.merlyn.invalid>
In reply to#27395
In comp.lang.javascript message <5aee3d05-e13b-40c1-845b-2db2eb48eb47@go
oglegroups.com>, Tue, 21 Apr 2015 08:39:14, Scott Sauyet
<scott.sauyet@gmail.com> posted:

>If I understand correctly, you want a function to convert a number
>specified in one base into another.
> ...

>But I am curious, and might if I did understand your requirements
>better, be willing to spend some time trying my own hand at the
>issue.  (The emphasis is on _might_!)

Doing arithmetic in the new base, multiply each digit of the input by
the appropriate power of the old base, and accumulate the results.

You can currently see this in function ConvBas, starting on line 1557 of
LONGCALC  via  <http://www.merlyn.demon.co.uk/programs/00index.htm>; the
function is indirectly invoked by function Fbas, starting on line 1603.

You will need to be able to read Borland Pascal or Delphi.  The
following minimal demonstrations (in RPN) calculate the date of this
year's Easter Sunday respectively using bases 3 & 13.


C:\HOMEPAGE>longcalc 2015 3 bas #eg wrt wrt     -->  +11 +12

C:\HOMEPAGE>longcalc 2015 13 bas #eg wrt wrt    -->  +4 +5


CAUTION - www.merlyn.demon.co.uk is expected to vanish soon, sometime
appearing in a new guise with luck.

-- 
 (c) John Stockton, nr London UK.  ?@merlyn.demon.co.uk  DOS 3.3, 6.20; WinXP.
  Web  <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms and links.
   PAS EXE TXT ZIP via  <http://www.merlyn.demon.co.uk/programs/00index.htm>
   My DOS  <http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

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


#27403

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-04-23 04:17 -0700
Message-ID<7a44528c-5924-4638-b81e-47f0df9ccbcf@googlegroups.com>
In reply to#27402
Dr J R Stockton wrote:
> Scott Sauyet posted:
> 
>> If I understand correctly, you want a function to convert a number
>> specified in one base into another.
>> ...
> 
>> But I am curious, and might if I did understand your requirements
>> better, be willing to spend some time trying my own hand at the
>> issue.  (The emphasis is on _might_!)

> Doing arithmetic in the new base, multiply each digit of the input by
> the appropriate power of the old base, and accumulate the results.

Yes, the parts you removed from my response had to do with the OP's
unreadable code and poorly specified problem.  My academic degrees are
in mathematics, and I find no particular difficulties in performing
arithematic in various bases, but there are real questions about number
representations in JS.

It's easy enough to represent numbers in bases up to 36 as strings, and
with a little ingenuity, it's quite possible to go significantly higher.
But if the OP wants to extend to arbitrary bases, then presumably there
is a need for arrays to represent the numbers, with each element standing
for a "digit" in the chosen base.  (And even here the limited range of
Javascript integers restricts the bases, although I can't imagine that's
a real-world problem.)  But if we know that this is the representation,
we still need to know how these "digits" are ordered.  There are two
equally reasonable representations.  Putting the least significant digit 
first is somewhat easier to use for calculations but is less familiar.

  -- Scott

(BTW, good luck with the rebirth of merlyn.demon)

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


#27410

FromDr J R Stockton <reply1500@merlyn.demon.co.uk.invalid>
Date2015-04-25 22:09 +0100
Message-ID<okgii5e0JAPVFwwY@invalid.uk.co.demon.merlyn.invalid>
In reply to#27403
In comp.lang.javascript message <7a44528c-5924-4638-b81e-47f0df9ccbcf@go
oglegroups.com>, Thu, 23 Apr 2015 04:17:37, Scott Sauyet
<scott.sauyet@gmail.com> posted:

>It's easy enough to represent numbers in bases up to 36 as strings,
> ...

AFAIK, that gives no unexpected problems for integers up to and
including signed 2^53.

But, when I last tested a while ago, some browsers do unreasonable
things when converting non-integer values to strings in unpopular bases.

I have seen strings with about a thousand places after the non-decimal
point.  And one browser handled a remainder of half or more in terms of
the least significant digit by incrementing the character representing
this digit.  That of course worked very well except when the least
significant digit had represented the value (base-1).  I believe that
has been corrected now.

(Thanks.  No news yet on merlyn.demon)

-- 
 (c) John Stockton, nr London, UK.   E-mail, see Home Page.    Turnpike v6.05.
 Website  <http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
 PAS EXE etc. : <http://www.merlyn.demon.co.uk/programs/> - see in 00index.htm
 Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

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


#27418

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-04-27 09:51 -0700
Message-ID<e98e46cd-fb60-448d-95d6-a122668bc940@googlegroups.com>
In reply to#27410
Dr J R Stockton wrote:
> Scott Sauyet posted:
> 
>> It's easy enough to represent numbers in bases up to 36 as strings,
>> ...
> 
> AFAIK, that gives no unexpected problems for integers up to and
> including signed 2^53.

The problem, so far as I understand it, is to write a custom implementation
of big numbers.  I was just pointing out that one can use Strings so long
as the bases being used are not too large.  But that for larger bases, it's
going to become more straightforward to use arrays of integers.

The built-in implementations of numbers are not that relevant, to my mind.

  -- Scott

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


#27420

Fromjonas.thornvall@gmail.com
Date2015-04-28 05:05 -0700
Message-ID<0d8a0877-3d14-47b5-9f98-ed84b0f9737c@googlegroups.com>
In reply to#27418
Den måndag 27 april 2015 kl. 18:51:53 UTC+2 skrev Scott Sauyet:
> Dr J R Stockton wrote:
> > Scott Sauyet posted:
> > 
> >> It's easy enough to represent numbers in bases up to 36 as strings,
> >> ...
> > 
> > AFAIK, that gives no unexpected problems for integers up to and
> > including signed 2^53.
> 
> The problem, so far as I understand it, is to write a custom implementation
> of big numbers.  I was just pointing out that one can use Strings so long
> as the bases being used are not too large.  But that for larger bases, it's
> going to become more straightforward to use arrays of integers.
> 
> The built-in implementations of numbers are not that relevant, to my mind.
> 
>   -- Scott

Can i ask you Scott do you know if it is common for implementations of arithmetic to use search algorithms or lookup tables?

Because it seem the higher the base the more gain from such approach?
I will have a hard time to give you an example until i actually implement the approach. But the context of it is, in binary base it is not meaning full to use a search because each digitplace only hold two values. A lookup tabel searching for the answer to an operation would not either make sense.

But that is not true if the base big, then you search a lookup table for the answer.

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


#27433

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-04-30 08:45 -0700
Message-ID<e8b9e74a-763e-4076-83b6-9288d152321f@googlegroups.com>
In reply to#27420
jonas.thornvall@gmail.com wrote: wrote:

> Can i ask you Scott do you know if it is common for implementations of 
> arithmetic to use search algorithms or lookup tables?
> 
> Because it seem the higher the base the more gain from such approach?

I can certainly imagine caching the small values, but you'd have to 
determine if a cache-lookup is actually faster than a recalculation.  
Arithmetic is generally reasonably quick, so it might turn out not to 
be worth doing.  Or it might be that by the time you reach a scale where
it's worth doing, it would require too much memory to be practical.

I'd still love to hear how you are representing numbers in higher bases.

What's your data-structure?

  -- Scott

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


#27436

Fromjonas.thornvall@gmail.com
Date2015-04-30 11:17 -0700
Message-ID<db7c215d-84d7-4aa2-92b1-cce0a565a451@googlegroups.com>
In reply to#27433
Den torsdag 30 april 2015 kl. 17:45:30 UTC+2 skrev Scott Sauyet:
> jonas.thornvall@gmail.com wrote: wrote:
> 
> > Can i ask you Scott do you know if it is common for implementations of 
> > arithmetic to use search algorithms or lookup tables?
> > 
> > Because it seem the higher the base the more gain from such approach?
> 
> I can certainly imagine caching the small values, but you'd have to 
> determine if a cache-lookup is actually faster than a recalculation.  
> Arithmetic is generally reasonably quick, so it might turn out not to 
> be worth doing.  Or it might be that by the time you reach a scale where
> it's worth doing, it would require too much memory to be practical.
> 
> I'd still love to hear how you are representing numbers in higher bases.
> 
> What's your data-structure?
> 
>   -- Scott

I wish i could say i had a datastructure, but i really have not yet. Right now each digit value/place just stored as a javascript integer within array. Which of course seem like a  waste of space especially if the base is 2...

But it seem hard to make a optimized datastructure that fit all sizes of bases, it would somehow had to be able to change the allocated space for each storage (unit?).

I probably could come up with something, i am ingenious, but it would not be meaningful until the actual arithmetic work in anybase with bignumbers.

I look at it this way right now using arrays of integer values stored at index, the bases and arithmetic holds for add, subtraction of anysized number, using bases upto (javascript int/2).
And it will do multiplications of anysized numbers in bases upto SQRT(Javascript int).

I have not really thought about division yet, but i remember    it is very close to the approach of my multiplication.

Until i have something generic that work doing arithmetic as  i mentioned above i will not construct a generic datastructure optimized for all bases.

Jonas Thörnvall

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


#27440

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-04-30 13:38 -0700
Message-ID<0cb82840-82d9-4b8f-90dd-2a07537815de@googlegroups.com>
In reply to#27436
jonas.thornvall@gmail.com wrote:
> skrev Scott Sauyet:
>> I'd still love to hear how you are representing numbers in higher bases.
>> 
>> What's your data-structure?
> 
> I wish i could say i had a datastructure, but i really have not yet. 
> Right now each digit value/place just stored as a javascript integer 
> within array. Which of course seem like a  waste of space especially
> if the base is 2...

And 6 * 7, is it represented (in base 10) as

    [4, 2]

or

    [2, 4]

?

The former is more familiar.  The latter might make for slightly
simpler algorithms.

  -- Scott

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


#27441

Fromjonas.thornvall@gmail.com
Date2015-04-30 23:23 -0700
Message-ID<001d29cd-98ed-4e48-b93e-f3db38b57a1e@googlegroups.com>
In reply to#27440
Den torsdag 30 april 2015 kl. 22:39:02 UTC+2 skrev Scott Sauyet:
> jonas.thornvall@gmail.com wrote:
> > skrev Scott Sauyet:
> >> I'd still love to hear how you are representing numbers in higher bases.
> >> 
> >> What's your data-structure?
> > 
> > I wish i could say i had a datastructure, but i really have not yet. 
> > Right now each digit value/place just stored as a javascript integer 
> > within array. Which of course seem like a  waste of space especially
> > if the base is 2...
> 
> And 6 * 7, is it represented (in base 10) as
> 
>     [4, 2]
> 
> or
> 
>     [2, 4]
> 
> ?
> 
> The former is more familiar.  The latter might make for slightly
> simpler algorithms.
> 
>   -- Scott

The arrays is reversed so the later.

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


#27408

Fromjonas.thornvall@gmail.com
Date2015-04-24 03:10 -0700
Message-ID<53c1bacb-067e-4d32-b624-ab2f34acc071@googlegroups.com>
In reply to#27402
Den torsdag 23 april 2015 kl. 00:42:22 UTC+2 skrev Dr J R Stockton:
> In comp.lang.javascript message <5aee3d05-e13b-40c1-845b-2db2eb48eb47@go
> oglegroups.com>, Tue, 21 Apr 2015 08:39:14, Scott Sauyet
> <scott.sauyet@gmail.com> posted:
> 
> >If I understand correctly, you want a function to convert a number
> >specified in one base into another.
> > ...
> 
> >But I am curious, and might if I did understand your requirements
> >better, be willing to spend some time trying my own hand at the
> >issue.  (The emphasis is on _might_!)
> 
> Doing arithmetic in the new base, multiply each digit of the input by
> the appropriate power of the old base, and accumulate the results.
> 
> You can currently see this in function ConvBas, starting on line 1557 of
> LONGCALC  via  <http://www.merlyn.demon.co.uk/programs/00index.htm>; the
> function is indirectly invoked by function Fbas, starting on line 1603.
> 
> You will need to be able to read Borland Pascal or Delphi.  The
> following minimal demonstrations (in RPN) calculate the date of this
> year's Easter Sunday respectively using bases 3 & 13.
> 
> 
> C:\HOMEPAGE>longcalc 2015 3 bas #eg wrt wrt     -->  +11 +12
> 
> C:\HOMEPAGE>longcalc 2015 13 bas #eg wrt wrt    -->  +4 +5
> 
> 
> CAUTION - www.merlyn.demon.co.uk is expected to vanish soon, sometime
> appearing in a new guise with luck.
> 
> -- 
>  (c) John Stockton, nr London UK.  ?@merlyn.demon.co.uk  DOS 3.3, 6.20; WinXP.
>   Web  <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms and links.
>    PAS EXE TXT ZIP via  <http://www.merlyn.demon.co.uk/programs/00index.htm>
>    My DOS  <http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

I forgot to say my bignumb is intended to be generic, so it will do the highlevel arithmetic, in the base of your choice. And give you the result in any bignumb base. 

That is the mainpurpose for me with this Little excersize, the gain of doing arithmetic in higher bases, is a waste of time the mathematicians at sci.math say. I think they are wrong and that there is very much to gain doing arithmetic in both higher bignumb bases and even using more exotic interpretation of digit values.

They say that the rules used for howto interpretate the digitvalues when doing arithmetic, in no way can effect the computational complexity.

And there i know they are wrong.

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


#27417

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-04-27 09:45 -0700
Message-ID<c1ec9388-8b78-487d-aa5d-ec2a0f9316e0@googlegroups.com>
In reply to#27408
jonas.thornvall@gmail.com wrote:

> I forgot to say my bignumb is intended to be generic, so it will do the 
> highlevel arithmetic, in the base of your choice. And give you the result 
> in any bignumb base. 

I had made that assumption.

> That is the mainpurpose for me with this Little excersize, the gain of 
> doing arithmetic in higher bases, is a waste of time the mathematicians at
> sci.math say. 

For mathematicians, these are solved, and fairly trivial, problems.  That
does not imply that they are not worth pursuing for programmers.



>                I think they are wrong and that there is very much to gain
> doing arithmetic in both higher bignumb bases and even using more exotic 
> interpretation of digit values.

If you're expecting gains in calculation performance or in reduced 
algorithmic complexity, do prepare yourself for disappointment.  I had been
under the impression that this was chiefly a learning exercise.



> They say that the rules used for howto interpretate the digitvalues when 
> doing arithmetic, in no way can effect the computational complexity.
> 
> And there i know they are wrong.

There are some pretty smart folks hanging out on sci.math (or there were
in my grad-school days, when I hung out there just to learn what I could.)
If I were to have to bet on this, I'd give pretty high odds in their favor.

:-)

  -- Scott

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


#27407

Fromjonas.thornvall@gmail.com
Date2015-04-24 02:54 -0700
Message-ID<a20d0825-c7e7-4e0f-8991-4773486f75c2@googlegroups.com>
In reply to#27395
Den tisdag 21 april 2015 kl. 17:39:25 UTC+2 skrev Scott Sauyet:
> jonas.thornvall@gmail.com wrote wrote:
> 
> > I show you the plain conversion function and then the one to take it to 
> > bignumb. I have a compare function that compare two arrays *work*, and i 
> > have two arithmetic bignumb functions that takes arrays as arguments. The 
> > conversion need bignumb add, mult and subt which i've implemented. I find 
> > it easy to implement plain ideas, but very hard to adjust them dealing 
> > with arguments and arrays. 
> > 
> > [ ... much code removed ... ]
> > 
> > How could it get so complicated?
> 
> I don't know.  
> 
> I've not  really been following your adventures in bignum very carefully.
> They end up with what is to me walls of unreadable code, without any
> useful explanation or challenging questions.
> 
> Because I have done something similar before in another language, I'm at
> least somewhat curious, and would actually *like* to find your work
> interesting.
> 
> If I understand correctly, you want a function to convert a number 
> specified in one base into another.  That's fine.  But these are not
> standard Javascript numbers, but your implementations of bignums.
> How are these represented?  Are they arrays of integers representing
> "digits" of your numbers?  If so, is the most significant or least significant digit first?  Do these represent just integers, fractions, 
> fixed "decimals", or some other format?
> 
> Without understanding this, I'm not likely to dig into code I find
> poorly formatted in order to find problems, nor to offer my own 
> alternatives.
> 
> But I am curious, and might if I did understand your requirements
> better, be willing to spend some time trying my own hand at the
> issue.  (The emphasis is on _might_!)
> 
>   -- Scott

Well Scott i know it is a bit sloppy written and without the necessary comments for the structure of the bignumbs and the parameters/arguments of the function calls.

Right now i am on vacation, but i will pick it up when i get back. The number pairs sent to subtraction, add and multiply is just as you say stored in an Array where the indexes represent digitplaces (whatever that mean when the base is decimal encoded) at the index. I think add, subbtract and multiply  functions expect the lowest digitvalue at index 0. 

Regarding the real fractional parts within the base, i was thinking doing two functioncalls and add up the results rather then keeping track on the fractional delimiter at least when doing add and subtract? It seem more straightforward, and maybe even faster. With my approach it may even work for multiplication and division, with a Little customization. It is just multiple adds afterall.

I have also implemented something similar in the late 90-s and 80-s, initially in JavaScript but with a string approach. But later i moved onto Java for implementing other types of numbersystems fashioned of my own.

It seem to get easier as the program languages develops, unfortunately i think my programming skills not quite there, yet.

And as you say there is a limit using bases bigger than Javascript integer, and the arithmetic only will hold up to SQRT of integersize. 

But moving base into an Array would help that or?
But it seem very convoluted.

I will Clean it up and comment, when i get back.

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


#27393

FromJJ <jj4public@vfemail.net>
Date2015-04-18 23:17 +0700
Message-ID<161pnkvq8hqib$.1vm3wijbmbtno$.dlg@40tude.net>
In reply to#27381
On Wed, 15 Apr 2015 09:23:31 -0700 (PDT), jonas.thornvall@gmail.com wrote:
> Is there a way to specify that the operands is not base 10? Or do one have to program it? If so which bases are supported.

You can specify a number in octal. e.g. 023 is 19 in decimal.
Or in hexadecimal. e.g. 0x23 is 35 in decimal.

But you can't detect whether a number value was specified in which base.

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


#27394

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-04-18 18:34 +0200
Message-ID<mgu11h$7dn$1@solani.org>
In reply to#27393
JJ wrote:

> On Wed, 15 Apr 2015 09:23:31 -0700 (PDT), jonas.thornvall@gmail.com wrote:
>> Is there a way to specify that the operands is not base 10? Or do one have to program it? If so which bases are supported.
> 
> You can specify a number in octal. e.g. 023 is 19 in decimal.

Unless in strict mode where octal literals are a syntax error.

-- 
Christoph M. Becker

[toc] | [prev] | [standalone]


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


csiph-web