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


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

Bignumb addition function, suggestion improvements.

Started byjonas.thornvall@gmail.com
First post2015-03-14 04:15 -0700
Last post2015-03-15 05:30 -0700
Articles 20 on this page of 22 — 5 participants

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


Contents

  Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-14 04:15 -0700
    Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-14 04:40 -0700
      Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-14 04:56 -0700
    Re: Bignumb addition function, suggestion improvements. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-03-14 13:41 +0000
      Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-14 10:17 -0700
        Re: Bignumb addition function, suggestion improvements. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-03-14 19:39 +0000
          Re: Bignumb addition function, suggestion improvements. Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-14 20:56 +0100
          Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-15 01:52 -0700
            Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-15 02:57 -0700
              Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-15 03:07 -0700
      Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-14 10:57 -0700
      Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-15 03:40 -0700
        Re: Bignumb addition function, suggestion improvements. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-03-15 11:06 +0000
        Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-15 04:09 -0700
    Re: Bignumb addition function, suggestion improvements. Dr.Kral@nyc.rr.com - 2015-03-14 19:51 -0400
      Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-14 23:57 -0700
      Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-15 00:08 -0700
    Re: Bignumb addition function, suggestion improvements. John Harris <niam@jghnorth.org.uk.invalid> - 2015-03-15 10:46 +0000
      Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-15 04:12 -0700
        Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-15 06:00 -0700
      Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-15 04:27 -0700
        Re: Bignumb addition function, suggestion improvements. jonas.thornvall@gmail.com - 2015-03-15 05:30 -0700

Page 1 of 2  [1] 2  Next page →


#27026 — Bignumb addition function, suggestion improvements.

Fromjonas.thornvall@gmail.com
Date2015-03-14 04:15 -0700
SubjectBignumb addition function, suggestion improvements.
Message-ID<f9fe170d-9fdf-42bf-9fca-23a3bc48f76c@googlegroups.com>
I just wrote a big numb addition function this morning, it is a bit silly and naive, not pure string not pure integer. I think it is correct, but i may have missed leading last digit.

Suggestions to write it shorter and more clear, but keeping the naive approach appreciated.

<SCRIPT LANGUAGE="Javascript">

function naiveAdd(strOne,strTwo) {
if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.length;}
oneS = strOne.split("").reverse().join("");
twoS = strTwo.split("").reverse().join("");
out="";
remainder=0;
for (i=0;i<length;i++){
         one=oneS.charAt(i);
         two=twoS.charAt(i);
         one=parseInt(one);
         two=parseInt(two);
         if (isNaN(one)) one = 0;
         if (isNaN(two)) two = 0;
         sum=one+two+remainder;
         if (sum>=10) { sum=sum-10; remainder=1;} else {remainder=0;}
         out=sum+out;
    }
document.write("Sum = ",out,"<BR>");
}

strOne="9003";
strTwo="13039";
naiveAdd(strOne,strTwo);

</SCRIPT>

[toc] | [next] | [standalone]


#27028

Fromjonas.thornvall@gmail.com
Date2015-03-14 04:40 -0700
Message-ID<388c3cf3-ec89-4848-b5ca-19347460a41a@googlegroups.com>
In reply to#27026
Den lördag 14 mars 2015 kl. 12:15:36 UTC+1 skrev jonas.t...@gmail.com:
> I just wrote a big numb addition function this morning, it is a bit silly and naive, not pure string not pure integer. I think it is correct, but i may have missed leading last digit.
> 
> Suggestions to write it shorter and more clear, but keeping the naive approach appreciated.
> 
> <SCRIPT LANGUAGE="Javascript">
> 
> function naiveAdd(strOne,strTwo) {
> if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.length;}
> oneS = strOne.split("").reverse().join("");
> twoS = strTwo.split("").reverse().join("");
> out="";
> remainder=0;
> for (i=0;i<length;i++){
>          one=oneS.charAt(i);
>          two=twoS.charAt(i);
>          one=parseInt(one);
>          two=parseInt(two);
>          if (isNaN(one)) one = 0;
>          if (isNaN(two)) two = 0;
>          sum=one+two+remainder;
>          if (sum>=10) { sum=sum-10; remainder=1;} else {remainder=0;}
>          out=sum+out;
>     }
> document.write("Sum = ",out,"<BR>");
> }
> 
> strOne="9003";
> strTwo="13039";
> naiveAdd(strOne,strTwo);
> 
> </SCRIPT>

I think it will be easy changing this to make addition in anybase. I try to make it generic.

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


#27029

Fromjonas.thornvall@gmail.com
Date2015-03-14 04:56 -0700
Message-ID<5d084c35-6e90-4982-9137-413c8466b2e2@googlegroups.com>
In reply to#27028
Den lördag 14 mars 2015 kl. 12:40:55 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 14 mars 2015 kl. 12:15:36 UTC+1 skrev jonas.t...@gmail.com:
> > I just wrote a big numb addition function this morning, it is a bit silly and naive, not pure string not pure integer. I think it is correct, but i may have missed leading last digit.
> > 
> > Suggestions to write it shorter and more clear, but keeping the naive approach appreciated.
> > 
> > <SCRIPT LANGUAGE="Javascript">
> > 
> > function naiveAdd(strOne,strTwo) {
> > if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.length;}
> > oneS = strOne.split("").reverse().join("");
> > twoS = strTwo.split("").reverse().join("");
> > out="";
> > remainder=0;
> > for (i=0;i<length;i++){
> >          one=oneS.charAt(i);
> >          two=twoS.charAt(i);
> >          one=parseInt(one);
> >          two=parseInt(two);
> >          if (isNaN(one)) one = 0;
> >          if (isNaN(two)) two = 0;
> >          sum=one+two+remainder;
> >          if (sum>=10) { sum=sum-10; remainder=1;} else {remainder=0;}
> >          out=sum+out;
> >     }
> > document.write("Sum = ",out,"<BR>");
> > }
> > 
> > strOne="9003";
> > strTwo="13039";
> > naiveAdd(strOne,strTwo);
> > 
> > </SCRIPT>
> 
> I think it will be easy changing this to make addition in anybase. I try to make it generic.

Should probably do an if else within loop for isnan when one string shorter and one longer and just copy values at else (if not something in remainder?).

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


#27032

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-03-14 13:41 +0000
Message-ID<87fv97u25x.fsf@bsb.me.uk>
In reply to#27026
jonas.thornvall@gmail.com writes:

