[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.javascript

Convoluted logic trying to do bignumb

JT

4/10/2015 5:27:00 AM


I try to change the basechanger to work with bignumb, but my logic find it hard.
I move decnumber, base and the exponent(digArr) into arrays.

And the function doing multiplication does take the base and two arrays as arguments..
But i feel clueless about howto do the comparission, within the loop?
Of course i can check before loop, but it seem i have to make the check mutliplication within the loop using an IF clause? And if the condition true i really do it?

But the actual IF clause now comparing the values of two multiplicated arrays vs the value of a third array.


[EXAMPLE] Code should compare a number to the ***nearest smaller exponent*** in base of choice e.g find x in 10^x that or in binary 2^x without causing overflow.


var digArr=[];

digArr[1]=1;

numbArr=decNumber.split("");

baseArr=base.split("");

while(digArr*baseArr<=numbArr){

digmult=naiveMult(base,base,digmult);

digits++;
}
37 Answers

JT

4/10/2015 5:45:00 AM

0

Den fredag 10 april 2015 kl. 07:26:56 UTC+2 skrev jonas.t...@gmail.com:
> I try to change the basechanger to work with bignumb, but my logic find it hard.
> I move decnumber, base and the exponent(digArr) into arrays.
>
> And the function doing multiplication does take the base and two arrays as arguments..
> But i feel clueless about howto do the comparission, within the loop?
> Of course i can check before loop, but it seem i have to make the check mutliplication within the loop using an IF clause? And if the condition true i really do it?
>
> But the actual IF clause now comparing the values of two multiplicated arrays vs the value of a third array.
>
>
> [EXAMPLE] Code should compare a number to the ***nearest smaller exponent*** in base of choice e.g find x in 10^x that or in binary 2^x without causing overflow.
>
>
> var digArr=[];
>
> digArr[1]=1;
>
> numbArr=decNumber.split("");
>
> baseArr=base.split("");
>
> while(digArr*baseArr<=numbArr){
>
> digmult=naiveMult(base,base,digmult);
>
> digits++;
> }

I think i need a separate function that compares two arrays so i go for that.

JT

4/10/2015 5:59:00 AM

0

Den fredag 10 april 2015 kl. 07:44:40 UTC+2 skrev jonas.t...@gmail.com:
> Den fredag 10 april 2015 kl. 07:26:56 UTC+2 skrev jonas.t...@gmail.com:
> > I try to change the basechanger to work with bignumb, but my logic find it hard.
> > I move decnumber, base and the exponent(digArr) into arrays.
> >
> > And the function doing multiplication does take the base and two arrays as arguments..
> > But i feel clueless about howto do the comparission, within the loop?
> > Of course i can check before loop, but it seem i have to make the check mutliplication within the loop using an IF clause? And if the condition true i really do it?
> >
> > But the actual IF clause now comparing the values of two multiplicated arrays vs the value of a third array.
> >
> >
> > [EXAMPLE] Code should compare a number to the ***nearest smaller exponent*** in base of choice e.g find x in 10^x that or in binary 2^x without causing overflow.
> >
> >
> > var digArr=[];
> >
> > digArr[1]=1;
> >
> > numbArr=decNumber.split("");
> >
> > baseArr=base.split("");
> >
> > while(digArr*baseArr<=numbArr){
> >
> > digmult=naiveMult(base,base,digmult);
> >
> > digits++;
> > }
>
> I think i need a separate function that compares two arrays so i go for that.



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

}

digits=1;
var digmult=[];
digmult[1]=1;
numbArr=decNumber.split("");
baseArr=base.split("");
smaller="true"
while(smaller=="true"){
digmultOld=digmult;
digmult=naiveMult(base,base,digmult);
answer=arrCompare(decNumber,digmult);
if (answer=="true") {} else {digmult=digmultOld}
digits++;
}

JT

4/10/2015 6:02:00 AM

0

