[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.javascript

Bignumb addition function, suggestion improvements.

JT

3/14/2015 11:15:00 AM

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>
21 Answers

JT

3/14/2015 11:41:00 AM

0

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.

JT

3/14/2015 11:56:00 AM

0

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?).

Ben Bacarisse

3/14/2015 1:42:00 PM

0

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.

JT

3/14/2015 5:18:00 PM

0

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.

JT

3/14/2015 5:57:00 PM

0

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.

Ben Bacarisse

3/14/2015 7:40:00 PM

0

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/Conditiona...

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#...(--)

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

Thomas 'PointedEars' Lahn

3/14/2015 7:56:00 PM

0

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/Conditiona...
>
> 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%7C...

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

Dr.Kral

3/14/2015 11:51:00 PM

0

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

JT

3/15/2015 6:57:00 AM

0

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

JT

3/15/2015 7:08:00 AM

0

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