> I just wrote a big numb addition function this morning, it is a bit
> silly and naive, not pure string not pure integer. I think it is
> correct, but i may have missed leading last digit.

Yes, you do miss the leading digit in some cases.  And it's only for
positive numbers.

> Suggestions to write it shorter and more clear, but keeping the naive
> approach appreciated.
>
> <SCRIPT LANGUAGE="Javascript">
>
> function naiveAdd(strOne,strTwo) {
> if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.length;}
> oneS = strOne.split("").reverse().join("");
> twoS = strTwo.split("").reverse().join("");

I would avoid this reversing operation.  It's not needed since you can
get the charCodeAt any index value you like.

> out="";
> remainder=0;

Use var.

> for (i=0;i<length;i++){
>          one=oneS.charAt(i);
>          two=twoS.charAt(i);
>          one=parseInt(one);
>          two=parseInt(two);
>          if (isNaN(one)) one = 0;
>          if (isNaN(two)) two = 0;
>          sum=one+two+remainder;
>          if (sum>=10) { sum=sum-10; remainder=1;} else {remainder=0;}
>          out=sum+out;
>     }

There's a bit of duplication here and a bug: you forget that the carry
(the usual name for your 'remainder') can be set at the end of the loop.

> document.write("Sum = ",out,"<BR>");
> }
>
> strOne="9003";
> strTwo="13039";
> naiveAdd(strOne,strTwo);
>
> </SCRIPT>

String.prototype.digitValueAt = function (pos) {
    var d = this.charCodeAt(pos);
    return isNaN(d) ? 0 : d - 48;
}

function naiveAddX(strOne, strTwo) {
    var i1 = strOne.length, i2 = strTwo.length, result = "", carry = 0;
    while (i1 > 0 || i2 > 0 || carry > 0) {
        var dsum = strOne.digitValueAt(--i1) + strTwo.digitValueAt(--i2) + carry;
        if (dsum >= 10) {
            dsum -= 10;
            carry = 1;
        }
        else carry = 0;
        result = dsum + result;
    }
    return result;
}

You could also build a result array and create the string on one go.
That's probably faster.

-- 
Ben.

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


#27034

Fromjonas.thornvall@gmail.com
Date2015-03-14 10:17 -0700
Message-ID<531f97ee-89e2-49a6-b9a8-4b11102e998c@googlegroups.com>
In reply to#27032
Den lördag 14 mars 2015 kl. 14:41:35 UTC+1 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
> 
> > I just wrote a big numb addition function this morning, it is a bit
> > silly and naive, not pure string not pure integer. I think it is
> > correct, but i may have missed leading last digit.
> 
> Yes, you do miss the leading digit in some cases.  And it's only for
> positive numbers.
> 
> > Suggestions to write it shorter and more clear, but keeping the naive
> > approach appreciated.
> >
> > <SCRIPT LANGUAGE="Javascript">
> >
> > function naiveAdd(strOne,strTwo) {
> > if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.length;}
> > oneS = strOne.split("").reverse().join("");
> > twoS = strTwo.split("").reverse().join("");
> 
> I would avoid this reversing operation.  It's not needed since you can
> get the charCodeAt any index value you like.
> 
> > out="";
> > remainder=0;
> 
> Use var.
> 
> > for (i=0;i<length;i++){
> >          one=oneS.charAt(i);
> >          two=twoS.charAt(i);
> >          one=parseInt(one);
> >          two=parseInt(two);
> >          if (isNaN(one)) one = 0;
> >          if (isNaN(two)) two = 0;
> >          sum=one+two+remainder;
> >          if (sum>=10) { sum=sum-10; remainder=1;} else {remainder=0;}
> >          out=sum+out;
> >     }
> 
> There's a bit of duplication here and a bug: you forget that the carry
> (the usual name for your 'remainder') can be set at the end of the loop.
> 
> > document.write("Sum = ",out,"<BR>");
> > }
> >
> > strOne="9003";
> > strTwo="13039";
> > naiveAdd(strOne,strTwo);
> >
> > </SCRIPT>
> 
> String.prototype.digitValueAt = function (pos) {
>     var d = this.charCodeAt(pos);
>     return isNaN(d) ? 0 : d - 48;
> }

I do not get ? 0 : d - 48;  
 
> function naiveAddX(strOne, strTwo) {
>     var i1 = strOne.length, i2 = strTwo.length, result = "", carry = 0;
>     while (i1 > 0 || i2 > 0 || carry > 0) {
>         var dsum = strOne.digitValueAt(--i1) + strTwo.digitValueAt(--i2) + carry;

What is the -- before i1 and i2?

>         if (dsum >= 10) {
>             dsum -= 10;
>             carry = 1;
>         }
>         else carry = 0;
>         result = dsum + result;
>     }
>     return result;
> }
> 
> You could also build a result array and create the string on one go.
> That's probably faster.

I was thinking that adding to negative numbers basicly a subtranction -7+4=4-7, mult and div go before add and subtraction. So if i implement them and powers with a parser. Will the lack of add to negative numbers mess up things or can i use subtraction instead?
 
> -- 
> Ben.

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


#27036

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-03-14 19:39 +0000
Message-ID<878uezpdv6.fsf@bsb.me.uk>
In reply to#27034
jonas.thornvall@gmail.com writes:

> Den lördag 14 mars 2015 kl. 14:41:35 UTC+1 skrev Ben Bacarisse:
>> jonas.thornvall@gmail.com writes:
>> 
>> > I just wrote a big numb addition function this morning, it is a bit
>> > silly and naive, not pure string not pure integer. I think it is
>> > correct, but i may have missed leading last digit.
>> 
>> Yes, you do miss the leading digit in some cases.  And it's only for
>> positive numbers.
>> 
>> > Suggestions to write it shorter and more clear, but keeping the naive
>> > approach appreciated.
>> >
>> > <SCRIPT LANGUAGE="Javascript">
>> >
>> > function naiveAdd(strOne,strTwo) {
>> > if (strOne.length>=strTwo.length) {length=strOne.length;} else
>> > {length=strTwo.length;}
>> > oneS = strOne.split("").reverse().join("");
>> > twoS = strTwo.split("").reverse().join("");
>> 
>> I would avoid this reversing operation.  It's not needed since you can
>> get the charCodeAt any index value you like.
>> 
>> > out="";
>> > remainder=0;
>> 
>> Use var.
>> 
>> > for (i=0;i<length;i++){
>> >          one=oneS.charAt(i);
>> >          two=twoS.charAt(i);
>> >          one=parseInt(one);
>> >          two=parseInt(two);
>> >          if (isNaN(one)) one = 0;
>> >          if (isNaN(two)) two = 0;
>> >          sum=one+two+remainder;
>> >          if (sum>=10) { sum=sum-10; remainder=1;} else {remainder=0;}
>> >          out=sum+out;
>> >     }
>> 
>> There's a bit of duplication here and a bug: you forget that the carry
>> (the usual name for your 'remainder') can be set at the end of the loop.
>> 
>> > document.write("Sum = ",out,"<BR>");
>> > }
>> >
>> > strOne="9003";
>> > strTwo="13039";
>> > naiveAdd(strOne,strTwo);
>> >
>> > </SCRIPT>
>> 
>> String.prototype.digitValueAt = function (pos) {
>>     var d = this.charCodeAt(pos);
>>     return isNaN(d) ? 0 : d - 48;
>> }
>
> I do not get ? 0 : d - 48;

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

