Category Archives: misc

Adding Very Large Numbers

Actionscript:
  1. // tracing out large numbers will go into exponent notation
  2. trace(1000000000000000000000000);
  3. // outputs: 1e+24
  4.  
  5. var a:String =  "1000000000000000000000009";
  6. var b:String = "12000000000000000000000001";
  7.  
  8. // bigAdd function adds two large numbers
  9. trace(bigAdd(a, b));
  10. // outputs: 13000000000000000000000010
  11.  
  12. // unclear what regular addition is doing here:
  13. trace(int(a) + int(b));
  14. // outputs: -3758096384
  15.  
  16. function bigAdd(a:String, b:String):String{
  17.        var answer:String="";
  18.        var carry:int = 0;
  19.        var zeros:int = 0;
  20.        var i:int = 0;
  21.        var max:int = 0;
  22.        
  23.         // obtain max variable and prepend zeros to the shortest string
  24.        if (a.length> b.length){
  25.           max = a.length;
  26.           zeros = max - b.length;
  27.            for (i= 0; i<zeros; i++) {
  28.                 b = "0" + b;
  29.            }
  30.        }else if (b.length>  a.length){
  31.            max = b.length;
  32.            zeros = max - a.length;
  33.            for (i= 0; i<zeros; i++) {
  34.                 a = "0" + a  ;
  35.             }
  36.        }else{
  37.            // a and b have same number of digets
  38.            max = a.length;  
  39.        }
  40.        
  41.        // add each character starting with the last (max - 1),
  42.        // carry the 1 if the sum has a length of 2
  43.        for (i = max - 1;  i> -1; i--){
  44.                var sum:String = String(int(a.charAt(i)) + int(b.charAt(i)) + carry);
  45.  
  46.                if (sum.length == 2){
  47.                        answer = sum.charAt(1) + answer;
  48.                        carry = 1;
  49.                }else{
  50.                        carry = 0;
  51.                        answer = sum + answer;
  52.                }
  53.        }
  54.        if (carry == 1){
  55.            answer = 1 + answer;
  56.        }
  57.        return answer;
  58. }

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't really work with. It uses a string based addition algorithm.

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.... but I couldn't find one that suited my needs... so I just created this one from scratch. There's room for optimization and improvement but it works well enough for just playing around.

BigDecimal & BigInteger

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 BigInteger andBigDecimal class.... I used java to write the program I had in mind, but thought it would be fun to eventually create something similar for ActionScript....

Also posted in Number, string manipulation, strings | Tagged , , , , | 2 Comments

E?

Actionscript:
  1. trace(Math.pow((1+1/10000000),10000000));
  2.  
  3. trace(Math.E);
  4.  
  5. /*outputs :
  6.  
  7. 2.7182816939803724
  8. 2.718281828459045
  9.  
  10. */

E

2.7182818284590452353602874713526624977572470936999595749669676277240766303535
47594571382178525166427427466391932003059921817413596629043572900334295260595630
73813232862794349076323382988075319525101901157383418793070215408914993488416750
92447614606680822648001684774118537423454424371075390777449920695517027618386062
61331384583000752044933826560297606737113200709328709127443747047230696977209310
14169283681902551510865746377211125238978442505695369677078......

Also posted in one-liners | Tagged , | Comments closed

Clamp Math.max(Math.min())

Actionscript:
  1. // this one-liner is good to know:
  2. // Math.max(minValue, Math.min(maxValue, valueToClamp));
  3.  
  4. // wrapped in a function it looks like this:
  5. const MIN:Number = 0;
  6. const MAX:Number = 100;
  7.  
  8. function clamp(val:Number, min:Number = MIN, max:Number = MAX){
  9.     return Math.max(min, Math.min(max, val))
  10. }
  11.  
  12. for (var i:int = 0; i<1000; i++){
  13.     var val:Number = Math.random()*1000 - 500;
  14.     // clamp the above random value between -100 and +100
  15.     trace(clamp(val, -100, 100));
  16. }

Although I show a function implementation above, I use this technique inline a good deal. If there is some value that I need to clamp I'll often just "max min it":

Actionscript:
  1. var val:Number = Math.random()*1000;
  2. // clamp it - 0-100
  3. trace(Math.max(0, Math.min(100, val));

Also posted in one-liners | Tagged , | 1 Comment

x2

Actionscript:
  1. var c:Number = 1;
  2. addEventListener(Event.ENTER_FRAME, function(){c *= 2,  trace(c)});

and....
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
4294967296
8589934592
17179869184
34359738368
68719476736
137438953472
274877906944
549755813888
1099511627776
2199023255552
4398046511104
8796093022208
17592186044416
35184372088832
70368744177664
140737488355328
281474976710656
562949953421312
1125899906842624
2251799813685248
4503599627370496
9007199254740992
18014398509481984
36028797018963970
72057594037927940
144115188075855870
288230376151711740
576460752303423500
1152921504606847000
2305843009213694000
4611686018427388000
9223372036854776000
18446744073709552000
36893488147419103000
73786976294838210000
147573952589676410000
295147905179352830000
590295810358705700000

etc...

Posted in misc | Tagged , | Leave a comment