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