Den fredag 10 april 2015 kl. 07:44:40 UTC+2 skrev jonas.t...@gmail.com:
> Den fredag 10 april 2015 kl. 07:26:56 UTC+2 skrev jonas.t...@gmail.com:
> > I try to change the basechanger to work with bignumb, but my logic find it hard.
> > I move decnumber, base and the exponent(digArr) into arrays.
> >
> > And the function doing multiplication does take the base and two arrays as arguments..
> > But i feel clueless about howto do the comparission, within the loop?
> > Of course i can check before loop, but it seem i have to make the check mutliplication within the loop using an IF clause? And if the condition true i really do it?
> >
> > But the actual IF clause now comparing the values of two multiplicated arrays vs the value of a third array.
> >
> >
> > [EXAMPLE] Code should compare a number to the ***nearest smaller exponent*** in base of choice e.g find x in 10^x that or in binary 2^x without causing overflow.
> >
> >
> > var digArr=[];
> >
> > digArr[1]=1;
> >
> > numbArr=decNumber.split("");
> >
> > baseArr=base.split("");
> >
> > while(digArr*baseArr<=numbArr){
> >
> > digmult=naiveMult(base,base,digmult);
> >
> > digits++;
> > }
>
> I think i need a separate function that compares two arrays so i go for that.

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

}

digits=1;
var digmult=[];
digmult[1]=1;
numbArr=decNumber.split("");
baseArr=base.split("");
smaller="true"
while(smaller=="true"){
digmultOld=digmult;
digmult=naiveMult(base,base,digmult);
answer=arrCompare(decNumber,digmult);
if (answer==true) { digits++;} else {smaller="false";digmult=digmultOld}

}

JT

4/10/2015 6:17:00 AM

0

Den fredag 10 april 2015 kl. 08:02:15 UTC+2 skrev jonas.t...@gmail.com:
> Den fredag 10 april 2015 kl. 07:44:40 UTC+2 skrev jonas.t...@gmail.com:
> > Den fredag 10 april 2015 kl. 07:26:56 UTC+2 skrev jonas.t...@gmail.com:
> > > I try to change the basechanger to work with bignumb, but my logic find it hard.
> > > I move decnumber, base and the exponent(digArr) into arrays.
> > >
> > > And the function doing multiplication does take the base and two arrays as arguments..
> > > But i feel clueless about howto do the comparission, within the loop?
> > > Of course i can check before loop, but it seem i have to make the check mutliplication within the loop using an IF clause? And if the condition true i really do it?
> > >
> > > But the actual IF clause now comparing the values of two multiplicated arrays vs the value of a third array.
> > >
> > >
> > > [EXAMPLE] Code should compare a number to the ***nearest smaller exponent*** in base of choice e.g find x in 10^x that or in binary 2^x without causing overflow.
> > >
> > >
> > > var digArr=[];
> > >
> > > digArr[1]=1;
> > >
> > > numbArr=decNumber.split("");
> > >
> > > baseArr=base.split("");
> > >
> > > while(digArr*baseArr<=numbArr){
> > >
> > > digmult=naiveMult(base,base,digmult);
> > >
> > > digits++;
> > > }
> >
> > I think i need a separate function that compares two arrays so i go for that.
>
> //If A<B returns true otherwise false
> function arrCompare(A,B){
>
> }
>
> digits=1;
> var digmult=[];
> digmult[1]=1;
> numbArr=decNumber.split("");
> baseArr=base.split("");
> smaller="true"
> while(smaller=="true"){
> digmultOld=digmult;
> digmult=naiveMult(base,base,digmult);
> answer=arrCompare(decNumber,digmult);
> if (answer==true) { digits++;} else {smaller="false";digmult=digmultOld}
>
> }

//If A<B returns true otherwise false
function arrCompare(A,B){
if(A.length>B.length)answer="false";
if(A.length==B.length){ if ( A[A.length] > B[B.length] )answer="false";}
}

JT

4/10/2015 8:50:00 AM

0

