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


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

Bignumb subtraction in anybase

Started byjonas.thornvall@gmail.com
First post2015-03-21 02:11 -0700
Last post2015-03-21 03:55 -0700
Articles 5 — 1 participant

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


Contents

  Bignumb subtraction in anybase jonas.thornvall@gmail.com - 2015-03-21 02:11 -0700
    Re: Bignumb subtraction in anybase jonas.thornvall@gmail.com - 2015-03-21 02:23 -0700
      Re: Bignumb subtraction in anybase jonas.thornvall@gmail.com - 2015-03-21 02:40 -0700
        Re: Bignumb subtraction in anybase jonas.thornvall@gmail.com - 2015-03-21 03:12 -0700
          Re: Bignumb subtraction in anybase jonas.thornvall@gmail.com - 2015-03-21 03:55 -0700

#27094 — Bignumb subtraction in anybase

Fromjonas.thornvall@gmail.com
Date2015-03-21 02:11 -0700
SubjectBignumb subtraction in anybase
Message-ID<ded4b839-0381-4e01-8271-6d007a319e51@googlegroups.com>
Have i missed anything?

/* SUBTRACT TWO VARIABLE */

function naiveSub(base,arrOne,arrTwo) {
document.calc.trash.value="5";
for (i=0;i<length;i++){ 
         if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
             j=i;
             while (arrOne[j+1]==0) {arrOne[j+1]+=base;
             j++;
             LoopedTrue=1;
          }
          if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
          j=0;LoopedTrue=0;
          result[i]=arrOne[i]-arrTwo[i];            
   } 
if (result[i]==0) result.splice(i++, 1);
}

************************************************************************
The full programs so far, the real expression parser will have to wait until the operands work.

<SCRIPT LANGUAGE="Javascript">


/*MULTIPLY TWO VARIABLES*/

function naiveMult(base,arrOne,arrTwo) {

document.calc.trash.value="hello";
}

/* SUBTRACT TWO VARIABLE */

function naiveSub(base,arrOne,arrTwo) {
document.calc.trash.value="5";
for (i=0;i<length;i++){ 
         if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
             j=i;
             while (arrOne[j+1]==0) {arrOne[j+1]+=base;
             j++;
             LoopedTrue=1;
          }
          if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
          j=0;LoopedTrue=0;
          result[i]=arrOne[i]-arrTwo[i];            
   } 
if (result[i]==0) result.splice(i++, 1);
}

/*ADD TWO VARIABLES*/

function naiveAdd(base,arrOne,arrTwo) {
remainder=0;
for (i=0;i<length;i++){ 
result[i]=arrOne[i]+arrTwo[i]+remainder; 
if (result[i]>=base) { result[i]=result[i]-base; remainder=1;} 
else {remainder=0;} 
} 
if (remainder==1) result[i++]=1; else result.splice(i, 1);;
}

/*PREPARE STRING PAIR*/

function strPrep(sign,strOne,strTwo) {
document.calc.trash.value="2";
if (sign=="-" && strOne<strTwo){ temp=strOne; strOne=strTwo; strTwo=temp; }
arrOne=strOne.split("");
arrTwo=strTwo.split("");
arrOne.reverse();
arrTwo.reverse();
if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.length;}
for (i=0;i<length;i++){
		arrOne[i]=parseInt(arrOne[i]);
		arrTwo[i]=parseInt(arrTwo[i]);
		if (isNaN(arrOne[i])) arrOne[i] = 0;
		if (isNaN(arrTwo[i])) arrTwo[i] = 0;
	}
}

/* MULTIPLY Container*/

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

/* SUBTRACTION Container*/

function subWrap(sign,strEval,strBase){
document.calc.trash.value="4";
strOne=subtract[0];
strTwo=subtract[1];
strPrep(sign,strOne,strTwo);
naiveSub(base,arrOne,arrTwo);
out=result.reverse().toString();
document.calc.result.value=out;
}

/* ADDITION Container*/

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

/*PARSE THE STRING*/

function parser(strEval,strBase){
document.calc.trash.value="2";
//sign="*";
//multiply=strEval.split("*");
//multWrap(sign,multiply,strBase)
sign="-";
subtract=strEval.split("-");
subWrap(sign,subtract,strBase);
//sign="+";
//adder=strEval.split("+");
//addWrapper(sign,adder,strBase);
}

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

function fetchValues(){

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

 
}

/*GLOBAL*/
var subtract = [];
var adder  = [];
var arrOne = [];
var arrTwo = [];
var result = [];
length; base=10;temp="";

