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


Groups > comp.lang.javascript > #27045

Re: Bignumb addition function, suggestion improvements.

Newsgroups comp.lang.javascript
Date 2015-03-15 03:07 -0700
References (1 earlier) <87fv97u25x.fsf@bsb.me.uk> <531f97ee-89e2-49a6-b9a8-4b11102e998c@googlegroups.com> <878uezpdv6.fsf@bsb.me.uk> <c4dc6cf8-5e96-4385-9e38-f90c0602a92e@googlegroups.com> <af2d0a0c-ec2a-4296-8df9-12b29564add7@googlegroups.com>
Message-ID <27efb621-9bcd-49bd-99fb-b16396f9b17c@googlegroups.com> (permalink)
Subject Re: Bignumb addition function, suggestion improvements.
From jonas.thornvall@gmail.com

Show all headers | View raw


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.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web