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


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

Optimise adder

Started byjonas.thornvall@gmail.com
First post2015-05-21 07:19 -0700
Last post2015-06-06 05:33 -0700
Articles 1 on this page of 41 — 5 participants

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


Contents

  Optimise adder jonas.thornvall@gmail.com - 2015-05-21 07:19 -0700
    Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-21 16:01 +0100
      Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-21 08:43 -0700
      Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-21 08:54 -0700
        Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-21 17:04 +0100
          Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-21 09:13 -0700
            Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-21 20:01 +0100
              Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-21 12:39 -0700
                Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-21 21:43 +0100
              Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-21 12:52 -0700
                Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-21 21:40 +0100
                  Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-21 14:30 -0700
                    Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-22 00:10 +0100
                      Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-21 23:20 -0700
                        Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-22 10:45 +0100
                          Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-22 02:56 -0700
                            Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-22 11:25 +0100
                              Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-22 04:37 -0700
                                Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-22 13:21 +0100
                              Re: Optimise adder Denis McMahon <denismfmcmahon@gmail.com> - 2015-05-22 18:14 +0000
                      Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-21 23:24 -0700
                        Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-22 01:18 -0700
                          Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-22 10:41 +0100
                            Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-22 02:52 -0700
                              Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-22 13:17 +0100
                                Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-22 05:59 -0700
                                  Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-22 17:37 +0100
                                    Re: Optimise adder Denis McMahon <denismfmcmahon@gmail.com> - 2015-05-22 18:17 +0000
                                      Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-22 12:58 -0700
                                  Re: Optimise adder "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-23 00:08 +0200
                        Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-22 10:38 +0100
        Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-21 09:05 -0700
          Re: Optimise adder Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-21 20:03 +0100
    Re: Optimise adder "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-21 22:51 +0200
      Re: Optimise adder jonas.thornvall@gmail.com - 2015-05-21 15:26 -0700
        Re: Optimise adder "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-05-22 13:40 +0200
        Re: Optimise adder Scott Sauyet <scott.sauyet@gmail.com> - 2015-05-26 06:20 -0700
          Re: Optimise adder jonas.thornvall@gmail.com - 2015-06-06 04:01 -0700
            Re: Optimise adder jonas.thornvall@gmail.com - 2015-06-06 04:09 -0700
              Re: Optimise adder jonas.thornvall@gmail.com - 2015-06-06 04:15 -0700
                Re: Optimise adder jonas.thornvall@gmail.com - 2015-06-06 05:33 -0700

Page 3 of 3 — ← Prev page 1 2 [3]


#28004

