Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27427 > unrolled thread

Overflow, integers and strings

Started byjonas.thornvall@gmail.com
First post2015-04-30 00:11 -0700
Last post2015-04-30 10:52 -0700
Articles 20 on this page of 105 — 9 participants

Back to article view | Back to comp.lang.javascript


Contents

  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 1 of 6  [1] 2 3 4 5 6  Next page →


#27427 — Overflow, integers and strings

Fromjonas.thornvall@gmail.com
Date2015-04-30 00:11 -0700
SubjectOverflow, integers and strings
Message-ID<e7aac5c2-b226-44f4-a77f-fa284c3877ba@googlegroups.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?

[toc] | [next] | [standalone]


#27428

Fromjonas.thornvall@gmail.com
Date2015-04-30 01:29 -0700
Message-ID<09495c64-e320-46ca-a6d9-b9f0427c7efd@googlegroups.com>
In reply to#27427
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;
}

[toc] | [prev] | [next] | [standalone]


#27429

Fromjonas.thornvall@gmail.com
Date2015-04-30 03:31 -0700
Message-ID<76bc4399-93af-4b42-a52a-c8bbc0037131@googlegroups.com>
In reply to#27428
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.

[toc] | [prev] | [next] | [standalone]


#27430

Fromjonas.thornvall@gmail.com
Date2015-04-30 03:33 -0700
Message-ID<66061bf2-9092-42a5-918e-5a039a53db20@googlegroups.com>
In reply to#27428
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;
}

[toc] | [prev] | [next] | [standalone]


#27431

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-04-30 13:38 +0200
Message-ID<XnsA48C8AB5EE74Ceejj99@194.109.133.246>
In reply to#27430
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)

[toc] | [prev] | [next] | [standalone]


#27432

Fromjonas.thornvall@gmail.com
Date2015-04-30 08:20 -0700
Message-ID<6ca07e69-1694-49c1-93eb-748c44d8ab68@googlegroups.com>
In reply to#27431
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)

[toc] | [prev] | [next] | [standalone]


#27437

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-04-30 20:37 +0200
Message-ID<XnsA48CD1D512C3Beejj99@194.109.133.246>
In reply to#27432
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)

[toc] | [prev] | [next] | [standalone]


#27442

Fromjonas.thornvall@gmail.com
Date2015-04-30 23:46 -0700
Message-ID<b378aebc-5acc-4b88-9050-6b812221cba9@googlegroups.com>
In reply to#27437
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?

[toc] | [prev] | [next] | [standalone]


#27443

Fromjonas.thornvall@gmail.com
Date2015-05-01 01:01 -0700
Message-ID<0b64da34-8f3d-48f6-b522-6dccc2facc94@googlegroups.com>
In reply to#27442
Den fredag 1 maj 2015 kl. 08:47:00 UTC+2 skrev jonas.t...@gmail.com:
> 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?

<SCRIPT LANGUAGE="Javascript">

// Compare numerical size of two "reversed"arrays an their index
// If A < B returns true otherwise false

function arrCompare(A, B)
{  
   AA=A.length;
   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;
}

//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 fed bigger-smaller, parser keep track sign
if(res==true){tempArr=subTwo; subTwo=subOne;subOne=tempArr;}

//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 < bigArr; 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>"); 
  }
// p.stop();
   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" PREPAIR FOR CONVERSION */

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]


#27444

Fromjonas.thornvall@gmail.com
Date2015-05-01 01:47 -0700
Message-ID<1367f180-5580-4d27-b68a-9a6851d548d7@googlegroups.com>
In reply to#27443
Den fredag 1 maj 2015 kl. 10:01:47 UTC+2 skrev jonas.t...@gmail.com:
> Den fredag 1 maj 2015 kl. 08:47:00 UTC+2 skrev jonas.t...@gmail.com:
> > 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?
> 
> <SCRIPT LANGUAGE="Javascript">
> 
> // Compare numerical size of two "reversed"arrays an their index
> // If A < B returns true otherwise false
> 
> function arrCompare(A, B)
> {  
>    AA=A.length;
>    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;
> }
> 
> //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 fed bigger-smaller, parser keep track sign
> if(res==true){tempArr=subTwo; subTwo=subOne;subOne=tempArr;}
> 
> //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 < bigArr; 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>"); 
>   }
> // p.stop();
>    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" PREPAIR FOR CONVERSION */
> 
> 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>