</SCRIPT>

<HTML>
<BODY>
<FORM name=calc onSubmit="fetchValues(); return false;">
BASE <input type="text" name="formbase" value="10" size="4"><br>
<input type=submit name="calc" value="Calc"><input type="text" name="eval" value="7000-6" size="80"><br>
RESULT<input type="text" name="result" value="" size="80"><br>
Trash<input type="text" name="trash" value="" size="80"><br>
</FORM>
</BODY>
</HTML>

[toc] | [next] | [standalone]


#27095

Fromjonas.thornvall@gmail.com
Date2015-03-21 02:23 -0700
Message-ID<0597c6a9-2426-4c2e-aece-bc98ed52bcdf@googlegroups.com>
In reply to#27094
Den lördag 21 mars 2015 kl. 10:11:20 UTC+1 skrev jonas.t...@gmail.com:
> Have i missed anything?
> 
> /* SUBTRACT TWO VARIABLE */
> 
> function naiveSub(base,arrOne,arrTwo) {
> document.calc.trash.value="5";
> for (i=0;i<length;i++){ 
>          if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
>              j=i;
>              while (arrOne[j+1]==0) {arrOne[j+1]+=base;
>              j++;
>              LoopedTrue=1;
>           }
>           if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
>           j=0;LoopedTrue=0;
>           result[i]=arrOne[i]-arrTwo[i];            
>    } 
> if (result[i]==0) result.splice(i++, 1);
> }
> 
> ************************************************************************
> The full programs so far, the real expression parser will have to wait until the operands work.
> 
> <SCRIPT LANGUAGE="Javascript">
> 
> 
> /*MULTIPLY TWO VARIABLES*/
> 
> function naiveMult(base,arrOne,arrTwo) {
> 
> document.calc.trash.value="hello";
> }
> 
> /* SUBTRACT TWO VARIABLE */
> 
> function naiveSub(base,arrOne,arrTwo) {
> document.calc.trash.value="5";
> for (i=0;i<length;i++){ 
>          if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
>              j=i;
>              while (arrOne[j+1]==0) {arrOne[j+1]+=base;
>              j++;
>              LoopedTrue=1;
>           }
>           if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
>           j=0;LoopedTrue=0;
>           result[i]=arrOne[i]-arrTwo[i];            
>    } 
> if (result[i]==0) result.splice(i++, 1);
> }
> 
> /*ADD TWO VARIABLES*/
> 
> function naiveAdd(base,arrOne,arrTwo) {
> remainder=0;
> for (i=0;i<length;i++){ 
> result[i]=arrOne[i]+arrTwo[i]+remainder; 
> if (result[i]>=base) { result[i]=result[i]-base; remainder=1;} 
> else {remainder=0;} 
> } 
> if (remainder==1) result[i++]=1; else result.splice(i, 1);;
> }
> 
> /*PREPARE STRING PAIR*/
> 
> function strPrep(sign,strOne,strTwo) {
> document.calc.trash.value="2";
> if (sign=="-" && strOne<strTwo){ temp=strOne; strOne=strTwo; strTwo=temp; }
> arrOne=strOne.split("");
> arrTwo=strTwo.split("");
> arrOne.reverse();
> arrTwo.reverse();
> if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.length;}
> for (i=0;i<length;i++){
> 		arrOne[i]=parseInt(arrOne[i]);
> 		arrTwo[i]=parseInt(arrTwo[i]);
> 		if (isNaN(arrOne[i])) arrOne[i] = 0;
> 		if (isNaN(arrTwo[i])) arrTwo[i] = 0;
> 	}
> }
> 
> /* MULTIPLY Container*/
> 
> function multWrap(sign,strEval,strBase){
> strOne=adder[0];
> strTwo=adder[1];
> strPrep(strOne,strTwo);
> naiveMult(base,arrOne,arrTwo);
> out=result.reverse().toString();
> document.calc.result.value=out;
> }
> 
> /* SUBTRACTION Container*/
> 
> function subWrap(sign,strEval,strBase){
> document.calc.trash.value="4";
> strOne=subtract[0];
> strTwo=subtract[1];
> strPrep(sign,strOne,strTwo);
> naiveSub(base,arrOne,arrTwo);
> out=result.reverse().toString();
> document.calc.result.value=out;
> }
> 
> /* ADDITION Container*/
> 
> function addWrap(sign,strEval,strBase){
> strOne=adder[0];
> strTwo=adder[1];
> strPrep(strOne,strTwo);
> naiveAdd(base,arrOne,arrTwo);
> out=result.reverse().toString();
> document.calc.result.value=out;
> }
> 
> /*PARSE THE STRING*/
> 
> function parser(strEval,strBase){
> document.calc.trash.value="2";
> //sign="*";
> //multiply=strEval.split("*");
> //multWrap(sign,multiply,strBase)
> sign="-";
> subtract=strEval.split("-");
> subWrap(sign,subtract,strBase);
> //sign="+";
> //adder=strEval.split("+");
> //addWrapper(sign,adder,strBase);
> }
> 
> /*[MAIN PROGRAM]*/
> /*[TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT]*/
> /*[GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */
> 
> function fetchValues(){
> 
>  strEval = document.calc.eval.value;
>  strBase = document.calc.formbase.value;
>  document.calc.trash.value="1";
>  base=parseInt(strBase);
>  parser(strEval,strBase);
> 
>  
> }
> 
> /*GLOBAL*/
> var subtract = [];
> var adder  = [];
> var arrOne = [];
> var arrTwo = [];
> var result = [];
> length; base=10;temp="";
> 
> </SCRIPT>
> 
> <HTML>
> <BODY>
> <FORM name=calc onSubmit="fetchValues(); return false;">
> BASE <input type="text" name="formbase" value="10" size="4"><br>
> <input type=submit name="calc" value="Calc"><input type="text" name="eval" value="7000-6" size="80"><br>
> RESULT<input type="text" name="result" value="" size="80"><br>
> Trash<input type="text" name="trash" value="" size="80"><br>
> </FORM>
> </BODY>
> </HTML>