Fromjonas.thornvall@gmail.com
Date2015-06-06 05:33 -0700
Message-ID<add64b1d-2081-4e2c-9c45-d7a34879a662@googlegroups.com>
In reply to#28003
Den lördag 6 juni 2015 kl. 13:15:14 UTC+2 skrev jonas.t...@gmail.com:
> Den lördag 6 juni 2015 kl. 13:09:11 UTC+2 skrev jonas.t...@gmail.com:
> > Den lördag 6 juni 2015 kl. 13:02:03 UTC+2 skrev jonas.t...@gmail.com:
> > > Den tisdag 26 maj 2015 kl. 15:20:22 UTC+2 skrev Scott Sauyet:
> > > > jonas.thornvall@gmail.com wrote:
> > > > > Evertjan wrote:
> > > > > jonas.thornvall@gmail.com wrote:
> > > > 
> > > > >>> Addresult holds the result of the integer adds.
> > > > >> 
> > > > >> Seems you got the idea of "containers" holding a value.
> > > > > 
> > > > > Maybe when you finished blather nonsense maybe you could make a 
> > > > > version that is faster?
> > > > 
> > > > 
> > > > You haven't been willing to listen to anyone's criticism of your 
> > > > techniques or your overall goals.  No one is likely to want to help
> > > > optimize the sort of code you've been posting.
> > > >  
> > > > > I feel though that Scott is the only one to cume up with something
> > > > > significantly better.
> > > > 
> > > > I haven't followed closely enough to know if I'm the Scott you mention.
> > > > I certainly believe I could write code for base conversion and BigInteger
> > > > arithmetic that works well and is cleaner and more comprehensible than
> > > > what you've been posting.  But a number of regulars in this group for
> > > > whom I hold varying degrees of respect have also posted here, and I have
> > > > no doubt that any one of them could also write such code.  It's simply 
> > > > not a difficult problem.
> > > > 
> > > > But you come into this group, refuse to pay attention to its conventions,
> > > > post reams of unreadable code, insult people who disagree with you, whine
> > > > about no one helping with your problems, start numerous threads for the
> > > > same material, and generally make a nuisance of yourself.  Do you see why
> > > > most of the threads you start end up going nowhere useful?
> > > > 
> > > >   -- Scott
> > > 
> > > Had a look and fixed the adder for when operands of different lengths. I think it take care of the possible cases. And decided that the result array was not necessary i add to the biggest operands array.
> > > 
> > > <script language="Javascript">
> > > 
> > > /* ADD TWO THE TWO VARIABLES STORED IN ADDONE AND ADDTWO USING CHOSEN BASE */
> > > 
> > > function naiveAddB(base, addOne, addTwo)
> > > {
> > >    abigArr = addOne.length;
> > >    asmallArr = addTwo.length;
> > >    addResult = [];
> > >    remainder = 0;
> > >    for (i = 0; i < asmallArr+1; i ++ )
> > >    {
> > >       if (isNaN(addTwo[i])) addTwo[i] = 0;
> > >       addOne[i] = addOne[i] + addTwo[i] + remainder;
> > >       if (addOne[i] >= base)
> > >       {  
> > >          
> > >          addOne[i] = addOne[i] - base;
> > >          remainder = 1;
> > >       }
> > >       else
> > >       {
> > >          remainder = 0;
> > >       }
> > >    }
> > > 
> > >    // If strings of equal length but there is a remainder;
> > >    while (remainder == 1 && addOne[i]==9)
> > >    {
> > >       addOne[i] = 0;
> > >       i++;
> > >    }
> > >    if (remainder == 1) addOne[i] = addOne[i]+1;
> > >    return addOne;
> > > }
> > > 
> > > base=10;
> > > var arrOne = [1,6,1,9,9,9,5,6,7];
> > > var arrTwo = [1,3,9];
> > > 
> > > /* TIMER START */
> > > var start = new Date().getTime();
> > > for(j=1;j<2;j++) {
> > > addResult=naiveAddB(base,arrOne,arrTwo);
> > > }
> > > var end = new Date().getTime();
> > > var time = end - start;
> > > document.write(addResult,"<BR>"," Timing = ", time, " ms");
> > > 
> > > </script>
> > 
> > Managed to hardcode the base. This seems more correct.
> > 
> > <script language="Javascript">
> > 
> > /* ADD TWO THE TWO VARIABLES STORED IN THE REVERSED ARRAYS ADDONE AND ADDTWO USING CHOSEN BASE */
> > 
> > function naiveAddB(base, addOne, addTwo)
> > {
> >    abigArr = addOne.length;
> >    asmallArr = addTwo.length;
> >    addResult = [];
> >    remainder = 0;
> >    for (i = 0; i < asmallArr+1; i ++ )
> >    {
> >       if (isNaN(addTwo[i])) addTwo[i] = 0;
> >       addOne[i] = addOne[i] + addTwo[i] + remainder;
> >       if (addOne[i] >= base)
> >       {  
> >          
> >          addOne[i] = addOne[i] - base;
> >          remainder = 1;
> >       }
> >       else
> >       {
> >          remainder = 0;
> >       }
> >    }
> > 
> >    // If strings of equal length but there is a remainder;
> >    while (remainder == 1 && addOne[i]==(base-1))
> >    {
> >       addOne[i] = 0;
> >       i++;
> >    }
> >    if (remainder == 1) addOne[i] = addOne[i]+1;
> >    return addOne;
> > }
> > 
> > base=10;
> > var arrOne = [1,6,1,9,9,9,5,6,7];
> > var arrTwo = [1,3,9];
> > 
> > /* TIMER START */
> > var start = new Date().getTime();
> > for(j=1;j<2;j++) {
> > addResult=naiveAddB(base,arrOne,arrTwo);
> > }
> > var end = new Date().getTime();
> > var time = end - start;
> > document.write(addResult,"<BR>"," Timing = ", time, " ms");
> > 
> > </script>
> 
> Forgot to check is nan for leading digit.
> 
> <script language="Javascript">
> 
> /* ADD TWO THE TWO VARIABLES STORED IN THE REVERSED ARRAYS ADDONE AND ADDTWO USING CHOSEN BASE */
> 
> function naiveAddB(base, addOne, addTwo)
> {
>    abigArr = addOne.length;
>    asmallArr = addTwo.length;
>    addResult = [];
>    remainder = 0;
>    for (i = 0; i < asmallArr+1; i ++ )
>    {
>       if (isNaN(addTwo[i])) addTwo[i] = 0;
>       addOne[i] = addOne[i] + addTwo[i] + remainder;
>       if (addOne[i] >= base)
>       {  
>          
>          addOne[i] = addOne[i] - base;
>          remainder = 1;
>       }
>       else
>       {
>          remainder = 0;
>       }
>    }
> 
>    // If strings of equal length but there is a remainder;
>    while (remainder == 1 && addOne[i]==(base-1))
>    {
>       addOne[i] = 0;
>       i++;
>    }
>    if (remainder == 1) {if (isNaN(addOne[i])) addOne[i] = 0; addOne[i] = addOne[i]+1;}
>    return addOne;
> }
> 
> base=10;
> var arrOne = [1,6,1,9,9,9,9,9,9];
> var arrTwo = [1,3,9];
> 
> /* TIMER START */
> var start = new Date().getTime();
> for(j=1;j<2;j++) {
> addResult=naiveAddB(base,arrOne,arrTwo);
> }
> var end = new Date().getTime();
> var time = end - start;
> document.write(addResult,"<BR>"," Timing = ", time, " ms");
> 
> </script>