Den fredag 10 april 2015 kl. 08:17:02 UTC+2 skrev jonas.t...@gmail.com:
> Den fredag 10 april 2015 kl. 08:02:15 UTC+2 skrev jonas.t...@gmail.com:
> > Den fredag 10 april 2015 kl. 07:44:40 UTC+2 skrev jonas.t...@gmail.com:
> > > Den fredag 10 april 2015 kl. 07:26:56 UTC+2 skrev jonas.t...@gmail.com:
> > > > I try to change the basechanger to work with bignumb, but my logic find it hard.
> > > > I move decnumber, base and the exponent(digArr) into arrays.
> > > >
> > > > And the function doing multiplication does take the base and two arrays as arguments..
> > > > But i feel clueless about howto do the comparission, within the loop?
> > > > Of course i can check before loop, but it seem i have to make the check mutliplication within the loop using an IF clause? And if the condition true i really do it?
> > > >
> > > > But the actual IF clause now comparing the values of two multiplicated arrays vs the value of a third array.
> > > >
> > > >
> > > > [EXAMPLE] Code should compare a number to the ***nearest smaller exponent*** in base of choice e.g find x in 10^x that or in binary 2^x without causing overflow.
> > > >
> > > >
> > > > var digArr=[];
> > > >
> > > > digArr[1]=1;
> > > >
> > > > numbArr=decNumber.split("");
> > > >
> > > > baseArr=base.split("");
> > > >
> > > > while(digArr*baseArr<=numbArr){
> > > >
> > > > digmult=naiveMult(base,base,digmult);
> > > >
> > > > digits++;
> > > > }
> > >
> > > I think i need a separate function that compares two arrays so i go for that.
> >
> > //If A<B returns true otherwise false
> > function arrCompare(A,B){
> >
> > }
> >
> > digits=1;
> > var digmult=[];
> > digmult[1]=1;
> > numbArr=decNumber.split("");
> > baseArr=base.split("");
> > smaller="true"
> > while(smaller=="true"){
> > digmultOld=digmult;
> > digmult=naiveMult(base,base,digmult);
> > answer=arrCompare(decNumber,digmult);
> > if (answer==true) { digits++;} else {smaller="false";digmult=digmultOld}
> >
> > }
>
> //If A<B returns true otherwise false
> function arrCompare(A,B){
> if(A.length>B.length)answer="false";
> if(A.length==B.length){ if ( A[A.length] > B[B.length] )answer="false";}
> }

//If A<B returns true otherwise false
function arrCompare(A,B){
if(A.length>B.length)answer="false";
else if (A.length==B.length){ if ( A[A.length] > B[B.length] )answer="false";}
}

Evertjan.

4/10/2015 9:53:00 AM

0

jonas.thornvall@gmail.com wrote on 10 apr 2015 in comp.lang.javascript:

> //If A<B returns true otherwise false

returns?? not in the Javascript way.

> function arrCompare(A,B){
> if(A.length>B.length)answer="false";
> if(A.length==B.length){ if ( A[A.length] > B[B.length] )answer="false";}
>}

You probably mean: A[A.length-1] > B[B.length-1]

However this wil not work for:

var A = [1,5,3];
var B = [1,2,3];

================================

I would prefer a 3-way return,
speed-optimized for [large] inequality:

// If A>B return 1, A=B return 0, A<B return -1

function arrTriCompare(A,B) {
var len = A.length, temp = len - B.length, i;
if (temp)
return Math.sign(temp);
for (i = len-1; i >= 0; i--)
if (temp = A[i] - B[i])
return Math.sign(temp);
return 0;
};





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

JT

4/10/2015 1:34:00 PM

0

Den fredag 10 april 2015 kl. 11:53:14 UTC+2 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 10 apr 2015 in comp.lang.javascript:
>
> > //If A<B returns true otherwise false
>
> returns?? not in the Javascript way.
>
> > function arrCompare(A,B){
> > if(A.length>B.length)answer="false";
> > if(A.length==B.length){ if ( A[A.length] > B[B.length] )answer="false";}
> >}
>
> You probably mean: A[A.length-1] > B[B.length-1]
>
> However this wil not work for:
>
> var A = [1,5,3];
> var B = [1,2,3];
>
> ================================
>
> I would prefer a 3-way return,
> speed-optimized for [large] inequality:
>
> // If A>B return 1, A=B return 0, A<B return -1
>
> function arrTriCompare(A,B) {
> var len = A.length, temp = len - B.length, i;
> if (temp)
> return Math.sign(temp);
> for (i = len-1; i >= 0; i--)
> if (temp = A[i] - B[i])
> return Math.sign(temp);
> return 0;
> };
>
>
>
>
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

A more general question the original loop with the boolean true false is doing a function call, where the boolean no longer exist unless declared global.

What i basicly asking how can i reach the boolean in both function without making it global variable?

You return 0 i see, but how would the loop look that calls the function and break on 0 / false?

JT

4/10/2015 1:55:00 PM

0

Den fredag 10 april 2015 kl. 11:53:14 UTC+2 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 10 apr 2015 in comp.lang.javascript:
>
> > //If A<B returns true otherwise false
>
> returns?? not in the Javascript way.
>
> > function arrCompare(A,B){
> > if(A.length>B.length)answer="false";
> > if(A.length==B.length){ if ( A[A.length] > B[B.length] )answer="false";}
> >}
>
> You probably mean: A[A.length-1] > B[B.length-1]
>
> However this wil not work for:
>
> var A = [1,5,3];
> var B = [1,2,3];
>
> ================================
>
> I would prefer a 3-way return,
> speed-optimized for [large] inequality:
>
> // If A>B return 1, A=B return 0, A<B return -1
>
> function arrTriCompare(A,B) {
> var len = A.length, temp = len - B.length, i;
> if (temp)
> return Math.sign(temp);
> for (i = len-1; i >= 0; i--)
> if (temp = A[i] - B[i])
> return Math.sign(temp);
> return 0;
> };
>
>
>
>
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

