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


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

How to manipulate integer that sometimes Nan and sometimes have a value?

Started byjonas.thornvall@gmail.com
First post2015-09-25 07:31 -0700
Last post2015-09-27 09:02 -0700
Articles 4 on this page of 44 — 8 participants

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


Contents

  How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-25 07:31 -0700
    Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-25 17:09 +0200
      Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-25 14:43 -0700
        Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 08:54 +0200
      Re: How to manipulate integer that sometimes Nan and sometimes have a value? Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-09-30 15:04 +0200
        Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-30 21:13 +0200
          Re: How to manipulate integer that sometimes Nan and sometimes have a value? Erwin Moller <erwinmollerusenet@xs4all.nl> - 2015-10-01 10:44 +0200
            Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-10-01 13:56 +0200
              Re: How to manipulate integer that sometimes Nan and sometimes have a value? John Harris <niam@jghnorth.org.uk.invalid> - 2015-10-02 11:09 +0100
                Re: How to manipulate integer that sometimes Nan and sometimes have a value? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-02 13:55 +0100
                Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-10-02 16:34 +0200
    Re: How to manipulate integer that sometimes Nan and sometimes have a value? Danny <dann90038@gmail.com> - 2015-09-25 15:11 -0700
      Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-25 17:50 -0700
        Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 08:59 +0200
          Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-26 04:25 -0700
            Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 15:00 +0200
              Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-26 06:15 -0700
                Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 15:34 +0200
                  Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-26 08:06 -0700
                  Re: How to manipulate integer that sometimes Nan and sometimes have a value? Tim Streater <timstreater@greenbee.net> - 2015-09-26 18:10 +0100
                    Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 22:53 +0200
                      Re: How to manipulate integer that sometimes Nan and sometimes have a value? Tim Streater <timstreater@greenbee.net> - 2015-09-26 22:37 +0100
                        Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-27 10:05 +0200
                          Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-27 08:42 -0700
                          Re: How to manipulate integer that sometimes Nan and sometimes have a value? Tim Streater <timstreater@greenbee.net> - 2015-09-27 17:18 +0100
        Re: How to manipulate integer that sometimes Nan and sometimes have a value? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-26 10:59 +0100
          Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 13:15 +0200
            Re: How to manipulate integer that sometimes Nan and sometimes have a value? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-26 14:58 +0100
              Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 16:57 +0200
          Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-26 04:28 -0700
            Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 15:11 +0200
              Re: How to manipulate integer that sometimes Nan and sometimes have a value? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-09-26 17:19 +0200
                Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-26 22:51 +0200
                  Re: How to manipulate integer that sometimes Nan and sometimes have a value? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-09-27 13:50 +0200
            Re: How to manipulate integer that sometimes Nan and sometimes have a value? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-26 20:51 +0100
              Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-28 22:45 -0700
                Re: How to manipulate integer that sometimes Nan and sometimes have a value? "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2015-09-29 12:36 +0200
                Re: How to manipulate integer that sometimes Nan and sometimes have a value? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-29 12:48 +0100
          Re: How to manipulate integer that sometimes Nan and sometimes have a value? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-09-26 14:43 +0200
            Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-26 06:06 -0700
              Re: How to manipulate integer that sometimes Nan and sometimes have a value? John Harris <niam@jghnorth.org.uk.invalid> - 2015-09-27 14:06 +0100
                Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-27 08:50 -0700
                Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-27 08:53 -0700
                  Re: How to manipulate integer that sometimes Nan and sometimes have a value? jonas.thornvall@gmail.com - 2015-09-27 09:02 -0700

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


#28744

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2015-09-27 14:06 +0100
Message-ID<qbqf0bhqnafnmd20dos88navce43b852j4@4ax.com>
In reply to#28727
On Sat, 26 Sep 2015 06:06:05 -0700 (PDT), jonas.thornvall@gmail.com
wrote:


 <snip>
>What is the reason for programming languages to not allow adding to null values NaN, 
>it seem like a problem that will come up in many situations. And to me it seem 
>reasonable that the programer expect it to be a carry operation or some arithmetic 
>where the variable not initialised?
>
>What could actually go wrong by allowing add to a NaN, that is presumably a zero 
>*startvalue*, it seem as something unavoidable programming for example statistics?

It would save a lot of confusion and avoid making the Standard even
bigger if programmers set the "carry out" cell to hold 0 before the
operation, then remove the cell if it still contains 0 afterwards.

(This is faster than testing at each digit to see if it requires a
longer array).

  John

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


#28747

Fromjonas.thornvall@gmail.com
Date2015-09-27 08:50 -0700
Message-ID<45a5d845-47d3-48ab-9dbd-c08aec9385fd@googlegroups.com>
In reply to#28744
Den söndag 27 september 2015 kl. 15:06:52 UTC+2 skrev John Harris:
> On Sat, 26 Sep 2015 06:06:05 -0700 (PDT), jonas.thornvall@gmail.com
> wrote:
> 
> 
>  <snip>
> >What is the reason for programming languages to not allow adding to null values NaN, 
> >it seem like a problem that will come up in many situations. And to me it seem 
> >reasonable that the programer expect it to be a carry operation or some arithmetic 
> >where the variable not initialised?
> >
> >What could actually go wrong by allowing add to a NaN, that is presumably a zero 
> >*startvalue*, it seem as something unavoidable programming for example statistics?
> 
> It would save a lot of confusion and avoid making the Standard even
> bigger if programmers set the "carry out" cell to hold 0 before the
> operation, then remove the cell if it still contains 0 afterwards.
> 
> (This is faster than testing at each digit to see if it requires a
> longer array).
> 
>   John