The 48 is "0".charCodeAt(0) which would be have been more self-documenting.

>  
>> function naiveAddX(strOne, strTwo) {
>>     var i1 = strOne.length, i2 = strTwo.length, result = "", carry = 0;
>>     while (i1 > 0 || i2 > 0 || carry > 0) {
>>         var dsum = strOne.digitValueAt(--i1) + strTwo.digitValueAt(--i2) + carry;
>
> What is the -- before i1 and i2?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement_(--)

<snip>
> I was thinking that adding to negative numbers basicly a subtranction
> -7+4=4-7, mult and div go before add and subtraction. So if i
> implement them and powers with a parser. Will the lack of add to
> negative numbers mess up things or can i use subtraction instead?

I don't follow.

-- 
Ben.

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


#27037

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-03-14 20:56 +0100
Message-ID<1870601.SULIsds41C@PointedEars.de>
In reply to#27036
Ben Bacarisse wrote:

> jonas.thornvall@gmail.com writes:
>> Den lördag 14 mars 2015 kl. 14:41:35 UTC+1 skrev Ben Bacarisse:
>>> String.prototype.digitValueAt = function (pos) {
>>>     var d = this.charCodeAt(pos);
>>>     return isNaN(d) ? 0 : d - 48;
>>> }
>>
>> I do not get ? 0 : d - 48;
> 
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
> 
> The 48 is "0".charCodeAt(0) which would be have been more
> self-documenting.

Apropos:

  s.digitValueAt(pos)

where s is a String, would be equivalent to

  +s.charAt(pos) || 0

or even

  +s[pos] || 0

in JavaScript 1.5+ and more recent implementations of ECMAScript.  (“…[…]” 
is _not_ an operation.)

<http://PointedEars.de/es-matrix/?filter=%5E%5C%2B%7C%5Estring%5C%5B%7CcharAt>

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

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


#27043

Fromjonas.thornvall@gmail.com
Date2015-03-15 01:52 -0700
Message-ID<c4dc6cf8-5e96-4385-9e38-f90c0602a92e@googlegroups.com>
In reply to#27036
Den lördag 14 mars 2015 kl. 20:40:02 UTC+1 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
> 
> > Den lördag 14 mars 2015 kl. 14:41:35 UTC+1 skrev Ben Bacarisse:
> >> jonas.thornvall@gmail.com writes:
> >> 
> >> > I just wrote a big numb addition function this morning, it is a bit
> >> > silly and naive, not pure string not pure integer. I think it is
> >> > correct, but i may have missed leading last digit.
> >> 
> >> Yes, you do miss the leading digit in some cases.  And it's only for
> >> positive numbers.
> >> 
> >> > Suggestions to write it shorter and more clear, but keeping the naive
> >> > approach appreciated.
> >> >
> >> > <SCRIPT LANGUAGE="Javascript">
> >> >
> >> > function naiveAdd(strOne,strTwo) {
> >> > if (strOne.length>=strTwo.length) {length=strOne.length;} else
> >> > {length=strTwo.length;}
> >> > oneS = strOne.split("").reverse().join("");
> >> > twoS = strTwo.split("").reverse().join("");
> >> 
> >> I would avoid this reversing operation.  It's not needed since you can
> >> get the charCodeAt any index value you like.
> >> 
> >> > out="";
> >> > remainder=0;
> >> 
> >> Use var.
> >> 
> >> > for (i=0;i<length;i++){
> >> >          one=oneS.charAt(i);
> >> >          two=twoS.charAt(i);
> >> >          one=parseInt(one);
> >> >          two=parseInt(two);
> >> >          if (isNaN(one)) one = 0;
> >> >          if (isNaN(two)) two = 0;
> >> >          sum=one+two+remainder;
> >> >          if (sum>=10) { sum=sum-10; remainder=1;} else {remainder=0;}
> >> >          out=sum+out;
> >> >     }
> >> 
> >> There's a bit of duplication here and a bug: you forget that the carry
> >> (the usual name for your 'remainder') can be set at the end of the loop.
> >> 
> >> > document.write("Sum = ",out,"<BR>");
> >> > }
> >> >
> >> > strOne="9003";
> >> > strTwo="13039";
> >> > naiveAdd(strOne,strTwo);
> >> >
> >> > </SCRIPT>
> >> 
> >> String.prototype.digitValueAt = function (pos) {
> >>     var d = this.charCodeAt(pos);
> >>     return isNaN(d) ? 0 : d - 48;
> >> }
> >
> > I do not get ? 0 : d - 48;
> 
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
> 
> The 48 is "0".charCodeAt(0) which would be have been more self-documenting.
> 
> >  
> >> function naiveAddX(strOne, strTwo) {
> >>     var i1 = strOne.length, i2 = strTwo.length, result = "", carry = 0;
> >>     while (i1 > 0 || i2 > 0 || carry > 0) {
> >>         var dsum = strOne.digitValueAt(--i1) + strTwo.digitValueAt(--i2) + carry;
> >
> > What is the -- before i1 and i2?
> 
> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement_(--)
> 
> <snip>
> > I was thinking that adding to negative numbers basicly a subtranction
> > -7+4=4-7, mult and div go before add and subtraction. So if i
> > implement them and powers with a parser. Will the lack of add to
> > negative numbers mess up things or can i use subtraction instead?
> 
> I don't follow.
> 
> -- 
> Ben.

I have not implemented the change of loop yet, and maybe i will implement array. But does the algorithm hold for anybase? Right now from 2-10, but if i make the character set commaseparated for HEX 15,7,2,0,11 and so on would it work for anybase?

<SCRIPT LANGUAGE="Javascript">

function naiveAdd(base,strOne,strTwo) {
oneS = strOne.split("").reverse().join("");
twoS = strTwo.split("").reverse().join("");
if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.length;}

out="";
remainder=0;
for (i=0;i<length;i++){
         one=oneS.charAt(i);
         two=twoS.charAt(i);
         one=parseInt(one);
         two=parseInt(two);
         if (isNaN(one)) one = 0;
         if (isNaN(two)) two = 0;
         sum=one+two+remainder;
         if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
         out=sum+out;
    }
if (remainder==1) out=remainder+out;
return out;
}
base=2;
strOne="110";
strTwo="11110";
naiveAdd(base,strOne,strTwo);
document.write("Sum = ",out,"<BR>");
</SCRIPT>

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


#27044

Fromjonas.thornvall@gmail.com
Date2015-03-15 02:57 -0700
Message-ID<af2d0a0c-ec2a-4296-8df9-12b29564add7@googlegroups.com>
In reply to#27043
Den söndag 15 mars 2015 kl. 09:52:06 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 14 mars 2015 kl. 20:40:02 UTC+1 skrev Ben Bacarisse:
> > jonas.thornvall@gmail.com writes:
> > 
> > > Den lördag 14 mars 2015 kl. 14:41:35 UTC+1 skrev Ben Bacarisse:
> > >> jonas.thornvall@gmail.com writes:
> > >> 
> > >> > I just wrote a big numb addition function this morning, it is a bit
> > >> > silly and naive, not pure string not pure integer. I think it is
> > >> > correct, but i may have missed leading last digit.
> > >> 
> > >> Yes, you do miss the leading digit in some cases.  And it's only for
> > >> positive numbers.
> > >> 
> > >> > Suggestions to write it shorter and more clear, but keeping the naive
> > >> > approach appreciated.
> > >> >
> > >> > <SCRIPT LANGUAGE="Javascript">
> > >> >
> > >> > function naiveAdd(strOne,strTwo) {
> > >> > if (strOne.length>=strTwo.length) {length=strOne.length;} else
> > >> > {length=strTwo.length;}
> > >> > oneS = strOne.split("").reverse().join("");
> > >> > twoS = strTwo.split("").reverse().join("");
> > >> 
> > >> I would avoid this reversing operation.  It's not needed since you can
> > >> get the charCodeAt any index value you like.
> > >> 
> > >> > out="";
> > >> > remainder=0;
> > >> 
> > >> Use var.
> > >> 
> > >> > for (i=0;i<length;i++){
> > >> >          one=oneS.charAt(i);
> > >> >          two=twoS.charAt(i);
> > >> >          one=parseInt(one);
> > >> >          two=parseInt(two);
> > >> >          if (isNaN(one)) one = 0;
> > >> >          if (isNaN(two)) two = 0;
> > >> >          sum=one+two+remainder;
> > >> >          if (sum>=10) { sum=sum-10; remainder=1;} else {remainder=0;}
> > >> >          out=sum+out;
> > >> >     }
> > >> 
> > >> There's a bit of duplication here and a bug: you forget that the carry
> > >> (the usual name for your 'remainder') can be set at the end of the loop.
> > >> 
> > >> > document.write("Sum = ",out,"<BR>");
> > >> > }
> > >> >
> > >> > strOne="9003";
> > >> > strTwo="13039";
> > >> > naiveAdd(strOne,strTwo);
> > >> >
> > >> > </SCRIPT>
> > >> 
> > >> String.prototype.digitValueAt = function (pos) {
> > >>     var d = this.charCodeAt(pos);
> > >>     return isNaN(d) ? 0 : d - 48;
> > >> }
> > >
> > > I do not get ? 0 : d - 48;
> > 
> > https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
> > 
> > The 48 is "0".charCodeAt(0) which would be have been more self-documenting.
> > 
> > >  
> > >> function naiveAddX(strOne, strTwo) {
> > >>     var i1 = strOne.length, i2 = strTwo.length, result = "", carry = 0;
> > >>     while (i1 > 0 || i2 > 0 || carry > 0) {
> > >>         var dsum = strOne.digitValueAt(--i1) + strTwo.digitValueAt(--i2) + carry;
> > >
> > > What is the -- before i1 and i2?
> > 
> > https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement_(--)
> > 
> > <snip>
> > > I was thinking that adding to negative numbers basicly a subtranction
> > > -7+4=4-7, mult and div go before add and subtraction. So if i
> > > implement them and powers with a parser. Will the lack of add to
> > > negative numbers mess up things or can i use subtraction instead?
> > 
> > I don't follow.
> > 
> > -- 
> > Ben.
> 
> I have not implemented the change of loop yet, and maybe i will implement array. But does the algorithm hold for anybase? Right now from 2-10, but if i make the character set commaseparated for HEX 15,7,2,0,11 and so on would it work for anybase?
> 
> <SCRIPT LANGUAGE="Javascript">
> 
> function naiveAdd(base,strOne,strTwo) {
> oneS = strOne.split("").reverse().join("");
> twoS = strTwo.split("").reverse().join("");
> if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.length;}
> 
> out="";
> remainder=0;
> for (i=0;i<length;i++){
>          one=oneS.charAt(i);
>          two=twoS.charAt(i);
>          one=parseInt(one);
>          two=parseInt(two);
>          if (isNaN(one)) one = 0;
>          if (isNaN(two)) two = 0;
>          sum=one+two+remainder;
>          if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
>          out=sum+out;
>     }
> if (remainder==1) out=remainder+out;
> return out;
> }
> base=2;
> strOne="110";
> strTwo="11110";
> naiveAdd(base,strOne,strTwo);
> document.write("Sum = ",out,"<BR>");
> </SCRIPT>

I will try to make a generic math library working for anybase for the simplest operators and operations like +,-,/,^,Sqrt,mod. 

And of course support for partitioned fractions in the base, eg decimals,ternimals?,octimals?binimals?

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


#27045

Fromjonas.thornvall@gmail.com
Date2015-03-15 03:07 -0700
Message-ID<27efb621-9bcd-49bd-99fb-b16396f9b17c@googlegroups.com>
In reply to#27044
Den söndag 15 mars 2015 kl. 10:57:30 UTC+1 skrev jonas.t...@gmail.com:
> Den söndag 15 mars 2015 kl. 09:52:06 UTC+1 skrev jonas.t...@gmail.com:
> > Den lördag 14 mars 2015 kl. 20:40:02 UTC+1 skrev Ben Bacarisse:
> > > jonas.thornvall@gmail.com writes:
> > > 
> > > > Den lördag 14 mars 2015 kl. 14:41:35 UTC+1 skrev Ben Bacarisse:
> > > >> jonas.thornvall@gmail.com writes:
> > > >> 
> > > >> > I just wrote a big numb addition function this morning, it is a bit
> > > >> > silly and naive, not pure string not pure integer. I think it is
> > > >> > correct, but i may have missed leading last digit.
> > > >> 
> > > >> Yes, you do miss the leading digit in some cases.  And it's only for
> > > >> positive numbers.
> > > >> 
> > > >> > Suggestions to write it shorter and more clear, but keeping the naive
> > > >> > approach appreciated.
> > > >> >
> > > >> > <SCRIPT LANGUAGE="Javascript">
> > > >> >
> > > >> > function naiveAdd(strOne,strTwo) {
> > > >> > if (strOne.length>=strTwo.length) {length=strOne.length;} else
> > > >> > {length=strTwo.length;}
> > > >> > oneS = strOne.split("").reverse().join("");
> > > >> > twoS = strTwo.split("").reverse().join("");
> > > >> 
> > > >> I would avoid this reversing operation.  It's not needed since you can
> > > >> get the charCodeAt any index value you like.
> > > >> 
> > > >> > out="";
> > > >> > remainder=0;
> > > >> 
> > > >> Use var.
> > > >> 
> > > >> > for (i=0;i<length;i++){
> > > >> >          one=oneS.charAt(i);
> > > >> >          two=twoS.charAt(i);
> > > >> >          one=parseInt(one);
> > > >> >          two=parseInt(two);
> > > >> >          if (isNaN(one)) one = 0;
> > > >> >          if (isNaN(two)) two = 0;
> > > >> >          sum=one+two+remainder;
> > > >> >          if (sum>=10) { sum=sum-10; remainder=1;} else {remainder=0;}
> > > >> >          out=sum+out;
> > > >> >     }
> > > >> 
> > > >> There's a bit of duplication here and a bug: you forget that the carry
> > > >> (the usual name for your 'remainder') can be set at the end of the loop.
> > > >> 
> > > >> > document.write("Sum = ",out,"<BR>");
> > > >> > }
> > > >> >
> > > >> > strOne="9003";
> > > >> > strTwo="13039";
> > > >> > naiveAdd(strOne,strTwo);
> > > >> >
> > > >> > </SCRIPT>
> > > >> 
> > > >> String.prototype.digitValueAt = function (pos) {
> > > >>     var d = this.charCodeAt(pos);
> > > >>     return isNaN(d) ? 0 : d - 48;
> > > >> }
> > > >
> > > > I do not get ? 0 : d - 48;
> > > 
> > > https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
> > > 
> > > The 48 is "0".charCodeAt(0) which would be have been more self-documenting.
> > > 
> > > >  
> > > >> function naiveAddX(strOne, strTwo) {
> > > >>     var i1 = strOne.length, i2 = strTwo.length, result = "", carry = 0;
> > > >>     while (i1 > 0 || i2 > 0 || carry > 0) {
> > > >>         var dsum = strOne.digitValueAt(--i1) + strTwo.digitValueAt(--i2) + carry;
> > > >
> > > > What is the -- before i1 and i2?
> > > 
> > > https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Decrement_(--)
> > > 
> > > <snip>
> > > > I was thinking that adding to negative numbers basicly a subtranction
> > > > -7+4=4-7, mult and div go before add and subtraction. So if i
> > > > implement them and powers with a parser. Will the lack of add to
> > > > negative numbers mess up things or can i use subtraction instead?
> > > 
> > > I don't follow.
> > > 
> > > -- 
> > > Ben.
> > 
> > I have not implemented the change of loop yet, and maybe i will implement array. But does the algorithm hold for anybase? Right now from 2-10, but if i make the character set commaseparated for HEX 15,7,2,0,11 and so on would it work for anybase?
> > 
> > <SCRIPT LANGUAGE="Javascript">
> > 
> > function naiveAdd(base,strOne,strTwo) {
> > oneS = strOne.split("").reverse().join("");
> > twoS = strTwo.split("").reverse().join("");
> > if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.length;}
> > 
> > out="";
> > remainder=0;
> > for (i=0;i<length;i++){
> >          one=oneS.charAt(i);
> >          two=twoS.charAt(i);
> >          one=parseInt(one);
> >          two=parseInt(two);
> >          if (isNaN(one)) one = 0;
> >          if (isNaN(two)) two = 0;
> >          sum=one+two+remainder;
> >          if (sum>=base) { sum=sum-base; remainder=1;} else {remainder=0;}
> >          out=sum+out;
> >     }
> > if (remainder==1) out=remainder+out;
> > return out;
> > }
> > base=2;
> > strOne="110";
> > strTwo="11110";
> > naiveAdd(base,strOne,strTwo);
> > document.write("Sum = ",out,"<BR>");
> > </SCRIPT>
> 
> I will try to make a generic math library working for anybase for the simplest operators and operations like +,-,/,^,Sqrt,mod. 
> 
> And of course support for partitioned fractions in the base, eg decimals,ternimals?,octimals?binimals?

And a parser for expressions.

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


#27035

Fromjonas.thornvall@gmail.com
Date2015-03-14 10:57 -0700
Message-ID<a8a8bf55-c276-451a-8cb8-2d12ce8227d1@googlegroups.com>
In reply to#27032
Den lördag 14 mars 2015 kl. 14:41:35 UTC+1 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
> 
> > I just wrote a big numb addition function this morning, it is a bit
> > silly and naive, not pure string not pure integer. I think it is
> > correct, but i may have missed leading last digit.
> 
> Yes, you do miss the leading digit in some cases.  And it's only for
> positive numbers.
> 
> > Suggestions to write it shorter and more clear, but keeping the naive
> > approach appreciated.
> >
> > <SCRIPT LANGUAGE="Javascript">
> >
> > function naiveAdd(strOne,strTwo) {
> > if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.length;}
> > oneS = strOne.split("").reverse().join("");
> > twoS = strTwo.split("").reverse().join("");
> 
> I would avoid this reversing operation.  It's not needed since you can
> get the charCodeAt any index value you like.
> 
> > out="";
> > remainder=0;
> 
> Use var.
> 
> > for (i=0;i<length;i++){
> >          one=oneS.charAt(i);
> >          two=twoS.charAt(i);
> >          one=parseInt(one);
> >          two=parseInt(two);
> >          if (isNaN(one)) one = 0;
> >          if (isNaN(two)) two = 0;
> >          sum=one+two+remainder;
> >          if (sum>=10) { sum=sum-10; remainder=1;} else {remainder=0;}
> >          out=sum+out;
> >     }
> 
> There's a bit of duplication here and a bug: you forget that the carry
> (the usual name for your 'remainder') can be set at the end of the loop.
> 
> > document.write("Sum = ",out,"<BR>");
> > }
> >
> > strOne="9003";
> > strTwo="13039";
> > naiveAdd(strOne,strTwo);
> >
> > </SCRIPT>
> 
> String.prototype.digitValueAt = function (pos) {
>     var d = this.charCodeAt(pos);
>     return isNaN(d) ? 0 : d - 48;
> }
> 
> function naiveAddX(strOne, strTwo) {
>     var i1 = strOne.length, i2 = strTwo.length, result = "", carry = 0;
>     while (i1 > 0 || i2 > 0 || carry > 0) {
>         var dsum = strOne.digitValueAt(--i1) + strTwo.digitValueAt(--i2) + carry;
>         if (dsum >= 10) {
>             dsum -= 10;
>             carry = 1;
>         }
>         else carry = 0;
>         result = dsum + result;
>     }
>     return result;
> }
> 
> You could also build a result array and create the string on one go.
> That's probably faster.
> 
> -- 
> Ben.

I felt something was wrong but the result seemed to come out correct but i did not try strings of equal length with carry at first digit.

I did need to add a digit if carry was 1 after loop.

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


#27046

Fromjonas.thornvall@gmail.com
Date2015-03-15 03:40 -0700
Message-ID<700a5833-152f-446b-8770-187fe99a3d81@googlegroups.com>
In reply to#27032
Den lördag 14 mars 2015 kl. 14:41:35 UTC+1 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
> 
> > I just wrote a big numb addition function this morning, it is a bit
> > silly and naive, not pure string not pure integer. I think it is
> > correct, but i may have missed leading last digit.
> 
> Yes, you do miss the leading digit in some cases.  And it's only for
> positive numbers.
> 
> > Suggestions to write it shorter and more clear, but keeping the naive
> > approach appreciated.
> >
> > <SCRIPT LANGUAGE="Javascript">
> >
> > function naiveAdd(strOne,strTwo) {
> > if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.length;}
> > oneS = strOne.split("").reverse().join("");
> > twoS = strTwo.split("").reverse().join("");
> 
> I would avoid this reversing operation.  It's not needed since you can
> get the charCodeAt any index value you like.
> 
> > out="";
> > remainder=0;
> 
> Use var.
> 
> > for (i=0;i<length;i++){
> >          one=oneS.charAt(i);
> >          two=twoS.charAt(i);
> >          one=parseInt(one);
> >          two=parseInt(two);
> >          if (isNaN(one)) one = 0;
> >          if (isNaN(two)) two = 0;
> >          sum=one+two+remainder;
> >          if (sum>=10) { sum=sum-10; remainder=1;} else {remainder=0;}
> >          out=sum+out;
> >     }
> 
> There's a bit of duplication here and a bug: you forget that the carry
> (the usual name for your 'remainder') can be set at the end of the loop.
> 
> > document.write("Sum = ",out,"<BR>");
> > }
> >
> > strOne="9003";
> > strTwo="13039";
> > naiveAdd(strOne,strTwo);
> >
> > </SCRIPT>
> 
> String.prototype.digitValueAt = function (pos) {
>     var d = this.charCodeAt(pos);
>     return isNaN(d) ? 0 : d - 48;
> }
> 
> function naiveAddX(strOne, strTwo) {
>     var i1 = strOne.length, i2 = strTwo.length, result = "", carry = 0;
>     while (i1 > 0 || i2 > 0 || carry > 0) {
>         var dsum = strOne.digitValueAt(--i1) + strTwo.digitValueAt(--i2) + carry;
>         if (dsum >= 10) {
>             dsum -= 10;
>             carry = 1;
>         }
>         else carry = 0;
>         result = dsum + result;
>     }
>     return result;
> }
> 
> You could also build a result array and create the string on one go.
> That's probably faster.
> 
> -- 
> Ben.

I just realised there is things i do not get in your code, i was trying to implement reading the array backwards **for (i=length-1;i>=0;i--){**
When i realised they will slide relative eachother, if not padded, but i can't see you doing any padding?

The work pad VS reverse?

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


#27048

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-03-15 11:06 +0000
Message-ID<87sid6o6yl.fsf@bsb.me.uk>
In reply to#27046
jonas.thornvall@gmail.com writes:

> Den lördag 14 mars 2015 kl. 14:41:35 UTC+1 skrev Ben Bacarisse:
<snip>
>> function naiveAddX(strOne, strTwo) {
>>     var i1 = strOne.length, i2 = strTwo.length, result = "", carry = 0;
>>     while (i1 > 0 || i2 > 0 || carry > 0) {
>>         var dsum = strOne.digitValueAt(--i1) + strTwo.digitValueAt(--i2) + carry;
>>         if (dsum >= 10) {
>>             dsum -= 10;
>>             carry = 1;
>>         }
>>         else carry = 0;
>>         result = dsum + result;
>>     }
>>     return result;
>> }
<snip>
> I just realised there is things i do not get in your code, i was
> trying to implement reading the array backwards **for
> (i=length-1;i>=0;i--){**
> When i realised they will slide relative eachother, if not padded, but
> i can't see you doing any padding?

I use two indexes and when either goes negative digitValueAt simulates
padded number by returning 0.

-- 
Ben.

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


#27049

Fromjonas.thornvall@gmail.com
Date2015-03-15 04:09 -0700
Message-ID<32031f41-5103-4aab-b7c2-cfcf41972ad7@googlegroups.com>
In reply to#27046
Den söndag 15 mars 2015 kl. 11:40:44 UTC+1 skrev jonas.t...@gmail.com:
> Den lördag 14 mars 2015 kl. 14:41:35 UTC+1 skrev Ben Bacarisse:
> > jonas.thornvall@gmail.com writes:
> > 
> > > I just wrote a big numb addition function this morning, it is a bit
> > > silly and naive, not pure string not pure integer. I think it is
> > > correct, but i may have missed leading last digit.
> > 
> > Yes, you do miss the leading digit in some cases.  And it's only for
> > positive numbers.
> > 
> > > Suggestions to write it shorter and more clear, but keeping the naive
> > > approach appreciated.
> > >
> > > <SCRIPT LANGUAGE="Javascript">
> > >
> > > function naiveAdd(strOne,strTwo) {
> > > if (strOne.length>=strTwo.length) {length=strOne.length;} else {length=strTwo.length;}
> > > oneS = strOne.split("").reverse().join("");
> > > twoS = strTwo.split("").reverse().join("");
> > 
> > I would avoid this reversing operation.  It's not needed since you can
> > get the charCodeAt any index value you like.
> > 
> > > out="";
> > > remainder=0;
> > 
> > Use var.
> > 
> > > for (i=0;i<length;i++){
> > >          one=oneS.charAt(i);
> > >          two=twoS.charAt(i);
> > >          one=parseInt(one);
> > >          two=parseInt(two);
> > >          if (isNaN(one)) one = 0;
> > >          if (isNaN(two)) two = 0;
> > >          sum=one+two+remainder;
> > >          if (sum>=10) { sum=sum-10; remainder=1;} else {remainder=0;}
> > >          out=sum+out;
> > >     }
> > 
> > There's a bit of duplication here and a bug: you forget that the carry
> > (the usual name for your 'remainder') can be set at the end of the loop.
> > 
> > > document.write("Sum = ",out,"<BR>");
> > > }
> > >
> > > strOne="9003";
> > > strTwo="13039";
> > > naiveAdd(strOne,strTwo);
> > >
> > > </SCRIPT>
> > 
> > String.prototype.digitValueAt = function (pos) {
> >     var d = this.charCodeAt(pos);
> >     return isNaN(d) ? 0 : d - 48;
> > }
> > 
> > function naiveAddX(strOne, strTwo) {
> >     var i1 = strOne.length, i2 = strTwo.length, result = "", carry = 0;
> >     while (i1 > 0 || i2 > 0 || carry > 0) {
> >         var dsum = strOne.digitValueAt(--i1) + strTwo.digitValueAt(--i2) + carry;
> >         if (dsum >= 10) {
> >             dsum -= 10;
> >             carry = 1;
> >         }
> >         else carry = 0;
> >         result = dsum + result;
> >     }
> >     return result;
> > }
> > 
> > You could also build a result array and create the string on one go.
> > That's probably faster.
> > 
> > -- 
> > Ben.
> 
> I just realised there is things i do not get in your code, i was trying to implement reading the array backwards **for (i=length-1;i>=0;i--){**
> When i realised they will slide relative eachother, if not padded, but i can't see you doing any padding?
> 
> The work pad VS reverse?

reversed index **read** would be nice maybe even offset vs other string/array strOne.charCodeAt(i,R,O(strTwo))
But now i am really dreaming i am pretty cluless about innerwork of hardware and stacks.

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


#27038

FromDr.Kral@nyc.rr.com
Date2015-03-14 19:51 -0400
Message-ID<p2i9gahbovhj16tmp5t5cgoac0ssfpgt7e@4ax.com>
In reply to#27026
On Sat, 14 Mar 2015 04:15:24 -0700 (PDT), jonas.thornvall@gmail.com wrote
in <f9fe170d-9fdf-42bf-9fca-23a3bc48f76c@googlegroups.com>:

>I just wrote a big numb addition function this morning, it is a bit silly and naive, not pure string not pure integer. I think it is correct, but i may have missed leading last digit.
>
>Suggestions to write it shorter and more clear, but keeping the naive approach appreciated.
<snip>

There are zillions of things to play with doing this.   :)

The first thing that comes to mind is what was on the back cover of grade
school notebooks since before The War (that's WWII) -- tables.  More
precisely addition and multiplication tables.  Simply two dimensional
arrays to give the sum or product of the two digits.  (Actually they went
up to 12 by 12.)  Rather than doing your addition just look up the sum and
carry.

And stop your laughing -- this was the method that was used in BCD machines
a half century ago!  In one computer I used, those tables were in regular
memory and I reset them to do octal arithmetic instead of decimal.

It would make more sense to make sure your strings are all numeric before
you start rather than during the calculation.  You only have to add for the
number of digits of the shorter string and then add the carry to the left
portion of the longer one and concatenate.  Alternatively, you can left pad
the shorter one with zeros to make them the same length.  That what you do
when you add manually (when learning). 

Also, pad the longer one first with a 0 and then the last carry is not
special.

Have fun.

K

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


#27041

Fromjonas.thornvall@gmail.com
Date2015-03-14 23:57 -0700
Message-ID<bc457e80-c7a0-441a-b77e-4fea0f91d7f9@googlegroups.com>
In reply to#27038
Den söndag 15 mars 2015 kl. 00:51:35 UTC+1 skrev Dr....@nyc.rr.com:
> On Sat, 14 Mar 2015 04:15:24 -0700 (PDT), jonas.thornvall@gmail.com wrote
> in <f9fe170d-9fdf-42bf-9fca-23a3bc48f76c@googlegroups.com>:
> 
> >I just wrote a big numb addition function this morning, it is a bit silly and naive, not pure string not pure integer. I think it is correct, but i may have missed leading last digit.
> >
> >Suggestions to write it shorter and more clear, but keeping the naive approach appreciated.
> <snip>
> 
> There are zillions of things to play with doing this.   :)
> 
> The first thing that comes to mind is what was on the back cover of grade
> school notebooks since before The War (that's WWII) -- tables.  More
> precisely addition and multiplication tables.  Simply two dimensional
> arrays to give the sum or product of the two digits.  (Actually they went
> up to 12 by 12.)  Rather than doing your addition just look up the sum and
> carry.
> 
> And stop your laughing -- this was the method that was used in BCD machines
> a half century ago!  In one computer I used, those tables were in regular
> memory and I reset them to do octal arithmetic instead of decimal.

But it does not hold for a more generic approach doing math in anybase?
I think my is easily adjustable passing a parameter holding base. And of course base higer than 10 would have to be encoded. I probably would use commaseparated decimals rather than an extended numeral set.

> It would make more sense to make sure your strings are all numeric before
> you start rather than during the calculation.  You only have to add for the
> number of digits of the shorter string and then add the carry to the left
> portion of the longer one and concatenate.  Alternatively, you can left pad
> the shorter one with zeros to make them the same length.  That what you do
> when you add manually (when learning). 
> 
> Also, pad the longer one first with a 0 and then the last carry is not
> special.

Oh i think i solved it and i also got many ideas of improvement from Ben.
I just needed this code after loop end, an extra check for carriage.

if (remainder==1) out=remainder+out;

I do not think i will pad, i will look for the shortest rather than longest string and break when there is no carriage, but keep the index and then loop thru copy rest of values.

> Have fun.
> 
> K

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


#27042

Fromjonas.thornvall@gmail.com
Date2015-03-15 00:08 -0700
Message-ID<6b6d69d8-e5ee-4a03-8ad1-bf3a5202cd25@googlegroups.com>
In reply to#27038
Den söndag 15 mars 2015 kl. 00:51:35 UTC+1 skrev Dr....@nyc.rr.com:
> On Sat, 14 Mar 2015 04:15:24 -0700 (PDT), jonas.thornvall@gmail.com wrote
> in <f9fe170d-9fdf-42bf-9fca-23a3bc48f76c@googlegroups.com>:
> 
> >I just wrote a big numb addition function this morning, it is a bit silly and naive, not pure string not pure integer. I think it is correct, but i may have missed leading last digit.
> >
> >Suggestions to write it shorter and more clear, but keeping the naive approach appreciated.
> <snip>
> 
> There are zillions of things to play with doing this.   :)
> 
> The first thing that comes to mind is what was on the back cover of grade
> school notebooks since before The War (that's WWII) -- tables.  More
> precisely addition and multiplication tables.  Simply two dimensional
> arrays to give the sum or product of the two digits.  (Actually they went
> up to 12 by 12.)  Rather than doing your addition just look up the sum and
> carry.
> 
> And stop your laughing -- this was the method that was used in BCD machines
> a half century ago!  In one computer I used, those tables were in regular
> memory and I reset them to do octal arithmetic instead of decimal.
> 
> It would make more sense to make sure your strings are all numeric before
> you start rather than during the calculation. 

Actually i was more in favor for skipping using the built in math operators all together, just doing string manipulations but that would required a table. And i am unsure how well such approach would have coped with changing base doing addition. I think i basicly only need exchange 10 with a variable that it a parameter that passed at function call to change base for the addition.


 You only have to add for the
> number of digits of the shorter string and then add the carry to the left
> portion of the longer one and concatenate.  Alternatively, you can left pad
> the shorter one with zeros to make them the same length.  That what you do
> when you add manually (when learning). 
> 
> Also, pad the longer one first with a 0 and then the last carry is not
> special.
> 
> Have fun.
> 
> K

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


#27047

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2015-03-15 10:46 +0000
Message-ID<fmoagatmndjst2jbipcdsqerreioj6560e@4ax.com>
In reply to#27026
On Sat, 14 Mar 2015 04:15:24 -0700 (PDT), jonas.thornvall@gmail.com
wrote:

>I just wrote a big numb addition function this morning, it is a bit silly and naive, not pure string not pure integer. I think it is correct, but i may have missed leading last digit.
>
>Suggestions to write it shorter and more clear, but keeping the naive approach appreciated.
  <snip>

Have you considered a different way of organising the system? That is
to use anybase as nothing but a display mode, and do all the internal
storage and operations on binary numbers.

Thus, if the user types in an octal number you convert it to binary
before storing it, then display the last entry in the current radix,
which could be base 13 for example.

  John

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


#27050

Fromjonas.thornvall@gmail.com
Date2015-03-15 04:12 -0700
Message-ID<84780054-0c83-4a51-a8b6-895ea4412dd5@googlegroups.com>
In reply to#27047
Den söndag 15 mars 2015 kl. 11:46:16 UTC+1 skrev John Harris:
> On Sat, 14 Mar 2015 04:15:24 -0700 (PDT), jonas.thornvall@gmail.com
> wrote:
> 
> >I just wrote a big numb addition function this morning, it is a bit silly and naive, not pure string not pure integer. I think it is correct, but i may have missed leading last digit.
> >
> >Suggestions to write it shorter and more clear, but keeping the naive approach appreciated.
>   <snip>
> 
> Have you considered a different way of organising the system? That is
> to use anybase as nothing but a display mode, and do all the internal
> storage and operations on binary numbers.
> 
> Thus, if the user types in an octal number you convert it to binary
> before storing it, then display the last entry in the current radix,
> which could be base 13 for example.
> 
>   John

No i find the challenge to highlevel emulate arithmetic, almost as working on another machine. And probably can have benefits when working with really big numbers?

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


#27053

Fromjonas.thornvall@gmail.com
Date2015-03-15 06:00 -0700
Message-ID<b438f2fb-b3f1-44e6-871b-3541acd4cff8@googlegroups.com>
In reply to#27050
Den söndag 15 mars 2015 kl. 12:13:01 UTC+1 skrev jonas.t...@gmail.com:
> Den söndag 15 mars 2015 kl. 11:46:16 UTC+1 skrev John Harris:
> > On Sat, 14 Mar 2015 04:15:24 -0700 (PDT), jonas.thornvall@gmail.com
> > wrote:
> > 
> > >I just wrote a big numb addition function this morning, it is a bit silly and naive, not pure string not pure integer. I think it is correct, but i may have missed leading last digit.
> > >
> > >Suggestions to write it shorter and more clear, but keeping the naive approach appreciated.
> >   <snip>
> > 
> > Have you considered a different way of organising the system? That is
> > to use anybase as nothing but a display mode, and do all the internal
> > storage and operations on binary numbers.
> > 
> > Thus, if the user types in an octal number you convert it to binary
> > before storing it, then display the last entry in the current radix,
> > which could be base 13 for example.
> > 
> >   John
> 
> No i find the challenge to highlevel emulate arithmetic, almost as working on another machine. And probably can have benefits when working with really big numbers?

I have a nice generic base converter for anybase that i will try to extend into bignumb. The bijective need some work.

Doing some big numb operators i hope to learn howto accomplish it.

http://jt.node365.se/bijbase.html

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


Page 1 of 2  [1] 2  Next page →

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


csiph-web