Well a lack of paranthese, and i forgot that to carry the borrow down with one for consecutive zeros.

/* SUBTRACT TWO VARIABLE */

function naiveSub(base,arrOne,arrTwo) {
document.calc.trash.value="5";
for (i=0;i<length;i++){ 
         if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
             		j=i;
             		while (arrOne[j+1]==0) {arrOne[j+1]+=base-1; j++; LoopedTrue=1;}
          }
          if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
          j=0;LoopedTrue=0;
          result[i]=arrOne[i]-arrTwo[i];            
   } 
if (result[i]==0) result.splice(i++, 1);
}

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


#27096

Fromjonas.thornvall@gmail.com
Date2015-03-21 02:40 -0700
Message-ID<e19824e8-839e-484a-9329-3f53c805aa38@googlegroups.com>
In reply to#27095
Den lördag 21 mars 2015 kl. 10:23:35 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 21 mars 2015 kl. 10:11:20 UTC+1 skrev jonas.t...@gmail.com:
> > Have i missed anything?
> > 
> > /* SUBTRACT TWO VARIABLE */
> > 
> > function naiveSub(base,arrOne,arrTwo) {
> > document.calc.trash.value="5";
> > for (i=0;i<length;i++){ 
> >          if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
> >              j=i;
> >              while (arrOne[j+1]==0) {arrOne[j+1]+=base;
> >              j++;
> >              LoopedTrue=1;
> >           }
> >           if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
> >           j=0;LoopedTrue=0;
> >           result[i]=arrOne[i]-arrTwo[i];            
> >    } 
> > if (result[i]==0) result.splice(i++, 1);
> > }
> > 
> > ************************************************************************
> > The full programs so far, the real expression parser will have to wait until the operands work.
> > 
> > <SCRIPT LANGUAGE="Javascript">
> > 
> > 
> > /*MULTIPLY TWO VARIABLES*/
> > 
> > function naiveMult(base,arrOne,arrTwo) {
> > 
> > document.calc.trash.value="hello";
> > }
> > 
> > /* SUBTRACT TWO VARIABLE */
> > 
> > function naiveSub(base,arrOne,arrTwo) {
> > document.calc.trash.value="5";
> > for (i=0;i<length;i++){ 
> >          if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
> >              j=i;
> >              while (arrOne[j+1]==0) {arrOne[j+1]+=base;
> >              j++;
> >              LoopedTrue=1;
> >           }
> >           if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
> >           j=0;LoopedTrue=0;
> >           result[i]=arrOne[i]-arrTwo[i];            
> >    } 
> > if (result[i]==0) result.splice(i++, 1);
> > }
> > 
> > /*ADD TWO VARIABLES*/
> > 
> > function naiveAdd(base,arrOne,arrTwo) {
> > remainder=0;
> > for (i=0;i<length;i++){ 
> > result[i]=arrOne[i]+arrTwo[i]+remainder; 
> > if (result[i]>=base) { result[i]=result[i]-base; remainder=1;} 
> > else {remainder=0;} 
> > } 
> > if (remainder==1) result[i++]=1; else result.splice(i, 1);;
> > }
> > 
> > /*PREPARE STRING PAIR*/
> > 
> > function strPrep(sign,strOne,strTwo) {
> > document.calc.trash.value="2";
> > if (sign=="-" && strOne<strTwo){ temp=strOne; strOne=strTwo; strTwo=temp; }
> > arrOne=strOne.split("");
> > arrTwo=strTwo.split("");
> > arrOne.reverse();
> > arrTwo.reverse();
> > if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.length;}
> > for (i=0;i<length;i++){
> > 		arrOne[i]=parseInt(arrOne[i]);
> > 		arrTwo[i]=parseInt(arrTwo[i]);
> > 		if (isNaN(arrOne[i])) arrOne[i] = 0;
> > 		if (isNaN(arrTwo[i])) arrTwo[i] = 0;
> > 	}
> > }
> > 
> > /* MULTIPLY Container*/
> > 
> > function multWrap(sign,strEval,strBase){
> > strOne=adder[0];
> > strTwo=adder[1];
> > strPrep(strOne,strTwo);
> > naiveMult(base,arrOne,arrTwo);
> > out=result.reverse().toString();
> > document.calc.result.value=out;
> > }
> > 
> > /* SUBTRACTION Container*/
> > 
> > function subWrap(sign,strEval,strBase){
> > document.calc.trash.value="4";
> > strOne=subtract[0];
> > strTwo=subtract[1];
> > strPrep(sign,strOne,strTwo);
> > naiveSub(base,arrOne,arrTwo);
> > out=result.reverse().toString();
> > document.calc.result.value=out;
> > }
> > 
> > /* ADDITION Container*/
> > 
> > function addWrap(sign,strEval,strBase){
> > strOne=adder[0];
> > strTwo=adder[1];
> > strPrep(strOne,strTwo);
> > naiveAdd(base,arrOne,arrTwo);
> > out=result.reverse().toString();
> > document.calc.result.value=out;
> > }
> > 
> > /*PARSE THE STRING*/
> > 
> > function parser(strEval,strBase){
> > document.calc.trash.value="2";
> > //sign="*";
> > //multiply=strEval.split("*");
> > //multWrap(sign,multiply,strBase)
> > sign="-";
> > subtract=strEval.split("-");
> > subWrap(sign,subtract,strBase);
> > //sign="+";
> > //adder=strEval.split("+");
> > //addWrapper(sign,adder,strBase);
> > }
> > 
> > /*[MAIN PROGRAM]*/
> > /*[TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT]*/
> > /*[GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */
> > 
> > function fetchValues(){
> > 
> >  strEval = document.calc.eval.value;
> >  strBase = document.calc.formbase.value;
> >  document.calc.trash.value="1";
> >  base=parseInt(strBase);
> >  parser(strEval,strBase);
> > 
> >  
> > }
> > 
> > /*GLOBAL*/
> > var subtract = [];
> > var adder  = [];
> > var arrOne = [];
> > var arrTwo = [];
> > var result = [];
> > length; base=10;temp="";
> > 
> > </SCRIPT>
> > 
> > <HTML>
> > <BODY>
> > <FORM name=calc onSubmit="fetchValues(); return false;">
> > BASE <input type="text" name="formbase" value="10" size="4"><br>
> > <input type=submit name="calc" value="Calc"><input type="text" name="eval" value="7000-6" size="80"><br>
> > RESULT<input type="text" name="result" value="" size="80"><br>
> > Trash<input type="text" name="trash" value="" size="80"><br>
> > </FORM>
> > </BODY>
> > </HTML>
> 
> Well a lack of paranthese, and i forgot that to carry the borrow down with one for consecutive zeros.
> 
> /* SUBTRACT TWO VARIABLE */
> 
> function naiveSub(base,arrOne,arrTwo) {
> document.calc.trash.value="5";
> for (i=0;i<length;i++){ 
>          if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
>              		j=i;
>              		while (arrOne[j+1]==0) {arrOne[j+1]+=base-1; j++; LoopedTrue=1;}
>           }
>           if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
>           j=0;LoopedTrue=0;
>           result[i]=arrOne[i]-arrTwo[i];            
>    } 
> if (result[i]==0) result.splice(i++, 1);
> }

