[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.javascript

Overflow, integers and strings

JT

4/30/2015 7:12:00 AM

Can one compare and decide which integer bigger even if it is bigger than the JavaScript integer?

Can one compare numeric strings of any size and decide which one is 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.

Is it possible?

104 Answers

JT

4/30/2015 8:30:00 AM

0

Den torsdag 30 april 2015 kl. 09:11:40 UTC+2 skrev jonas.t...@gmail.com:
> Can one compare and decide which integer bigger even if it is bigger than the JavaScript integer?
>
> Can one compare numeric strings of any size and decide which one is 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.
>
> Is it possible?

Is it bad logic/programming(basic programming) to break out of loops on conditions, rather then have it as part of loop condition?

I compare two numbers where the digit places read into array index below, comparing from biggest to smallest.

I start with assumption that A is smaller than B true.
But if length of array A bigger than it is false.
Than if they are equal i compare highest digit place to lowest, and break out of loop if the digit place of A < B digit place.

This is the least work version i can come up with, is there better approaches.



// If A < B returns loop = true otherwise loop = false

function arrCompare(A, B, smaller)
{
smaller=true;
document.write(B.length,"<BR>");
AA=A.length;
BB=B.length;
if(AA > BB)
{
smaller = false;
document.write("false 1 <BR>");
}
else if(AA == BB)
{
smaller = false;
for(i = AA - 1; i >= 0; i -- )
{
document.write(A[i]," VS ",B[i],"<BR>");
if(A[i] < B[i])
{
smaller = true;
break;
}
else
{

// smaller = false;

}
}

}
return smaller;
}

JT

4/30/2015 10:32:00 AM

0

Den torsdag 30 april 2015 kl. 10:30:05 UTC+2 skrev jonas.t...@gmail.com:
> Den torsdag 30 april 2015 kl. 09:11:40 UTC+2 skrev jonas.t...@gmail.com:
> > Can one compare and decide which integer bigger even if it is bigger than the JavaScript integer?
> >
> > Can one compare numeric strings of any size and decide which one is 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.
> >
> > Is it possible?
>
> Is it bad logic/programming(basic programming) to break out of loops on conditions, rather then have it as part of loop condition?
>
> I compare two numbers where the digit places read into array index below, comparing from biggest to smallest.
>
> I start with assumption that A is smaller than B true.
> But if length of array A bigger than it is false.
> Than if they are equal i compare highest digit place to lowest, and break out of loop if the digit place of A < B digit place.
>
> This is the least work version i can come up with, is there better approaches.
>
>
>
> // If A < B returns loop = true otherwise loop = false
>
> function arrCompare(A, B, smaller)
> {
> smaller=true;
> document.write(B.length,"<BR>");
> AA=A.length;
> BB=B.length;
> if(AA > BB)
> {
> smaller = false;
> document.write("false 1 <BR>");
> }
> else if(AA == BB)
> {
> smaller = false;
> for(i = AA - 1; i >= 0; i -- )
> {
> document.write(A[i]," VS ",B[i],"<BR>");
> if(A[i] < B[i])
> {
> smaller = true;
> break;
> }
> else
> {
>
> // smaller = false;
>
> }
> }
>
> }
> return smaller;
> }

I need to swap the content of two arrays "when smaller true".

But this approach does not seem to work, one of the swaps work fine the other not, probably because it is not initialised to hold integers.
tempArr just declared as tempArr = [];

if(smaller==true){tempArr=subTwo; subTwo=subOne;subOne=tempArr;}

Any suggestion.

JT

4/30/2015 10:34:00 AM

0

Den torsdag 30 april 2015 kl. 10:30:05 UTC+2 skrev jonas.t...@gmail.com:
> Den torsdag 30 april 2015 kl. 09:11:40 UTC+2 skrev jonas.t...@gmail.com:
> > Can one compare and decide which integer bigger even if it is bigger than the JavaScript integer?
> >
> > Can one compare numeric strings of any size and decide which one is 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.
> >
> > Is it possible?
>
> Is it bad logic/programming(basic programming) to break out of loops on conditions, rather then have it as part of loop condition?
>
> I compare two numbers where the digit places read into array index below, comparing from biggest to smallest.
>
> I start with assumption that A is smaller than B true.
> But if length of array A bigger than it is false.
> Than if they are equal i compare highest digit place to lowest, and break out of loop if the digit place of A < B digit place.
>
> This is the least work version i can come up with, is there better approaches.
>
>
>
> // If A < B returns loop = true otherwise loop = false
>
> function arrCompare(A, B, smaller)
> {
> smaller=true;
> document.write(B.length,"<BR>");
> AA=A.length;
> BB=B.length;
> if(AA > BB)
> {
> smaller = false;
> document.write("false 1 <BR>");
> }
> else if(AA == BB)
> {
> smaller = false;
> for(i = AA - 1; i >= 0; i -- )
> {
> document.write(A[i]," VS ",B[i],"<BR>");
> if(A[i] < B[i])
> {
> smaller = true;
> break;
> }
> else
> {
>
> // smaller = false;
>
> }
> }
>
> }
> return smaller;
> }

A little better.

// If A < B returns loop = true otherwise loop = false

function arrCompare(A, B, smaller)
{
smaller=true;
//document.write(B.length,"<BR>");
AA=A.length;
BB=B.length;
if(AA > BB)
{
smaller = false;

}
else if(AA == BB)
{
smaller=false;
for(i = AA - 1; i >= 0; i -- )
{
if(A[i] < B[i])
{
smaller = true;
break;
}
}
}
return smaller;
}

Evertjan.

4/30/2015 11:38:00 AM

0

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

> function arrCompare(A, B, smaller)

Illogical to have smaller as an input parameter,
as you overwrite it immediately here:

> {
> smaller=true;

Remember this parameter is by value, not by reference.

The arrays A and B parameters are by reference!

> AA=A.length;
> BB=B.length;

use var !!!

> if(AA > BB)
> {
> smaller = false;

why not return here?

> }
> else if(AA == BB)
> {
> smaller=false;
> for(i = AA - 1; i >= 0; i -- )
> {
> if(A[i] < B[i])
> {
> smaller = true;

why not return here, breaking inherently?

> break;
> }
> }
> }
> return smaller;

you know it smaller = false here!

>}

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-- )
if(A[i] > B[i])
return false // greater
else
if(A[i] < B[i])
return true; // smaller