As you can see the swap of arrays do not work.
My gut feeling telling me that the check size check, probably should be done while still strings fetched from form. And then always put the bigger numeric string into arrOne and the smaller to arrTwo. 

Is there a way to compare numerical values in strings of anysize? Well i can use strlen of coursem but howto compare the unicode content the two strings string hold? 

[toc] | [prev] | [next] | [standalone]


#27446

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-05-01 10:50 +0200
Message-ID<XnsA48D6E35828E8eejj99@194.109.133.246>
In reply to#27443
jonas.thornvall@gmail.com wrote on 01 mei 2015 in comp.lang.javascript:

> /* THE NOTATION BASE TEN, SO FOR INDEX VALUES IF BASE 2000, DIGITVALUE
> IN INDEX RANGE = (0-1999)*/ 
> 

Please do not shout on usenet.

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

[toc] | [prev] | [next] | [standalone]


#27447

Fromjonas.thornvall@gmail.com
Date2015-05-01 02:04 -0700
Message-ID<d8c85946-4277-4f97-a3c7-1e9d9d6229df@googlegroups.com>
In reply to#27446
Den fredag 1 maj 2015 kl. 10:50:37 UTC+2 skrev Evertjan.:
> jonas.thornvall@gmail.com wrote on 01 mei 2015 in comp.lang.javascript:
> 
> > /* THE NOTATION BASE TEN, SO FOR INDEX VALUES IF BASE 2000, DIGITVALUE
> > IN INDEX RANGE = (0-1999)*/ 
> > 
> 
> Please do not shout on usenet.
> 
> -- 
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)

Sorry about that Evertjan, if you run script i posted you can under the subtraction to evaluate see "the content of the arrays" before subtraction.

If you write 2015-2013 it will write out Array 5,1,0,2 - 3,1,0,2 as expected.
If you write 2013-2015 it will swap arrays and also write out Array 5,1,0,2 - 3,1,0,2 as expected because swapped.


The result differ though, the one above yield 2 as should answer the one below yield 9998, but should yield 2 (parser will hold signs so that is not needed)

And as you see the array input to naiveSub is the same both examples, so there must be something about the format of the indexes?

[toc] | [prev] | [next] | [standalone]


