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


Groups > comp.lang.javascript > #27038

Re: Bignumb addition function, suggestion improvements.

From Dr.Kral@nyc.rr.com
Newsgroups comp.lang.javascript
Subject Re: Bignumb addition function, suggestion improvements.
Message-ID <p2i9gahbovhj16tmp5t5cgoac0ssfpgt7e@4ax.com> (permalink)
References <f9fe170d-9fdf-42bf-9fca-23a3bc48f76c@googlegroups.com>
Organization Easynews - www.easynews.com
Date 2015-03-14 19:51 -0400

Show all headers | View raw


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

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