It seem to work now.

300010001234561789-190010001234560788=2,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1  

Can anyone see for which it does fail if it does? 
The sign is stored and not printed out so don't worry about it, just worry about the digits.

I suspect it will work for base 2.

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


#27097

Fromjonas.thornvall@gmail.com
Date2015-03-21 03:12 -0700
Message-ID<e3a05af9-8e35-4096-af7e-ba5bfadbf06a@googlegroups.com>
In reply to#27096
Den lördag 21 mars 2015 kl. 10:40:44 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 21 mars 2015 kl. 10:23:35 UTC+1 skrev jonas.t...@gmail.com:
> > Den lördag 21 mars 2015 kl. 10:11:20 UTC+1 skrev jonas.t...@gmail.com:
> > > Have i missed anything?
> > > 
> > > /* SUBTRACT TWO VARIABLE */
> > > 
> > > function naiveSub(base,arrOne,arrTwo) {
> > > document.calc.trash.value="5";
> > > for (i=0;i<length;i++){ 
> > >          if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
> > >              j=i;
> > >              while (arrOne[j+1]==0) {arrOne[j+1]+=base;
> > >              j++;
> > >              LoopedTrue=1;
> > >           }
> > >           if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
> > >           j=0;LoopedTrue=0;
> > >           result[i]=arrOne[i]-arrTwo[i];            
> > >    } 
> > > if (result[i]==0) result.splice(i++, 1);
> > > }
> > > 
> > > ************************************************************************
> > > The full programs so far, the real expression parser will have to wait until the operands work.
> > > 
> > > <SCRIPT LANGUAGE="Javascript">
> > > 
> > > 
> > > /*MULTIPLY TWO VARIABLES*/
> > > 
> > > function naiveMult(base,arrOne,arrTwo) {
> > > 
> > > document.calc.trash.value="hello";
> > > }
> > > 
> > > /* SUBTRACT TWO VARIABLE */
> > > 
> > > function naiveSub(base,arrOne,arrTwo) {
> > > document.calc.trash.value="5";
> > > for (i=0;i<length;i++){ 
> > >          if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
> > >              j=i;
> > >              while (arrOne[j+1]==0) {arrOne[j+1]+=base;
> > >              j++;
> > >              LoopedTrue=1;
> > >           }
> > >           if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
> > >           j=0;LoopedTrue=0;
> > >           result[i]=arrOne[i]-arrTwo[i];            
> > >    } 
> > > if (result[i]==0) result.splice(i++, 1);
> > > }
> > > 
> > > /*ADD TWO VARIABLES*/
> > > 
> > > function naiveAdd(base,arrOne,arrTwo) {
> > > remainder=0;
> > > for (i=0;i<length;i++){ 
> > > result[i]=arrOne[i]+arrTwo[i]+remainder; 
> > > if (result[i]>=base) { result[i]=result[i]-base; remainder=1;} 
> > > else {remainder=0;} 
> > > } 
> > > if (remainder==1) result[i++]=1; else result.splice(i, 1);;
> > > }
> > > 
> > > /*PREPARE STRING PAIR*/
> > > 
> > > function strPrep(sign,strOne,strTwo) {
> > > document.calc.trash.value="2";
> > > if (sign=="-" && strOne<strTwo){ temp=strOne; strOne=strTwo; strTwo=temp; }
> > > arrOne=strOne.split("");
> > > arrTwo=strTwo.split("");
> > > arrOne.reverse();
> > > arrTwo.reverse();
> > > if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.length;}
> > > for (i=0;i<length;i++){
> > > 		arrOne[i]=parseInt(arrOne[i]);
> > > 		arrTwo[i]=parseInt(arrTwo[i]);
> > > 		if (isNaN(arrOne[i])) arrOne[i] = 0;
> > > 		if (isNaN(arrTwo[i])) arrTwo[i] = 0;
> > > 	}
> > > }
> > > 
> > > /* MULTIPLY Container*/
> > > 
> > > function multWrap(sign,strEval,strBase){
> > > strOne=adder[0];
> > > strTwo=adder[1];
> > > strPrep(strOne,strTwo);
> > > naiveMult(base,arrOne,arrTwo);
> > > out=result.reverse().toString();
> > > document.calc.result.value=out;
> > > }
> > > 
> > > /* SUBTRACTION Container*/
> > > 
> > > function subWrap(sign,strEval,strBase){
> > > document.calc.trash.value="4";
> > > strOne=subtract[0];
> > > strTwo=subtract[1];
> > > strPrep(sign,strOne,strTwo);
> > > naiveSub(base,arrOne,arrTwo);
> > > out=result.reverse().toString();
> > > document.calc.result.value=out;
> > > }
> > > 
> > > /* ADDITION Container*/
> > > 
> > > function addWrap(sign,strEval,strBase){
> > > strOne=adder[0];
> > > strTwo=adder[1];
> > > strPrep(strOne,strTwo);
> > > naiveAdd(base,arrOne,arrTwo);
> > > out=result.reverse().toString();
> > > document.calc.result.value=out;
> > > }
> > > 
> > > /*PARSE THE STRING*/
> > > 
> > > function parser(strEval,strBase){
> > > document.calc.trash.value="2";
> > > //sign="*";
> > > //multiply=strEval.split("*");
> > > //multWrap(sign,multiply,strBase)
> > > sign="-";
> > > subtract=strEval.split("-");
> > > subWrap(sign,subtract,strBase);
> > > //sign="+";
> > > //adder=strEval.split("+");
> > > //addWrapper(sign,adder,strBase);
> > > }
> > > 
> > > /*[MAIN PROGRAM]*/
> > > /*[TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT]*/
> > > /*[GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */
> > > 
> > > function fetchValues(){
> > > 
> > >  strEval = document.calc.eval.value;
> > >  strBase = document.calc.formbase.value;
> > >  document.calc.trash.value="1";
> > >  base=parseInt(strBase);
> > >  parser(strEval,strBase);
> > > 
> > >  
> > > }
> > > 
> > > /*GLOBAL*/
> > > var subtract = [];
> > > var adder  = [];
> > > var arrOne = [];
> > > var arrTwo = [];
> > > var result = [];
> > > length; base=10;temp="";
> > > 
> > > </SCRIPT>
> > > 
> > > <HTML>
> > > <BODY>
> > > <FORM name=calc onSubmit="fetchValues(); return false;">
> > > BASE <input type="text" name="formbase" value="10" size="4"><br>
> > > <input type=submit name="calc" value="Calc"><input type="text" name="eval" value="7000-6" size="80"><br>
> > > RESULT<input type="text" name="result" value="" size="80"><br>
> > > Trash<input type="text" name="trash" value="" size="80"><br>
> > > </FORM>
> > > </BODY>
> > > </HTML>
> > 
> > Well a lack of paranthese, and i forgot that to carry the borrow down with one for consecutive zeros.
> > 
> > /* SUBTRACT TWO VARIABLE */
> > 
> > function naiveSub(base,arrOne,arrTwo) {
> > document.calc.trash.value="5";
> > for (i=0;i<length;i++){ 
> >          if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
> >              		j=i;
> >              		while (arrOne[j+1]==0) {arrOne[j+1]+=base-1; j++; LoopedTrue=1;}
> >           }
> >           if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
> >           j=0;LoopedTrue=0;
> >           result[i]=arrOne[i]-arrTwo[i];            
> >    } 
> > if (result[i]==0) result.splice(i++, 1);
> > }
> 
> It seem to work now.
> 
> 300010001234561789-190010001234560788=2,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1  
> 
> Can anyone see for which it does fail if it does? 
> The sign is stored and not printed out so don't worry about it, just worry about the digits.
> 
> I suspect it will work for base 2.