#27449

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-05-01 11:08 +0100
Message-ID<87vbgc7gqk.fsf@bsb.me.uk>
In reply to#27443
jonas.thornvall@gmail.com writes:
<snip>
> // Compare numerical size of two "reversed"arrays an their index
> // If A < B returns true otherwise false
>
> function arrCompare(A, B)
> {  
>    AA=A.length;
>    BB=B.length;

A few people have pointed out that using global names like this is a bad
idea.  Typing "var" is not very much effort so I wonder why you are
still doing this?  Do you not believe that it's a bad idea?

<snip>
> function naiveSub(base, subOne, subTwo)
> {
>    prepSubt(subOne,subTwo);
>    result=[];
>    for (i = 0; i < bigArr; i ++ )

Using globals like this is even worse because the reader has to go and
hunt down where and to what value bigArr was set.  You should write
functions that a self-contained and do one simple task each.

By the way, mixing up the string conversion and the subtraction is also
a bad idea.  Once you have a number in your internal array format, there
is no need to keep calling parseInt on the elements before every
subtraction.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#27450

Fromjonas.thornvall@gmail.com
Date2015-05-01 04:43 -0700
Message-ID<4643cc8d-9d12-4824-926b-997c7982539b@googlegroups.com>
In reply to#27449
Den fredag 1 maj 2015 kl. 12:08:07 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
> <snip>
> > // Compare numerical size of two "reversed"arrays an their index
> > // If A < B returns true otherwise false
> >
> > function arrCompare(A, B)
> > {  
> >    AA=A.length;
> >    BB=B.length;
> 
> A few people have pointed out that using global names like this is a bad
> idea.  Typing "var" is not very much effort so I wonder why you are
> still doing this?  Do you not believe that it's a bad idea?
> 
> <snip>
> > function naiveSub(base, subOne, subTwo)
> > {
> >    prepSubt(subOne,subTwo);
> >    result=[];
> >    for (i = 0; i < bigArr; i ++ )
> 
> Using globals like this is even worse because the reader has to go and
> hunt down where and to what value bigArr was set.  You should write
> functions that a self-contained and do one simple task each.
> 
> By the way, mixing up the string conversion and the subtraction is also
> a bad idea.  Once you have a number in your internal array format, there
> is no need to keep calling parseInt on the elements before every
> subtraction.
> 
> <snip>
> -- 
> Ben.

I know my programming is not up to standard and very rudimentary. I will try to fix code after your suggestions. And at same time improve efficiency.
I am really a bit clueless by howto make shortArr and bigArr local, without invoke the checkcode into naiveSub. And i was hoping to keep the checks outside the actual arithmetic.

Now i only add while not zero in the subtrahend.

function naiveSub(base, subOne, subTwo)
{
   prepSubt(subOne,subTwo);
   result=[];
   for (i = 0; i < shortArr; i ++ )
   {
      j = 0;
      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++;
 
  }
   while(i<bigArr) { result[i]=subOne[i];i++;}
   while (result[bigArr - 1] == 0)
   {
      result.splice(bigArr - 1, 1);
      bigArr += - 1;
   }
   return result;
}

[toc] | [prev] | [next] | [standalone]


#27451

Fromjonas.thornvall@gmail.com
Date2015-05-01 04:46 -0700
Message-ID<fb465478-3931-424a-92d9-5b2df3f02418@googlegroups.com>
In reply to#27450
Den fredag 1 maj 2015 kl. 13:44:02 UTC+2 skrev jonas.t...@gmail.com:
> Den fredag 1 maj 2015 kl. 12:08:07 UTC+2 skrev Ben Bacarisse:
> > jonas.thornvall@gmail.com writes:
> > <snip>
> > > // Compare numerical size of two "reversed"arrays an their index
> > > // If A < B returns true otherwise false
> > >
> > > function arrCompare(A, B)
> > > {  
> > >    AA=A.length;
> > >    BB=B.length;
> > 
> > A few people have pointed out that using global names like this is a bad
> > idea.  Typing "var" is not very much effort so I wonder why you are
> > still doing this?  Do you not believe that it's a bad idea?
> > 
> > <snip>
> > > function naiveSub(base, subOne, subTwo)
> > > {
> > >    prepSubt(subOne,subTwo);
> > >    result=[];
> > >    for (i = 0; i < bigArr; i ++ )
> > 
> > Using globals like this is even worse because the reader has to go and
> > hunt down where and to what value bigArr was set.  You should write
> > functions that a self-contained and do one simple task each.
> > 
> > By the way, mixing up the string conversion and the subtraction is also
> > a bad idea.  Once you have a number in your internal array format, there
> > is no need to keep calling parseInt on the elements before every
> > subtraction.
> > 
> > <snip>
> > -- 
> > Ben.
> 
> I know my programming is not up to standard and very rudimentary. I will try to fix code after your suggestions. And at same time improve efficiency.
> I am really a bit clueless by howto make shortArr and bigArr local, without invoke the checkcode into naiveSub. And i was hoping to keep the checks outside the actual arithmetic.
> 
> Now i only add while not zero in the subtrahend.
> 
> function naiveSub(base, subOne, subTwo)
> {
>    prepSubt(subOne,subTwo);
>    result=[];
>    for (i = 0; i < shortArr; i ++ )
>    {
>       j = 0;
>       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++;
>  
>   }
>    while(i<bigArr) { result[i]=subOne[i];i++;}
>    while (result[bigArr - 1] == 0)
>    {
>       result.splice(bigArr - 1, 1);
>       bigArr += - 1;
>    }
>    return result;
> }

Sorry subtract while not zero in subtrahend, rest is copied to result.
Is it correct approach to keep a separate result array or should i subtract directly upon the minuend array?

[toc] | [prev] | [next] | [standalone]


#27445

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-05-01 10:48 +0200
Message-ID<XnsA48D6DDF66B5Feejj99@194.109.133.246>
In reply to#27442
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)

[toc] | [prev] | [next] | [standalone]


#27448

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-05-01 10:58 +0100
Message-ID<871tj08vrb.fsf@bsb.me.uk>
In reply to#27445
"Evertjan." <exxjxw.hannivoort@inter.nl.net> writes:
<snip>
> 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);
> };

I don't think this does what you think it does.  arraySwap has no effect
on the arrays that are passed as parameters.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#27453

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-05-01 14:34 +0200
Message-ID<XnsA48D942F1A7FFeejj99@194.109.133.246>
In reply to#27448
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote on 01 mei 2015 in 
comp.lang.javascript:

>>       arraySwap(a, b);

> 
> I don't think this does what you think it does.  arraySwap has no effect
> on the arrays that are passed as parameters.

Right you are, pity.

'this' works:

======================================
<script type='text/javascript'>

var a1 = ['first','array'];
var a2 = ['second-array'];

document.write('<br>a1: '+ a1); 
document.write('<br>a2: '+ a2);
document.write('<br>swap');

function arraySwap(x,y) {
   var tempArrName = this[y]; 
   this[y] = this[x];
   this[x] = tempArrName;
}

arraySwap('a1','a2')

document.write('<br>a1: '+ a1); 
document.write('<br>a2: '+ a2);

</script>
======================================
 
-- 
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

[toc] | [prev] | [next] | [standalone]


#27454

From"Evertjan." <exxjxw.hannivoort@inter.nl.net>
Date2015-05-01 14:47 +0200
Message-ID<XnsA48D967FD5951eejj99@194.109.133.246>
In reply to#27453
"Evertjan." <exxjxw.hannivoort@inter.nl.net> wrote on 01 mei 2015 in 
comp.lang.javascript:

> function arraySwap(x,y) {
>    var tempArrName = this[y]; 
>    this[y] = this[x];
>    this[x] = tempArrName;
>}

Hey, this swaps also simple values and even objects.

I'll rename it to: swapByRef()

<script type='text/javascript'>

var a1 = {x:'one'};
var a2 = {x:'two'};

document.write('<br>a1.x: '+ a1.x); 
document.write('<br>a2.x: '+ a2.x);
document.write('<br>swap objects');

function swapByRef(x,y) {
   var tempArrName = this[y]; 
   this[y] = this[x];
   this[x] = tempArrName;
}

swapByRef('a1','a2')

document.write('<br>a1.x: '+ a1.x); 
document.write('<br>a2.x: '+ a2.x);

</script>




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

[toc] | [prev] | [next] | [standalone]


#27455

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2015-05-01 15:04 +0200
Message-ID<mhvtku$rh2$1@solani.org>
In reply to#27453
Evertjan. wrote:

> 'this' works:

Not necessarily:

    function test() {

      var a = [1], b = [2];

      function arraySwap(x,y) {
        var tempArrName = this[y];
        this[y] = this[x];
        this[x] = tempArrName;
      }

      console.log(a, b);
      arraySwap(a, b);
      console.log(a, b);
    }

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


Page 1 of 6  [1] 2 3 4 5 6  Next page →

Back to top | Article view | comp.lang.javascript


csiph-web