{"id":148,"date":"2009-01-08T00:00:13","date_gmt":"2009-01-08T07:00:13","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=486"},"modified":"2009-01-08T00:00:13","modified_gmt":"2009-01-08T07:00:13","slug":"adding-very-large-numbers","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=148","title":{"rendered":"Adding Very Large Numbers"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\n\/\/ tracing out large numbers will go into exponent notation\r\ntrace(1000000000000000000000000);\r\n\/\/ outputs: 1e+24\r\n\r\nvar a:String =  &quot;1000000000000000000000009&quot;;\r\nvar b:String = &quot;12000000000000000000000001&quot;;\r\n\r\n\/\/ bigAdd function adds two large numbers \r\ntrace(bigAdd(a, b));\r\n\/\/ outputs: 13000000000000000000000010\r\n\r\n\/\/ unclear what regular addition is doing here:\r\ntrace(int(a) + int(b));\r\n\/\/ outputs: -3758096384\r\n\r\nfunction bigAdd(a:String, b:String):String{\r\n       var answer:String=&quot;&quot;;\r\n       var carry:int = 0;\r\n\t   var zeros:int = 0;\r\n\t   var i:int = 0;\r\n\t   var max:int = 0;\r\n\t    \r\n\t\t\/\/ obtain max variable and prepend zeros to the shortest string\r\n\t   if (a.length &gt; b.length){\r\n\t\t  max = a.length;\r\n\t\t  zeros = max - b.length;\r\n\t\t   for (i= 0; i&lt;zeros; i++) {\r\n\t\t\t\tb = &quot;0&quot; + b;\r\n\t\t   }\r\n\t   }else if (b.length &gt;  a.length){\r\n\t\t   max = b.length;\r\n\t\t   zeros = max - a.length;\r\n\t\t   for (i= 0; i&lt;zeros; i++) {\r\n\t\t\t\ta = &quot;0&quot; + a  ;\r\n\t\t\t}\r\n\t   }else{\r\n\t\t   \/\/ a and b have same number of digets\r\n\t\t   max = a.length;   \r\n\t   }\r\n\t   \r\n\t   \/\/ add each character starting with the last (max - 1), \r\n\t   \/\/ carry the 1 if the sum has a length of 2\r\n       for (i = max - 1;  i &gt; -1; i--){ \r\n               var sum:String = String(int(a.charAt(i)) + int(b.charAt(i)) + carry);\r\n\r\n               if (sum.length == 2){\r\n                       answer = sum.charAt(1) + answer;\r\n                       carry = 1;\r\n               }else{\r\n                       carry = 0;\r\n                       answer = sum + answer;\r\n               }\r\n       }\r\n\t   if (carry == 1){\r\n           answer = 1 + answer;\r\n       }\r\n       return answer;\r\n}\r\n<\/pre>\n<p>This snippet demos a function called bigAdd(), which can be used to add very large numbers that the standard numerical datatypes (int, Number, etc) won&#8217;t really work with. It uses a string based addition algorithm.<\/p>\n<p>When I decided to write this snippet I dug around for awhile trying to find a nice elegant algorithm to port, something done without prepending zeros etc&#8230;. but I couldn&#8217;t find one that suited my needs&#8230; so I just created this one from scratch. There&#8217;s room for optimization and improvement but it works well enough for just playing around.<\/p>\n<h1>BigDecimal &#038; BigInteger<\/h1>\n<p>A few months back I needed to do some programming with very large numbers. I dug around for awhile and eventually found out that java has a built in <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/math\/BigInteger.html\">BigInteger<\/a> and<a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/math\/BigDecimal.html\">BigDecimal<\/a> class&#8230;. I used java to write the program I had in mind, but thought it would be fun to eventually create something similar for ActionScript&#8230;. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ tracing out large numbers will go into exponent notation trace(1000000000000000000000000); \/\/ outputs: 1e+24 var a:String = &quot;1000000000000000000000009&quot;; var b:String = &quot;12000000000000000000000001&quot;; \/\/ bigAdd function adds two large numbers trace(bigAdd(a, b)); \/\/ outputs: 13000000000000000000000010 \/\/ unclear what regular addition is doing here: trace(int(a) + int(b)); \/\/ outputs: -3758096384 function bigAdd(a:String, b:String):String{ var answer:String=&quot;&quot;; var carry:int [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[94,41,20,21],"tags":[3,111,112,4,113],"class_list":["post-148","post","type-post","status-publish","format-standard","hentry","category-misc","category-number","category-string-manipulation","category-strings","tag-actionscript","tag-bigdecimal","tag-biginteger","tag-flash","tag-large-numbers"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=148"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/148\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}