Roo/util/Math.js
[roojs1] / Roo / util / Math.js
1 //<script type="text/javascript">
2 /**
3 //+ based on.. Jonas Raoni Soares Silva
4 //@ http://jsfromhell.com/classes/bignumber [rev. #4]
5 **/
6
7
8 BigNumber = function(num, precision, roundType){
9         var i;
10         if(num instanceof BigNumber){
11                 for(i in {precision: 0, roundType: 0, _s: 0, _f: 0}) o[i] = num[i];
12                 this._d = num._d.slice();
13                 return;
14         }
15     
16         this.precision = isNaN(precision = Math.abs(precision)) ? BigNumber.defaultPrecision : precision;
17         this.roundType = isNaN(r = Math.abs(roundType)) ? BigNumber.defaultRoundType : roundType;
18     
19         this._s = (num += "").charAt(0) == "-";
20         this._f = (
21             (num = num.replace(/[^\d.]/g, "").split(".", 2))[0] = num[0].replace(/^0+/, "") || "0"
22         ).length;
23         for(i = (num = this._d = (num.join("") || "0").split("")).length; i; num[--i] = +num[i]);
24         this.round();
25 };
26
27 BigNumber.ROUND_HALF_EVEN = (BigNumber.ROUND_HALF_DOWN = (BigNumber.ROUND_HALF_UP =
28     (BigNumber.ROUND_FLOOR = (BigNumber.ROUND_CEIL = (BigNumber.ROUND_DOWN 
29     = (BigNumber.ROUND_UP = 0) + 1) + 1) + 1) + 1) + 1) + 1;
30     
31 BigNumber.defaultPrecision = 40;
32 BigNumber.defaultRoundType = BigNumber.ROUND_HALF_UP;
33
34     
35 BigNumber.prototype = {
36     _s : 0,
37     _f : 0
38     roundType : 0,
39     precision : 0,
40     add : function(num)
41     {
42                 num = new BigNumber(num, this.precision, this.roundType);
43         if (this._s != num._s) { //netagive...
44             return num._s ^= 1, this.subtract(num);
45                 }
46         var o = new BigNumber(this), 
47             a = o._d, 
48             b = num._d, 
49             la = o._f,
50             lb = num._f, 
51             num = Math.max(la, lb), 
52             i, r;
53                 
54         la != lb && ((lb = la - lb) > 0 ? o._zeroes(b, lb, 1) : o._zeroes(a, -lb, 1));
55                 i = (la = a.length) == (lb = b.length) ?
56                 a.length : (
57                     (lb = la - lb) > 0 ? o._zeroes(b, lb) : o._zeroes(a, -lb)
58                 ).length;
59                 
60                 for(r = 0; i; r = (a[--i] = a[i] + b[i] + r) / 10 >>> 0, a[i] %= 10);
61                 return r && ++num && a.unshift(r), o._f = num, o.round();
62         },
63         subtract : function(n){
64                 if(this._s != (n = new BigNumber(n))._s)
65                         return n._s ^= 1, this.add(n);
66                 var o = new BigNumber(this), c = o.abs().compare(n.abs()) + 1, a = c ? o : n, b = c ? n : o, la = a._f, lb = b._f, d = la, i, j;
67                 a = a._d, b = b._d, la != lb && ((lb = la - lb) > 0 ? o._zeroes(b, lb, 1) : o._zeroes(a, -lb, 1));
68                 for(i = (la = a.length) == (lb = b.length) ? a.length : ((lb = la - lb) > 0 ? o._zeroes(b, lb) : o._zeroes(a, -lb)).length; i;){
69                         if(a[--i] < b[i]){
70                                 for(j = i; j && !a[--j]; a[j] = 9);
71                                 --a[j], a[i] += 10;
72                         }
73                         b[i] = a[i] - b[i];
74                 }
75                 return c || (o._s ^= 1), o._f = d, o._d = b, o.round();
76         },
77         multiply : function(n){
78                 var o = new BigNumber(this), r = o._d.length >= (n = new BigNumber(n))._d.length, a = (r ? o : n)._d,
79                 b = (r ? n : o)._d, la = a.length, lb = b.length, x = new BigNumber, i, j, s;
80                 for(i = lb; i; r && s.unshift(r), x.set(x.add(new BigNumber(s.join("")))))
81                         for(s = (new Array(lb - --i)).join("0").split(""), r = 0, j = la; j; r += a[--j] * b[i], s.unshift(r % 10), r = (r / 10) >>> 0);
82                 return o._s = o._s != n._s, o._f = ((r = la + lb - o._f - n._f) >= (j = (o._d = x._d).length) ? this._zeroes(o._d, r - j + 1, 1).length : j) - r, o.round();
83         },
84         divide : function(n){
85                 if((n = new BigNumber(n)) == "0")
86                         throw new Error("Division by 0");
87                 else if(this == "0")
88                         return new BigNumber;
89                 var o = new BigNumber(this), a = o._d, b = n._d, la = a.length - o._f,
90                 lb = b.length - n._f, r = new BigNumber, i = 0, j, s, l, f = 1, c = 0, e = 0;
91                 r._s = o._s != n._s, r.precision = Math.max(o.precision, n.precision),
92                 r._f = +r._d.pop(), la != lb && o._zeroes(la > lb ? b : a, Math.abs(la - lb));
93                 n._f = b.length, b = n, b._s = false, b = b.round();
94                 for(n = new BigNumber; a[0] == "0"; a.shift());
95                 out:
96                 do{
97                         for(l = c = 0, n == "0" && (n._d = [], n._f = 0); i < a.length && n.compare(b) == -1; ++i){
98                                 (l = i + 1 == a.length, (!f && ++c > 1 || (e = l && n == "0" && a[i] == "0")))
99                                 && (r._f == r._d.length && ++r._f, r._d.push(0));
100                                 (a[i] == "0" && n == "0") || (n._d.push(a[i]), ++n._f);
101                                 if(e)
102                                         break out;
103                                 if((l && n.compare(b) == -1 && (r._f == r._d.length && ++r._f, 1)) || (l = 0))
104                                         while(r._d.push(0), n._d.push(0), ++n._f, n.compare(b) == -1);
105                         }
106                         if(f = 0, n.compare(b) == -1 && !(l = 0))
107                                 while(l ? r._d.push(0) : l = 1, n._d.push(0), ++n._f, n.compare(b) == -1);
108                         for(s = new BigNumber, j = 0; n.compare(y = s.add(b)) + 1 && ++j; s.set(y));
109                         n.set(n.subtract(s)), !l && r._f == r._d.length && ++r._f, r._d.push(j);
110                 }
111                 while((i < a.length || n != "0") && (r._d.length - r._f) <= r.precision);
112                 return r.round();
113         },
114         mod : function(n){
115                 return this.subtract(this.divide(n).intPart().multiply(n));
116         },
117         pow : function(n){
118                 var o = new BigNumber(this), i;
119                 if((n = (new BigNumber(n)).intPart()) == 0) return o.set(1);
120                 for(i = Math.abs(n); --i; o.set(o.multiply(this)));
121                 return n < 0 ? o.set((new BigNumber(1)).divide(o)) : o;
122         },
123         set : function(n){
124                 return this.constructor(n), this;
125         },
126         compare : function(n){
127                 var a = this, la = this._f, b = new BigNumber(n), lb = b._f, r = [-1, 1], i, l;
128                 if(a._s != b._s)
129                         return a._s ? -1 : 1;
130                 if(la != lb)
131                         return r[(la > lb) ^ a._s];
132                 for(la = (a = a._d).length, lb = (b = b._d).length, i = -1, l = Math.min(la, lb); ++i < l;)
133                         if(a[i] != b[i])
134                                 return r[(a[i] > b[i]) ^ a._s];
135                 return la != lb ? r[(la > lb) ^ a._s] : 0;
136         },
137         negate : function(){
138                 var n = new BigNumber(this); return n._s ^= 1, n;
139         },
140         abs : function(){
141                 var n = new BigNumber(this); return n._s = 0, n;
142         },
143         intPart : function(){
144                 return new BigNumber((this._s ? "-" : "") + (this._d.slice(0, this._f).join("") || "0"));
145         },
146     valueOf : function() {
147         return this.toString();
148     }
149         toString : function(){
150                 var o = this;
151                 return (o._s ? "-" : "") + (o._d.slice(0, o._f).join("") || "0") + (o._f != o._d.length ? "." + o._d.slice(o._f).join("") : "");
152         }
153         _zeroes : function(n, l, t){
154                 var s = ["push", "unshift"][t || 0];
155                 for(++l; --l;  n[s](0));
156                 return n;
157         },
158         round : function(){
159                 if("_rounding" in this) return this;
160                 var BigNumber = BigNumber, r = this.roundType, b = this._d, d, p, n, x;
161                 for(this._rounding = true; this._f > 1 && !b[0]; --this._f, b.shift());
162                 for(d = this._f, p = this.precision + d, n = b[p]; b.length > d && !b[b.length -1]; b.pop());
163                 x = (this._s ? "-" : "") + (p - d ? "0." + this._zeroes([], p - d - 1).join("") : "") + 1;
164                 if(b.length > p){
165                         n && (r == BigNumber.DOWN ? false : r == BigNumber.UP ? true : r == BigNumber.CEIL ? !this._s
166                         : r == BigNumber.FLOOR ? this._s : r == BigNumber.HALF_UP ? n >= 5 : r == BigNumber.HALF_DOWN ? n > 5
167                         : r == BigNumber.HALF_EVEN ? n >= 5 && b[p - 1] & 1 : false) && this.add(x);
168                         b.splice(p, b.length - p);
169                 }
170                 return delete this._rounding, this;
171         }
172 })