Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #27427 > unrolled thread
| Started by | jonas.thornvall@gmail.com |
|---|---|
| First post | 2015-04-30 00:11 -0700 |
| Last post | 2015-04-30 10:52 -0700 |
| Articles | 5 on this page of 105 — 9 participants |
Back to article view | Back to comp.lang.javascript
Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 00:11 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 01:29 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 03:31 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 03:33 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-04-30 13:38 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 08:20 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-04-30 20:37 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 23:46 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 01:01 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 01:47 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-01 10:50 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 02:04 -0700
Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-01 11:08 +0100
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 04:43 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 04:46 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-01 10:48 +0200
Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-01 10:58 +0100
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-01 14:34 +0200
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-01 14:47 +0200
Re: Overflow, integers and strings "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-01 15:04 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 06:18 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 06:24 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 06:49 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 07:00 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 07:17 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 07:21 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 07:37 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 11:27 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 12:34 -0700
Re: Overflow, integers and strings Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-01 22:42 +0200
Re: Overflow, integers and strings "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-01 23:37 +0200
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-02 00:19 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-02 05:50 -0700
Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-02 14:21 +0100
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-03 04:30 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-03 07:12 -0700
Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-03 16:45 +0100
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-03 09:17 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-03 09:22 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 03:38 -0700
Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-04 14:01 +0100
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 06:41 -0700
Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-04 15:39 +0100
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 07:48 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 06:49 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 07:25 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 08:26 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 08:59 -0700
Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-05 00:59 +0100
Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-04 15:28 +0100
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 07:34 -0700
Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-04 15:41 +0100
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 07:52 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 08:03 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 17:56 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 09:04 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 09:21 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 09:38 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 21:07 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 12:25 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 21:43 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 12:46 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 22:11 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 12:53 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 22:23 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 13:40 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 23:12 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 13:06 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 22:39 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 13:42 -0700
Re: Overflow, integers and strings Scott Sauyet <scott.sauyet@gmail.com> - 2015-05-04 13:40 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 13:47 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 23:30 +0200
Re: Overflow, integers and strings "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-04 23:33 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 15:53 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 13:51 -0700
Re: Overflow, integers and strings Scott Sauyet <scott.sauyet@gmail.com> - 2015-05-04 18:00 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 18:22 -0700
Re: Overflow, integers and strings Scott Sauyet <scott.sauyet@gmail.com> - 2015-05-04 21:38 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 18:37 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 14:00 -0700
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 09:19 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 21:24 +0200
Re: Overflow, integers and strings wolfgang zeidler <invalid@invalid.invalid> - 2015-05-04 21:37 +0200
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 21:56 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 13:21 -0700
Re: Overflow, integers and strings wolfgang zeidler <invalid@invalid.invalid> - 2015-05-04 22:38 +0200
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 22:46 +0200
Re: Overflow, integers and strings "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-04 22:57 +0200
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 23:17 +0200
Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-05 00:45 +0100
Re: Overflow, integers and strings "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-06 00:01 +0200
Re: Overflow, integers and strings Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-05-05 08:14 +0200
Re: Overflow, integers and strings "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-05-05 23:54 +0200
Re: Overflow, integers and strings wolfgang zeidler <invalid@invalid.invalid> - 2015-05-04 23:18 +0200
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-04 23:53 +0200
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 16:06 -0700
Re: Overflow, integers and strings "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-05 10:26 +0200
Re: Overflow, integers and strings Dr J R Stockton <reply1500@merlyn.demon.co.uk.invalid> - 2015-05-05 19:59 +0100
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-04 07:40 -0700
Re: Overflow, integers and strings Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-05 01:10 +0100
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-05-01 05:03 -0700
Re: Overflow, integers and strings Ben Bacarisse <ben.lists@bsb.me.uk> - 2015-04-30 20:36 +0100
Re: Overflow, integers and strings jonas.thornvall@gmail.com - 2015-04-30 13:08 -0700
Re: Overflow, integers and strings Scott Sauyet <scott.sauyet@gmail.com> - 2015-04-30 10:52 -0700
Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-05-05 01:10 +0100 |
| Message-ID | <87y4l3dgto.fsf@bsb.me.uk> |
| In reply to | #27486 |
jonas.thornvall@gmail.com writes: > Den måndag 4 maj 2015 kl. 16:28:09 UTC+2 skrev Ben Bacarisse: >> jonas.thornvall@gmail.com writes: >> >> > Den måndag 4 maj 2015 kl. 15:01:44 UTC+2 skrev Ben Bacarisse: >> >> jonas.thornvall@gmail.com writes: >> >> <snip> >> >> > This is only an outline / pseudocoe for how the base conversion would >> >> > look with bignumb implementation will look. You have to check the >> >> > arrays so they contain intergers and not chars, that is probably what >> >> > fail in first loop. >> >> >> >> There is no value in using a bugnum for the base in a bignum >> >> implementation. This whole part of the project is a waste of time >> >> (unless you just want learn ECMAScript programming but that seems >> >> unlikely). >> > >> > I can see that the implementation faster multiplying in base 2048 >> > rather than base 2, although it right now cause overflows if number >> > outside integer scope for all bases other than 10. >> >> I can't follow this. > > There is nothing to follow i say the higher base my implementation use > the faster arithmetic. Of course it could be due to my implementation, > but it isn't > it is due to the weight of the digiplaces. What is the optimum base for a calculation like AxB? Presumably it depends of the sizes of A and B. According to you, as the base increases, the calculation gets faster. But once the base is so large that A, B and AxB are all single digits, making it bigger won't help. This means there is an optimum size. You current argument implies that this optimum base is AxB+1 (or there abouts). The calculation is then just a single digit multiplication, but those digits are bignums themselves. What is the optimum base for *these* bignums? Do you see the problem now? >> > I will fix that though, and that it will becom apparent when >> > multiplying big numbers which approach is faster. >> > >> > Binary base or bignumb base. Does this mean you don't know that a bignum base will be faster? You seemed to be saying that you did know. <snip> -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-05-01 05:03 -0700 |
| Message-ID | <b1d2c8f8-374a-46d3-a3b6-185bdf88dc96@googlegroups.com> |
| In reply to | #27445 |
Den fredag 1 maj 2015 kl. 10:48:07 UTC+2 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 01 mei 2015 in comp.lang.javascript:
> > After comparisson, if the result is smaller then i have to swap the
> > content of the arrays, because the next function call require to be fed
> > in order, next(bigArr,smallArr(.
> >
> > Maybe a bit naive i thought this would work.
> >
> > var tempArr = [];
>
> Not needed!
> "tempArr=subTwo" will make it an array,
> independent of what it was earlier.
>
> Remember a variable name is just a pointer to the internal structure.
>
> > res=arrCompare(subOne,subTwo);
> > if(res==true){tempArr=subTwo; subTwo=subOne;subOne=tempArr;}
>
> (res==true) ?
> Why not just (res) ?
>
> Build and think modular:
>
> function arraySwap(a, b) { // 2 different arrays expected
> var tempArrName = b;
> b = a;
> a = tempArrName;
> // the name tempArrName will be garbagecollected automagically
> };
>
> function arraySwapIfSmaller(a, b) { // arrays by reference
> if (arrCompareSmaller(a, b))
> arraySwap(a, b);
> };
>
> > The subOne to subTwo work just fine,
> > but it seem like tempArrs index not
> > of integer type. When i look at the swapped arrays they look correct.
> > But when i perform arithmetic operations it is obvious that somethings
> > wrong.
> >
> > How can i initialize the indexes of tempArr to be integers?
>
> I don't understand.
>
> What does "initializing an index"
> and "initializing to be integer [type]" mean?
>
> ===================
>
> Array indexes are ALWAYS integers,
> however arrays can also be used as an object:
>
> var a = ['first','second'];
> var b = a;
> a[2.3] = 'not an array member';
> b['blah'] = 'not an array member too';
>
> alert(a.length); // 2
> alert(b.length); // 2
> alert(a.blah); // 'not an array member too'
> alert(b[2.3]); // 'not an array member'
>
> So 1: the array index behavour is not touched
> even when assigning object properties to it.
>
> So 2: there is only one array, with two variable names!
>
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)
Using your code for the swap cause same wrong answer "although again Array look correct", so it is probably something that i do not understand about variables and howto pass parameters.
<SCRIPT LANGUAGE="Javascript">
// Compare numerical size of two "reversed"arrays and their indexes
// If A < B returns true otherwise false
function arrCompare(A, B)
{
var AA=A.length;
var BB=B.length;
if(AA > BB) return false;
if(BB < AA) return true;
//AA = BB, compare indexes from biggest to smallest.
for(i = AA - 1; i >= 0; i -- )
{
if(A[i] < B[i]) return true;
if(A[i] > B[i]) return false;
}
return false;
}
function arraySwap(a, b) { // 2 different arrays expected
var tempArrName = b;
b = a;
a = tempArrName;
// the name tempArrName will be garbagecollected automagically
};
function arraySwapIfSmaller(a, b) { // arrays by reference
if (arrCompareSmaller(a, b))
arraySwap(a, b);
};
//Prepare arrays for subtraction
function prepSubt(subOne,subTwo){
//Check which number bigger, smaller
res=arrCompare(subOne,subTwo);
//If subOne smaller need to swap arrays
//Function naiveSub need to be fed bigger-smaller, parser keep track sign
if(res){arraySwap(subOne,subTwo);}
//Check length of biggest array store bigArr
if(subOne.length<subTwo.length) {
shortArr = subOne.length; bigArr = subTwo.length;
} else {bigArr = subOne.length; shortArr = subTwo.length;}
//Parse with zeros to get same length, and make sure they are integers.
for (i = 0; i < bigArr; 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;
}
document.calc.outbase.value += subOne+ " - \n";
document.calc.outbase.value += subTwo+ "\n";
}
/* SUBTRACT TWO VARIABLES */
/* THE NOTATION BASE TEN, SO FOR INDEX VALUES IF BASE 2000, DIGITVALUE IN INDEX RANGE = (0-1999)*/
/* VARIABLES base INTEGER, subOne, subTwo ARRAYS WITH INDEX VALUES REVERSED */
/* A SINGLE "DIGITPLACE/VALUE" OF BASE, HOLD IN EACH INDEX */
function naiveSub(base, subOne, subTwo)
{
prepSubt(subOne,subTwo);
result=[];
for (i = 0; i < shortArr; i ++ )
{
j = 0;
// document.write(subOne[i],"-",subTwo[i],"First<br>");
if (subOne[i] < subTwo[i])
{
intCarry ++ ;
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];
intSub++;
// document.write(subOne[i],"-",subTwo[i],"=",result[i],"<br>");
}
while(i<bigArr) { result[i]=subOne[i];i++;}
while (result[bigArr - 1] == 0)
{
result.splice(bigArr - 1, 1);
bigArr += - 1;
}
return result;
}
/* RESET FORM FIELDS, FETCH BASE AND STRING TO EVALUATE */
function fetchVal()
{
document.calc.result.value = "";
document.calc.stats.value = "";
document.calc.outbase.value = "";
strEval = document.calc.eval.value;
base = document.calc.formbase.value;
base = parseInt(base);
}
/* SPLIT OPERAND PAIR AND DECIDE OPERATION */
function parsePair()
{
genArr = strEval.split("-");
return genArr;
}
/* IF BASE 10 NO CONVERSION NEEDED "ELSE" DO CONVERSION TO NEWBASE */
function ifBaseTen()
{
if(base != 10)
{
arrOne = newbase(genArr[0], 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;
}
}
/* RESULT OUT TO FORM */
function resOutForm()
{
document.calc.stats.value += "INT Mult operations = " + intMult + "\n";
document.calc.stats.value += "INT Add operations = " + intAdd + "\n";
document.calc.stats.value += "INT Sub operations = " + intSub + "\n";
document.calc.stats.value += "INT Carry operations = " + intCarry + "\n";
tempC = out.reverse();
if(base < 11)
{
tempC = tempC.toString();
tempC = tempC.replace(/,/g, '')
}
document.calc.result.value += " = " + tempC + "\n";
}
/* GLOBAL VARIABLES */
// Arrays holding digitplaces
arrOne = [];
arrTwo = [];
subOne = [];
subTwo = [];
tempA = [];
tempB = [];
tempArr = [];
/* MAIN FUNCTION */
function MAIN()
{
// Counters, adds, subtraction, multiplications
intMult = 0;
intAdd = 0;
intSub = 0;
intCarry = 0;
// Length of the operands
bigArr = 0;
shortArr = 0;
// Fetched variables from form base and string to evaluate
base = 0;
strEval = "";
fetchVal();
genArr = parsePair();
ifBaseTen();
out = naiveSub(base, arrOne, arrTwo);
resOutForm();
}
</SCRIPT>
<!DOCTYPE html>
<html lang="en">
<head><meta charset="utf-8"/></head>
<body bgcolor="gold" onload="MAIN()">
<H3>ARITHMETICS IN ANYBASE USING BIG NUMBERS</H3><P>
<FORM name=calc onSubmit="MAIN(); return false;"><B>
<input type=submit name="calc" value="Calc"> *use radiX* <input type="text" name="formbase" value="10" size="10"><br><BR>
Evaluate <input type="text" name="eval" value="3313-3315" size="300"><br>
Base conv.(rev arrays)<input type="text" name="outbase" value="" size="60"><br>
<FONT COLOR="gray">
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]
| From | Ben Bacarisse <ben.lists@bsb.me.uk> |
|---|---|
| Date | 2015-04-30 20:36 +0100 |
| Message-ID | <877fst8l2q.fsf@bsb.me.uk> |
| In reply to | #27431 |
"Evertjan." <exxjxw.hannivoort@inter.nl.net> writes:
<snip>
> The whole lacks logic, code-beauty and perhaps speed,
> please try:
>
> function arrCompareSmaller(A, B) {
>
> var AA = A.length;
> var BB = B.length;
>
> if(AA > BB)
> return false; // greater
>
> if(AA < BB)
> return true; // smaller
>
> // so: AA == BB
> for(i = AA - 1; i > -1; i-- )
(missing var)
> if(A[i] > B[i])
> return false // greater
> else
> if(A[i] < B[i])
> return true; // smaller
>
> return false; // because is equal
>
> };
You've made the main points I was going to make, but, for what it's
worth, I was going to suggest this:
function arrCompareSmaller(A, B)
{
var alen = A.length, blen = B.length;
if (alen == blen) {
var i = alen - 1;
while (i >= 0 && A[i] == B[i]) --i;
return i >= 0 && A[i] < B[i];
}
else return alen < blen;
}
and remark that the OP seems to be ignoring negative numbers, but I
don't suppose he's bothered by that.
<snip>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-04-30 13:08 -0700 |
| Message-ID | <5e17d940-7791-49bc-8591-b8046fb887bb@googlegroups.com> |
| In reply to | #27438 |
Den torsdag 30 april 2015 kl. 21:36:52 UTC+2 skrev Ben Bacarisse:
> "Evertjan." <exxjxw.hannivoort@inter.nl.net> writes:
> <snip>
> > The whole lacks logic, code-beauty and perhaps speed,
> > please try:
> >
> > function arrCompareSmaller(A, B) {
> >
> > var AA = A.length;
> > var BB = B.length;
> >
> > if(AA > BB)
> > return false; // greater
> >
> > if(AA < BB)
> > return true; // smaller
> >
> > // so: AA == BB
> > for(i = AA - 1; i > -1; i-- )
>
> (missing var)
>
> > if(A[i] > B[i])
> > return false // greater
> > else
> > if(A[i] < B[i])
> > return true; // smaller
> >
> > return false; // because is equal
> >
> > };
>
> You've made the main points I was going to make, but, for what it's
> worth, I was going to suggest this:
>
> function arrCompareSmaller(A, B)
> {
> var alen = A.length, blen = B.length;
>
> if (alen == blen) {
> var i = alen - 1;
> while (i >= 0 && A[i] == B[i]) --i;
> return i >= 0 && A[i] < B[i];
> }
> else return alen < blen;
> }
>
> and remark that the OP seems to be ignoring negative numbers, but I
> don't suppose he's bothered by that.
>
> <snip>
> --
> Ben.
I am hoping that i can deduct the sign from the arithmetic operation at an earlier stage, taken care of parser.
This is just to know which operand is smaller and bigger.
For example subtraction, 11-9 yield same result as 9-11. I just need to keep track of the sign.
I just need to know if any of A or B negative. Same with multiplication the parser take care if A and B negative or if one of them negative.
I can not see any problem with it.
[toc] | [prev] | [next] | [standalone]
| From | Scott Sauyet <scott.sauyet@gmail.com> |
|---|---|
| Date | 2015-04-30 10:52 -0700 |
| Message-ID | <2fa6f537-0ba9-4d15-9b44-e7463940b5f2@googlegroups.com> |
| In reply to | #27427 |
jonas.thornvall@gmail.com wrote:
> Can one compare numeric strings of any size and decide which one
> bigger?
>
> I have a routine to compare anysize numbers read into array, but
> it would be smooth just to be able to compare a numeric string or
> integer of anysize, without function call.
With what tools? You could inline the code from (any non-recursive)
function call, I suppose, wherever you needed to do this, but that
would be ridiculous in the extreme. The language supports numbers
in a fairly simple format. The source-code format in which they're
supplied is well-documented. If you want to do more, and to treat
strings as numbers, you're really going to need your own functions.
There have been several such functions on this thread. They all
seem to have the same issue, which might or might not matter at all
to you. They assume that the data is not only well-structured, but
also normalized. I didn't look carefully, but if I'm not mistaken,
they would all report that "3456" is less than "001234".
Here is a function with a slightly different API that fixes that
issue:
var compare = function(a, b) {
if (!a || !a.length) {
return (b && b.length) ? 1 : 0;
}
if (!b || !b.length) {
return -1;
}
if (!Number(a[0])) {
return compare(a.slice(1), b);
}
if (Number(!b[0])) {
return compare(a, b.slice(1));
}
return a[0] < b[0] ? -1 : a[0] > b[0] ? 1 :
compare(a.slice(1), b.slice(1));
};
But this one is limited to the recursion depth of the underlying ES
engine. One could alter it to make an iterative version if desired.
Note that its API is different. It returns -1 if the first value is
smaller than the second, +1 if it's larger, and 0 if the two values
are equal, much the same as you would do with a comparator passed to
`Array.prototype.sort`. It would be easy enough to wrap this with a
function to generate the API you supplied, if you chose to do so. But
there is a lot to be said for the comparator API.
-- Scott
[toc] | [prev] | [standalone]
Page 6 of 6 — ← Prev page 1 2 3 4 5 [6]
Back to top | Article view | comp.lang.javascript
csiph-web