Well i suspect i got lucky alot of bugs. Must reset j and loop to begin with.
/* SUBTRACT TWO VARIABLE */

function naiveSub(base,arrOne,arrTwo) {
LoopedTrue=0;j=0;
document.calc.trash.value="5";
for (i=0;i<length;i++){ 
         if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
             		j=i;
             		while (arrOne[j+1]==0) {arrOne[j+1]+=base-1; j++; LoopedTrue=1;}
          }
          if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
          j=0;LoopedTrue=0;
          result[i]=arrOne[i]-arrTwo[i];            
   } 
if (result[i]==0) result.splice(i++, 1);
}

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


#27098

Fromjonas.thornvall@gmail.com
Date2015-03-21 03:55 -0700
Message-ID<f2e4c97c-98db-456d-ad95-b9c5dd0df86e@googlegroups.com>
In reply to#27097
Den lördag 21 mars 2015 kl. 11:12:34 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 21 mars 2015 kl. 10:40:44 UTC+1 skrev jonas.t...@gmail.com:
> > Den lördag 21 mars 2015 kl. 10:23:35 UTC+1 skrev jonas.t...@gmail.com:
> > > Den lördag 21 mars 2015 kl. 10:11:20 UTC+1 skrev jonas.t...@gmail.com:
> > > > Have i missed anything?
> > > > 
> > > > /* SUBTRACT TWO VARIABLE */
> > > > 
> > > > function naiveSub(base,arrOne,arrTwo) {
> > > > document.calc.trash.value="5";
> > > > for (i=0;i<length;i++){ 
> > > >          if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
> > > >              j=i;
> > > >              while (arrOne[j+1]==0) {arrOne[j+1]+=base;
> > > >              j++;
> > > >              LoopedTrue=1;
> > > >           }
> > > >           if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
> > > >           j=0;LoopedTrue=0;
> > > >           result[i]=arrOne[i]-arrTwo[i];            
> > > >    } 
> > > > if (result[i]==0) result.splice(i++, 1);
> > > > }
> > > > 
> > > > ************************************************************************
> > > > The full programs so far, the real expression parser will have to wait until the operands work.
> > > > 
> > > > <SCRIPT LANGUAGE="Javascript">
> > > > 
> > > > 
> > > > /*MULTIPLY TWO VARIABLES*/
> > > > 
> > > > function naiveMult(base,arrOne,arrTwo) {
> > > > 
> > > > document.calc.trash.value="hello";
> > > > }
> > > > 
> > > > /* SUBTRACT TWO VARIABLE */
> > > > 
> > > > function naiveSub(base,arrOne,arrTwo) {
> > > > document.calc.trash.value="5";
> > > > for (i=0;i<length;i++){ 
> > > >          if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
> > > >              j=i;
> > > >              while (arrOne[j+1]==0) {arrOne[j+1]+=base;
> > > >              j++;
> > > >              LoopedTrue=1;
> > > >           }
> > > >           if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
> > > >           j=0;LoopedTrue=0;
> > > >           result[i]=arrOne[i]-arrTwo[i];            
> > > >    } 
> > > > if (result[i]==0) result.splice(i++, 1);
> > > > }
> > > > 
> > > > /*ADD TWO VARIABLES*/
> > > > 
> > > > function naiveAdd(base,arrOne,arrTwo) {
> > > > remainder=0;
> > > > for (i=0;i<length;i++){ 
> > > > result[i]=arrOne[i]+arrTwo[i]+remainder; 
> > > > if (result[i]>=base) { result[i]=result[i]-base; remainder=1;} 
> > > > else {remainder=0;} 
> > > > } 
> > > > if (remainder==1) result[i++]=1; else result.splice(i, 1);;
> > > > }
> > > > 
> > > > /*PREPARE STRING PAIR*/
> > > > 
> > > > function strPrep(sign,strOne,strTwo) {
> > > > document.calc.trash.value="2";
> > > > if (sign=="-" && strOne<strTwo){ temp=strOne; strOne=strTwo; strTwo=temp; }
> > > > arrOne=strOne.split("");
> > > > arrTwo=strTwo.split("");
> > > > arrOne.reverse();
> > > > arrTwo.reverse();
> > > > if (arrOne.length>=arrTwo.length) {length=arrOne.length;} else {length=arrTwo.length;}
> > > > for (i=0;i<length;i++){
> > > > 		arrOne[i]=parseInt(arrOne[i]);
> > > > 		arrTwo[i]=parseInt(arrTwo[i]);
> > > > 		if (isNaN(arrOne[i])) arrOne[i] = 0;
> > > > 		if (isNaN(arrTwo[i])) arrTwo[i] = 0;
> > > > 	}
> > > > }
> > > > 
> > > > /* MULTIPLY Container*/
> > > > 
> > > > function multWrap(sign,strEval,strBase){
> > > > strOne=adder[0];
> > > > strTwo=adder[1];
> > > > strPrep(strOne,strTwo);
> > > > naiveMult(base,arrOne,arrTwo);
> > > > out=result.reverse().toString();
> > > > document.calc.result.value=out;
> > > > }
> > > > 
> > > > /* SUBTRACTION Container*/
> > > > 
> > > > function subWrap(sign,strEval,strBase){
> > > > document.calc.trash.value="4";
> > > > strOne=subtract[0];
> > > > strTwo=subtract[1];
> > > > strPrep(sign,strOne,strTwo);
> > > > naiveSub(base,arrOne,arrTwo);
> > > > out=result.reverse().toString();
> > > > document.calc.result.value=out;
> > > > }
> > > > 
> > > > /* ADDITION Container*/
> > > > 
> > > > function addWrap(sign,strEval,strBase){
> > > > strOne=adder[0];
> > > > strTwo=adder[1];
> > > > strPrep(strOne,strTwo);
> > > > naiveAdd(base,arrOne,arrTwo);
> > > > out=result.reverse().toString();
> > > > document.calc.result.value=out;
> > > > }
> > > > 
> > > > /*PARSE THE STRING*/
> > > > 
> > > > function parser(strEval,strBase){
> > > > document.calc.trash.value="2";
> > > > //sign="*";
> > > > //multiply=strEval.split("*");
> > > > //multWrap(sign,multiply,strBase)
> > > > sign="-";
> > > > subtract=strEval.split("-");
> > > > subWrap(sign,subtract,strBase);
> > > > //sign="+";
> > > > //adder=strEval.split("+");
> > > > //addWrapper(sign,adder,strBase);
> > > > }
> > > > 
> > > > /*[MAIN PROGRAM]*/
> > > > /*[TAKE INPUT FROM FORM] [PARSE THE STRING] [CALCULATE RESULT]*/
> > > > /*[GENERIC CALCULATIONS] [USING BIGNUMB OPERATORS] [IN ANYBASE] [UPON OPERANDS] */
> > > > 
> > > > function fetchValues(){
> > > > 
> > > >  strEval = document.calc.eval.value;
> > > >  strBase = document.calc.formbase.value;
> > > >  document.calc.trash.value="1";
> > > >  base=parseInt(strBase);
> > > >  parser(strEval,strBase);
> > > > 
> > > >  
> > > > }
> > > > 
> > > > /*GLOBAL*/
> > > > var subtract = [];
> > > > var adder  = [];
> > > > var arrOne = [];
> > > > var arrTwo = [];
> > > > var result = [];
> > > > length; base=10;temp="";
> > > > 
> > > > </SCRIPT>
> > > > 
> > > > <HTML>
> > > > <BODY>
> > > > <FORM name=calc onSubmit="fetchValues(); return false;">
> > > > BASE <input type="text" name="formbase" value="10" size="4"><br>
> > > > <input type=submit name="calc" value="Calc"><input type="text" name="eval" value="7000-6" size="80"><br>
> > > > RESULT<input type="text" name="result" value="" size="80"><br>
> > > > Trash<input type="text" name="trash" value="" size="80"><br>
> > > > </FORM>
> > > > </BODY>
> > > > </HTML>
> > > 
> > > Well a lack of paranthese, and i forgot that to carry the borrow down with one for consecutive zeros.
> > > 
> > > /* SUBTRACT TWO VARIABLE */
> > > 
> > > function naiveSub(base,arrOne,arrTwo) {
> > > document.calc.trash.value="5";
> > > for (i=0;i<length;i++){ 
> > >          if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
> > >              		j=i;
> > >              		while (arrOne[j+1]==0) {arrOne[j+1]+=base-1; j++; LoopedTrue=1;}
> > >           }
> > >           if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
> > >           j=0;LoopedTrue=0;
> > >           result[i]=arrOne[i]-arrTwo[i];            
> > >    } 
> > > if (result[i]==0) result.splice(i++, 1);
> > > }
> > 
> > It seem to work now.
> > 
> > 300010001234561789-190010001234560788=2,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1  
> > 
> > Can anyone see for which it does fail if it does? 
> > The sign is stored and not printed out so don't worry about it, just worry about the digits.
> > 
> > I suspect it will work for base 2.
> 
> Well i suspect i got lucky alot of bugs. Must reset j and loop to begin with.
> /* SUBTRACT TWO VARIABLE */
> 
> function naiveSub(base,arrOne,arrTwo) {
> LoopedTrue=0;j=0;
> document.calc.trash.value="5";
> for (i=0;i<length;i++){ 
>          if(arrOne[i]<arrTwo[i]){arrOne[i]+=base; 
>              		j=i;
>              		while (arrOne[j+1]==0) {arrOne[j+1]+=base-1; j++; LoopedTrue=1;}
>           }
>           if (LoopedTrue==1){arrOne[j+1]=arrOne[j+1]-1;}
>           j=0;LoopedTrue=0;
>           result[i]=arrOne[i]-arrTwo[i];            
>    } 
> if (result[i]==0) result.splice(i++, 1);
> }

It seem i need some conditional break points out of loop.

[toc] | [prev] | [standalone]


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


csiph-web