return false; // because is equal

};

However I would suggest to retain
the three way result,
which you probably need anyway sometime.
If you only need smaller do this:
"if (arrCompareThreeway(A, B) == -1) ..."


function arrCompareThreeway(A, B) {

var AA = A.length;
var BB = B.length;

if(AA > BB)
return 1; // greater

if(AA < BB)
return -1; // smaller

for(i = AA - 1; i > -1; i-- )
if(A[i] > B[i])
return 1 // greater
else
if(A[i] < B[i])
return -1; // smaller

return 0; // equal

};


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

JT

4/30/2015 3:21:00 PM

0

Den torsdag 30 april 2015 kl. 13:38:14 UTC+2 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 30 apr 2015 in comp.lang.javascript:
>
> > function arrCompare(A, B, smaller)
>
> Illogical to have smaller as an input parameter,
> as you overwrite it immediately here:
>
> > {
> > smaller=true;
>
> Remember this parameter is by value, not by reference.
>
> The arrays A and B parameters are by reference!
>
> > AA=A.length;
> > BB=B.length;
>
> use var !!!
>
> > if(AA > BB)
> > {
> > smaller = false;
>
> why not return here?

What is the difference, the else leg will be skipped if AA>BB?

Does it send the return value faster?


>
> > }
> > else if(AA == BB)
> > {
> > smaller=false;
> > for(i = AA - 1; i >= 0; i -- )
> > {
> > if(A[i] < B[i])
> > {
> > smaller = true;
>
> why not return here, breaking inherently?
Is there a difference between doing the return here or at end?

> > break;
> > }
> > }
> > }
> > return smaller;
>
> you know it smaller = false here!

No??? smaller could be set to true or false above before returned?

> >}
>
> 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

This i sure get you step out from function directly when false.
So below will not be executed.

> if(AA < BB)
> return true; // smaller

This is the same as above but when true, i get it.

> // so: AA == BB
> for(i = AA - 1; i > -1; i-- )
> if(A[i] > B[i])

First i thought it was wrong but now i see it is corrett you loop thru while equal and break at bigger or smaller. Now i wonder if my code really worked, but i am pretty sure i checked... It probably did since i only interested in smaller case, but cause a longer for loop.

