Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: JS coding
Replies: 27   Last Post: Feb 24, 2013 6:17 AM

 Messages: [ Previous | Next ]
 JT Posts: 1,448 Registered: 4/7/12
Re: JS coding
Posted: Feb 6, 2013 2:54 AM

On 3 Feb, 23:33, Virgil <vir...@ligriv.com> wrote:
> In article
>
>  JT <jonas.thornv...@gmail.com> wrote:

> > I do not want a counter, i want a general solution to re encode any
> > decimal number where anybase(decimal,base) {}
> > This is probably why people save code instead of posting it to
> > usenet ;D

>
> The process involves repeated divisions with whole number quotients and
> remainder
>  Example: Convert 99 base 10 to base 8
>
> Step 1.  8 into 99 goes 12 times with 3 left over
> Step 2.  8 into 12 goes 1  time  with 4 left over
> Step 3.  8 into  1 goes 0  times with 1 left over
> Stop when base goes into dividend 0 times
>
> Then line up the leftovers from last to first: 1,4,3
>
> Result 99(base 10) is 143(base 8)
>
> Check: 143(base 8) = 1*8^2 + 4*8^1 + 3*8^0
>                    = 1*64  + 4*8   + 3*1
>                    =   64  + 32    + 3
>                    =   99  (base 10)
> --

give it a shot now?
<META http-equiv=Content-Type content="text/html;
charset=windows-1252">
<SCRIPT language=Javascript>
function anybase() {

fullnumber=document.doit.dnumb.value;
twoparts = fullnumber.split(",");
bas=document.doit.bval.value;

// Main loop, one for naturals and one for fraction part. (if you just
want naturals set loop to one else two)
for(loop=0;loop<2;loop++){
basemultip=1;
multip=0;
unr=0;
decimal=twoparts[loop];

entry=0;
basestr="";

// Number of zero entries in fraction part
while(decimal[entry]==0){
entry++;
}
basestr=entry+",";

//Gettin size of first digit(basemultip) (3,9,27 etc...) and nr of
multip (1,2,3) etc.
while (basemultip<decimal){
basemultip=basemultip*bas;
multip=multip+1;
}

document.doit.trash.value=multip;

//While decimal have value bigger then zero.
while (decimal>0){
unr++;
set=0;
//Here we loop for each basmultip and get the digit while
decimal>0
//Loop Basmultip= 27 i=bas for i= 3-->2-->-->1 [next loop]
basmultip=9 for i= 3-->2-->1 [next loop] basmultiple=3 i=3 for i=3--
>2-->1
//If decimal = subtrahend we add the ***digit=basestring*** in
that position.

for(i=bas;i>0;i--){
subtrahend=basemultip*i;
//******Somewhere here code must change to get NyaN
//******I thought by say that if the bas*basemultip e.g 3*27 equals
decimal 81
//******We can move down multiple by using two IF
(decimal=subtrahend) or IF (decimal>subtrahend) but i can not get it
to work.
//******If subtrahend=decimal) {basemultip=basemultip/bas;}

if (decimal>=subtrahend) {
decimal=decimal-subtrahend;
set=1;
basestr=basestr+i+",";

}
}

if(set==0 && decimal!=0)basestr=basestr+0+",";
basemultip=basemultip/bas;
}
while (multip>=unr){
basestr=basestr+0+",";
unr++;

}

}

controldec=document.doit.fracted.value;
show=document.doit.dnumb.value+"=";
//Control Radix by go back to decimal
darr = control.split(",");
decarr = controldec.split(",");
prezero="";
prezero=decarr[0];

darr.reverse();
decarr.reverse();

total=0;
totalfrac=0;
multiple=1
for (i=1;i<darr.length;i++){
cval=darr[i];
cval=cval*multiple;
total=total+cval;
show=show+"("+multiple+"*"+darr[i]+")+";
multiple=bas*multiple;
}
pre="";
for (i=0;i<prezero;i++){
pre="0"+pre;
}
multiple=1
for (i=1;i<decarr.length-1;i++){
cval=decarr[i];
cval=cval*multiple;
totalfrac=totalfrac+cval;
show=show+"("+multiple+"*"+decarr[i]+")+";
multiple=bas*multiple;
}
totalfrac=pre+totalfrac;
totalfull=total+","+totalfrac;
document.doit.controlout.value=totalfull;
document.doit.overview.value=show;
}

</SCRIPT>
<HTML><BODY>
<FORM NAME=doit onSubmit="anybase(); return false;">
CHANGE BASE:
<input type=submit value="GO!"><P>
DECIMAL VALUE:<TEXTAREA NAME=dnumb COLS=30
ROWS=1>81,33333</TEXTAREA><BR>
BASE:<TEXTAREA NAME=bval COLS=30
ROWS=1>3</TEXTAREA><BR>
First Digit in Radix and Fraction tells proceeding 0's not that
important for N but for fractional digit part.<BR>
ROWS=1></TEXTAREA><BR>
FRACTION OUT:<TEXTAREA NAME=fracted COLS=200
ROWS=1></TEXTAREA><BR>

BACK TO DECIMAL CONTROL CONVERSION->
<TEXTAREA NAME=controlout COLS=30
ROWS=1></TEXTAREA><BR>
ROWS=1></TEXTAREA><BR>
(Programmers)TRASHCAN<TEXTAREA NAME=trash COLS=200
ROWS=1></TEXTAREA><BR>
</FORM>
</BODY>
</HTML>

Date Subject Author
2/3/13 JT
2/3/13 JT
2/3/13 JT
2/3/13 Virgil
2/3/13 JT
2/3/13 JT
2/3/13 JT
2/3/13 JT
2/3/13 JT
2/4/13 JT
2/6/13 JT
2/6/13 Virgil
2/6/13 JT
2/7/13 JT
2/10/13 JT
2/10/13 Virgil
2/22/13 JT
2/23/13 Virgil
2/23/13 JT
2/23/13 JT
2/23/13 JT
2/24/13 JT
2/23/13 JT
2/6/13 JT
2/6/13 JT
2/6/13 JT
2/3/13 JT
2/3/13 JT