I think i understand your code but i try to hold it basic simple.
This was my thought, after you showed me i need loop through.

I know i should use boolean, but i have a hard time get it work.

function arrCompare(A, B, smaller)
{
if(A.length > B.length)
{
smaller = "false";
}
else if(A.length == B.length)
{
for(i = 0; i < A.length; i ++ )
{
if(A[i] < B[i])
{
smaller = "true"
}
else
{
smaller = "false"
}
}

}
return smaller;
}

JT

4/10/2015 2:20:00 PM

0

Den fredag 10 april 2015 kl. 11:53:14 UTC+2 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 10 apr 2015 in comp.lang.javascript:
>
> > //If A<B returns true otherwise false
>
> returns?? not in the Javascript way.
>
> > function arrCompare(A,B){
> > if(A.length>B.length)answer="false";
> > if(A.length==B.length){ if ( A[A.length] > B[B.length] )answer="false";}
> >}
>
> You probably mean: A[A.length-1] > B[B.length-1]
>
> However this wil not work for:
>
> var A = [1,5,3];
> var B = [1,2,3];
>
> ================================
>
> I would prefer a 3-way return,
> speed-optimized for [large] inequality:
>
> // If A>B return 1, A=B return 0, A<B return -1
>
> function arrTriCompare(A,B) {
> var len = A.length, temp = len - B.length, i;
> if (temp)
> return Math.sign(temp);
> for (i = len-1; i >= 0; i--)
> if (temp = A[i] - B[i])
> return Math.sign(temp);
> return 0;
> };
>
>
>
>
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

So far i got
// If A < B returns true otherwise false
function arrCompare(A, B, smaller)
{

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

smaller = "false"
}
}

}
return smaller;
}
// CONVERT A DECIMAL NUMBER INTO ANYBASE
function newbase(decNumber, base)
{
var out = [];
digits = 1;
var digmult = [];
digmult[0] = 1;
numbArr = decNumber.split("");
baseArr = base.split("");
smaller = "true"
while(smaller == "true")
{
digmultOld = digmult;
digmult = naiveMult(base, base, digmult);
smaller = arrCompare(digmult, numbArr, smaller);
if (smaller == "true")
{
digits ++ ;
}
else
{
smaller = "false";
digmult = digmultOld
}
}

digsave = digmult;



This is to do...

while(digits >= 0)
{
digit = 0;
for (digit = 0; digit <= base; digit ++ )
{
if((digit + 1) * digmult > decNumber) //CALL BIGNUMB MULT & COMPARE
break;
}
out[digits] = digit;
digmult = digmult * digit;
decNumber = decNumber - digmult; //CALL BIGNUMB SUBTRACTION
digsave = digsave / base; //No division,save values when multiply
digmult = digsave;
digits -- ;
}
if(out[0] == 0) out.splice(0, 1);
return out;

}

JT

4/10/2015 2:27:00 PM

0

Den fredag 10 april 2015 kl. 07:26:56 UTC+2 skrev jonas.t...@gmail.com:
> I try to change the basechanger to work with bignumb, but my logic find it hard.
> I move decnumber, base and the exponent(digArr) into arrays.
>
> And the function doing multiplication does take the base and two arrays as arguments..
> But i feel clueless about howto do the comparission, within the loop?
> Of course i can check before loop, but it seem i have to make the check mutliplication within the loop using an IF clause? And if the condition true i really do it?
>
> But the actual IF clause now comparing the values of two multiplicated arrays vs the value of a third array.
>
>
> [EXAMPLE] Code should compare a number to the ***nearest smaller exponent*** in base of choice e.g find x in 10^x that or in binary 2^x without causing overflow.
>
>
> var digArr=[];
>
> digArr[1]=1;
>
> numbArr=decNumber.split("");
>
> baseArr=base.split("");
>
> while(digArr*baseArr<=numbArr){
>
> digmult=naiveMult(base,base,digmult);
>
> digits++;
> }

One probably should make also digit an array to compare with the base array, that way one can use bigger bases than 32-bit.