Was a bug this is it...

<script language="Javascript">

/* ADD TWO THE TWO VARIABLES STORED IN THE REVERSED ARRAYS ADDONE AND ADDTWO USING CHOSEN BASE */

function naiveAddB(base, addOne, addTwo)
{
   abigArr = addOne.length;
   asmallArr = addTwo.length;
   addResult = [];
   remainder = 0;
   for (i = 0; i < asmallArr; i ++ )
   {
      if (isNaN(addTwo[i])) addTwo[i] = 0;
      if (isNaN(addOne[i])) addOne[i] = 0; 
      addOne[i] = addOne[i] + addTwo[i] + remainder;
      if (addOne[i] >= base)
      {  
         
         addOne[i] = addOne[i] - base;
         remainder = 1;
      
      }
      else
      {
         remainder = 0;
      }
   }
 
 
   // If strings of equal length but there is a remainder;
   while (remainder == 1 && addOne[i]==(base-1))
   {
      addOne[i] = 0;
      i++;
   }
 
   if (remainder == 1) {if (isNaN(addOne[i])) addOne[i] = 0; addOne[i] = addOne[i]+1;}
   return addOne;
}

base=10;
var arrOne = [9,3,9];
var arrTwo = [1,6,4];
/* TIMER START */
var start = new Date().getTime();
for(j=1;j<2;j++) {
addResult=naiveAddB(base,arrOne,arrTwo);
}
var end = new Date().getTime();
var time = end - start;
document.write(addResult,"<BR>"," Timing = ", time, " ms");

</script>

[toc] | [prev] | [standalone]


Page 3 of 3 — ← Prev page 1 2 [3]

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


csiph-web