You are correct Jan no need loop thru until true.

> return false // greater
> else
> if(A[i] < B[i])
> return true; // smaller
>
> return false; // because is equal
>
> };
>
> However I would suggest to retain
> the three way result,
> which you probably need anyway sometime.
> If you only need smaller do this:
> "if (arrCompareThreeway(A, B) == -1) ..."
>
>
> function arrCompareThreeway(A, B) {
>
> var AA = A.length;
> var BB = B.length;
>
> if(AA > BB)
> return 1; // greater
>
> if(AA < BB)
> return -1; // smaller
>
> for(i = AA - 1; i > -1; i-- )
> if(A[i] > B[i])
> return 1 // greater
> else
> if(A[i] < B[i])
> return -1; // smaller
>
> return 0; // equal
>
> };
>
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

Scott Sauyet

4/30/2015 5:53:00 PM

0

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

Evertjan.

4/30/2015 6:38:00 PM

0

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

> This is the same as above but when true, i get it.
>
>> // so: AA == BB
>> for(i = AA - 1; i > -1; i-- )
>> if(A[i] > B[i])
>
> First i thought it was wrong but now i see it is corrett you loop thru
> while equal and break at bigger or smaller. Now i wonder if my code
> really worked, but i am pretty sure i checked... It probably did since i
> only interested in smaller case, but cause a longer for loop.
>
> You are correct Jan no need loop thru until true.

Not my name.

>> return false // greater
>> else
>> if(A[i] < B[i])
>> return true; // smaller
>>
>> return false; // because is equal
>>
>> };

Methinks my code is more sparse,
and perhaps quicker the larger the difference,
equality is slowest,
so if you expect that to be most common
you could provide for that.

Methinks the beauty of the KISS-code
should reflect the basic threeway logic:

if array length difference -> exit with result
if most significant member difference -> exit with result
repeat this for all less significant members -> exit with result
no exit yet, so must be equal -> exit equal

only then you start writing code.

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

Just for fun, you could empty the arrays in the process:

function arrCompareThreeway(A, B) {
var temp;
if ((temp = Math.sign(B.length - A.length)) != 0) return temp;
do ; while (temp = Math.sign(A.pop() - B.pop()));
return (isNaN(temp))?0 :temp ;
};




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

Ben Bacarisse

4/30/2015 7:37:00 PM

0

"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.

JT

4/30/2015 8:09:00 PM

0

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.

JT

5/1/2015 6:47:00 AM

0

Den torsdag 30 april 2015 kl. 20:37:44 UTC+2 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 30 apr 2015 in comp.lang.javascript:
>
> > This is the same as above but when true, i get it.
> >
> >> // so: AA == BB
> >> for(i = AA - 1; i > -1; i-- )
> >> if(A[i] > B[i])
> >
> > First i thought it was wrong but now i see it is corrett you loop thru
> > while equal and break at bigger or smaller. Now i wonder if my code
> > really worked, but i am pretty sure i checked... It probably did since i
> > only interested in smaller case, but cause a longer for loop.
> >
> > You are correct Jan no need loop thru until true.
>
> Not my name.
>
> >> return false // greater
> >> else
> >> if(A[i] < B[i])
> >> return true; // smaller
> >>
> >> return false; // because is equal
> >>
> >> };
>
> Methinks my code is more sparse,
> and perhaps quicker the larger the difference,
> equality is slowest,
> so if you expect that to be most common
> you could provide for that.
>
> Methinks the beauty of the KISS-code
> should reflect the basic threeway logic:
>
> if array length difference -> exit with result
> if most significant member difference -> exit with result
> repeat this for all less significant members -> exit with result
> no exit yet, so must be equal -> exit equal
>
> only then you start writing code.
>
> =======================
>
> Just for fun, you could empty the arrays in the process:
>
> function arrCompareThreeway(A, B) {
> var temp;
> if ((temp = Math.sign(B.length - A.length)) != 0) return temp;
> do ; while (temp = Math.sign(A.pop() - B.pop()));
> return (isNaN(temp))?0 :temp ;
> };
>
>
>
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

Hello EvertJan thank you for help.

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 = [];
res=arrCompare(subOne,subTwo);
if(res==true){tempArr=subTwo; subTwo=subOne;subOne=tempArr;}

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?