I do set it to hold zero but i do not really understand why one will have to do a check that in 90% of cases is unnecessary.

/*ADD TWO VARIABLES USING CHOSEN BASE*/
function naiveAdd(base, addOne, addTwo)
{
   abigArr = addOne.length;
   asmallArr = addTwo.length;
   addResult = [];
   remainder = 0;
   for (i = 0; i < asmallArr; i ++ )
   {
      addOne[i] = parseInt(addOne[i]);
      addTwo[i] = parseInt(addTwo[i]);
      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;
}

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


#28748

Fromjonas.thornvall@gmail.com
Date2015-09-27 08:53 -0700
Message-ID<ca34d297-958d-4240-94f8-37510dbbd0c7@googlegroups.com>
In reply to#28744
Den söndag 27 september 2015 kl. 15:06:52 UTC+2 skrev John Harris:
> On Sat, 26 Sep 2015 06:06:05 -0700 (PDT), jonas.thornvall@gmail.com
> wrote:
> 
> 
>  <snip>
> >What is the reason for programming languages to not allow adding to null values NaN, 
> >it seem like a problem that will come up in many situations. And to me it seem 
> >reasonable that the programer expect it to be a carry operation or some arithmetic 
> >where the variable not initialised?
> >
> >What could actually go wrong by allowing add to a NaN, that is presumably a zero 
> >*startvalue*, it seem as something unavoidable programming for example statistics?
> 
> It would save a lot of confusion and avoid making the Standard even
> bigger if programmers set the "carry out" cell to hold 0 before the
> operation, then remove the cell if it still contains 0 afterwards.
> 
> (This is faster than testing at each digit to see if it requires a
> longer array).
> 
>   John

Ok i got you want me to set the result[result.length+1]=0  ?
But then i may have a faulthy length, it is a leading zero so probably no problem, but it seem weird.

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


#28749

Fromjonas.thornvall@gmail.com
Date2015-09-27 09:02 -0700
Message-ID<746e6814-44c6-4fdc-afdd-717ffc71c51a@googlegroups.com>
In reply to#28748
Den söndag 27 september 2015 kl. 17:53:52 UTC+2 skrev jonas.t...@gmail.com:
> Den söndag 27 september 2015 kl. 15:06:52 UTC+2 skrev John Harris:
> > On Sat, 26 Sep 2015 06:06:05 -0700 (PDT), jonas.thornvall@gmail.com
> > wrote:
> > 
> > 
> >  <snip>
> > >What is the reason for programming languages to not allow adding to null values NaN, 
> > >it seem like a problem that will come up in many situations. And to me it seem 
> > >reasonable that the programer expect it to be a carry operation or some arithmetic 
> > >where the variable not initialised?
> > >
> > >What could actually go wrong by allowing add to a NaN, that is presumably a zero 
> > >*startvalue*, it seem as something unavoidable programming for example statistics?
> > 
> > It would save a lot of confusion and avoid making the Standard even
> > bigger if programmers set the "carry out" cell to hold 0 before the
> > operation, then remove the cell if it still contains 0 afterwards.
> > 
> > (This is faster than testing at each digit to see if it requires a
> > longer array).
> > 
> >   John
> 
> Ok i got you want me to set the result[result.length+1]=0  ?
> But then i may have a faulthy length, it is a leading zero so probably no problem, but it seem weird.

/*ADD TWO VARIABLES USING CHOSEN BASE*/
function naiveAdd(base, addOne, addTwo)
{
   abigArr = addOne.length;
   asmallArr = addTwo.length;
   addResult = [];
   remainder = 0;
   for (i = 0; i < asmallArr; i ++ )
   {
      addOne[i] = parseInt(addOne[i]);
      addTwo[i] = parseInt(addTwo[i]);
      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;
}

/* COMPARE SIZE OF TWO ARRAYS, IF A < B RETURN TRUE, IF B >= A RETURN FALSE */
function lessThan(A, B)
{
   var AA = A.length;
   var BB = B.length;
   if(AA > BB) return false;
   if(AA < BB) 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;
}

/* ORDER THE ARRAYS SO THE BIGGER IS PASSED AS FIRST ARGUMENT*/
function orderArrayAdd(Abase, A, B)
{
   return lessThan(A, B) ? counterArr = naiveAdd(Abase, B, A) : counterArr = naiveAdd(Abase, A, B);
}

function main(){
base = document.eval.base.value;
myeval = document.eval.expression.value;
if (result = myeval.indexOf("+") != - 1)
   {
      opArr = myeval.split("+");
      operation = "+";
   }
   arrOne = opArr[0].split("").reverse();
   arrTwo = opArr[1].split("").reverse();
  
/* TIMER START */
var start = new Date().getTime();
out=orderArrayAdd(base,arrOne,arrTwo);
//out=naiveAdd(base,arrOne,arrTwo);
var end = new Date().getTime();
var time = end - start;
document.eval.result.value=out.reverse().join('');

} </script>

<html><body onLoad="main()";>
<form name="eval" onsubmit="main(); return false;">
Base <input type="text" name="base" value="10" size="6"><br>
Evaluate <input type="text" name="expression" value="11+30" size="30"><br>
Result <input type="text" name="result" size="30">
</form>

[toc] | [prev] | [standalone]


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

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


csiph-web