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>