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