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 = typeof(Roo) != 'undefined' ? Roo :  { util : { }};
7     
8
9 Roo.util.Math = function(num, precision, roundType){
10         var i;
11         if(num instanceof Roo.util.Math){
12         var o = this;
13                 for(i in {precision: 0, roundType: 0, _s: 0, _f: 0}) o[i] = num[i];
14                 this._d = num._d.slice();
15                 return;
16         }
17     
18         this.precision = isNaN(precision = Math.abs(precision)) ? Roo.util.Math.defaultPrecision : precision;
19         this.roundType = isNaN(r = Math.abs(roundType)) ? Roo.util.Math.defaultRoundType : roundType;
20     
21         this._s = (num += "").charAt(0) == "-";
22         this._f = (
23             (num = num.replace(/[^\d.]/g, "").split(".", 2))[0] = num[0].replace(/^0+/, "") || "0"
24         ).length;
25         for(i = (num = this._d = (num.join("") || "0").split("")).length; i; num[--i] = +num[i]);
26         this.round();
27 };
28
29 Roo.util.Math.ROUND_HALF_EVEN = (Roo.util.Math.ROUND_HALF_DOWN = (Roo.util.Math.ROUND_HALF_UP =
30     (Roo.util.Math.ROUND_FLOOR = (Roo.util.Math.ROUND_CEIL = (Roo.util.Math.ROUND_DOWN 
31     = (Roo.util.Math.ROUND_UP = 0) + 1) + 1) + 1) + 1) + 1) + 1;
32     
33 Roo.util.Math.defaultPrecision = 40;
34 Roo.util.Math.defaultRoundType = Roo.util.Math.ROUND_HALF_UP;
35
36     
37 Roo.util.Math.prototype = {
38     _s : 0,
39     _f : 0,
40     roundType : 0,
41     precision : 0,
42     add : function(num)
43     {
44                 num = new Roo.util.Math(num, this.precision, this.roundType);
45         
46         if (this._s != num._s) { //netagive...
47             return num._s ^= 1, this.subtract(num);
48                 }
49         
50         var o = new Roo.util.Math(this), 
51             a = o._d, 
52             b = num._d, 
53             la = o._f,
54             lb = num._f, 
55             num = Math.max(la, lb), 
56             i, r;
57                 
58         la != lb && ((lb = la - lb) > 0 ? o._zeroes(b, lb, 1) : o._zeroes(a, -lb, 1));
59                 i = (la = a.length) == (lb = b.length) ?
60                 a.length : (
61                     (lb = la - lb) > 0 ? o._zeroes(b, lb) : o._zeroes(a, -lb)
62                 ).length;
63                 
64                 for(r = 0; i; 
65             r = (a[--i] = a[i] + b[i] + r) / 10 >>> 0, 
66             a[i] %= 10
67         );
68         r && ++num && a.unshift(r);
69         o._f = num;
70         return o.round();
71                  
72         },
73         subtract : function(n){
74                 if(this._s != (n = new Roo.util.Math(n, this.precision, this.roundType))._s)
75                         return n._s ^= 1, this.add(n);
76                 var o = new Roo.util.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;
77                 a = a._d, b = b._d, la != lb && ((lb = la - lb) > 0 ? o._zeroes(b, lb, 1) : o._zeroes(a, -lb, 1));
78                 for(i = (la = a.length) == (lb = b.length) ? a.length : ((lb = la - lb) > 0 ? o._zeroes(b, lb) : o._zeroes(a, -lb)).length; i;){
79                         if(a[--i] < b[i]){
80                                 for(j = i; j && !a[--j]; a[j] = 9);
81                                 --a[j], a[i] += 10;
82                         }
83                         b[i] = a[i] - b[i];
84                 }
85                 return c || (o._s ^= 1), o._f = d, o._d = b, o.round();
86         },
87         multiply : function(n)
88     {
89                 var o = new Roo.util.Math(this), 
90             r = o._d.length >= (n = new Roo.util.Math(n, this.precision, this.roundType))._d.length,
91             a = (r ? o : n)._d,
92             b = (r ? n : o)._d, 
93             la = a.length, 
94             lb = b.length, 
95             x = new Roo.util.Math(0,this.precision, this.roundType), 
96             i, j, s;
97             
98                 for(i = lb;   i; 
99                 r && s.unshift(r), 
100                 x.set(x.add(new Roo.util.Math(s.join(""), this.precision, this.roundType)))
101         ) {
102             
103             s = (new Array(lb - --i)).join("0").split("");
104             j = la;
105             for( r = 0 ; j ; r += a[--j] * b[i]) {
106                 s.unshift(r % 10);
107                 r = (r / 10) >>> 0);
108             }
109         }
110         
111         console.log(o);
112         
113                 o._s = o._s != n._s;
114         o._f = (
115                     (r = la + lb - o._f - n._f) >= (j = (o._d = x._d).length) ? 
116                         this._zeroes(o._d, r - j + 1, 1).length : 
117                         j
118             ) - r; 
119             
120         return  o.round();
121         },
122     
123         divide : function(n){
124                 if((n = new Roo.util.Math(n, this.precision, this.roundType)) == "0")
125                         throw new Error("Division by 0");
126                 else if(this == "0")
127                         return new Roo.util.Math(0, this.precision, this.roundType);
128                 var o = new Roo.util.Math(this), a = o._d, b = n._d, la = a.length - o._f,
129                 lb = b.length - n._f, r = new Roo.util.Math(0, this.precision, this.roundType), i = 0, j, s, l, f = 1, c = 0, e = 0;
130                 r._s = o._s != n._s, r.precision = Math.max(o.precision, n.precision),
131                 r._f = +r._d.pop(), la != lb && o._zeroes(la > lb ? b : a, Math.abs(la - lb));
132                 n._f = b.length, b = n, b._s = false, b = b.round();
133                 for(n = new Roo.util.Math(0, this.precision, this.roundType); a[0] == "0"; a.shift());
134                 out:
135                 do{
136                         for(l = c = 0, n == "0" && (n._d = [], n._f = 0); i < a.length && n.compare(b) == -1; ++i){
137                                 (l = i + 1 == a.length, (!f && ++c > 1 || (e = l && n == "0" && a[i] == "0")))
138                                 && (r._f == r._d.length && ++r._f, r._d.push(0));
139                                 (a[i] == "0" && n == "0") || (n._d.push(a[i]), ++n._f);
140                                 if(e)
141                                         break out;
142                                 if((l && n.compare(b) == -1 && (r._f == r._d.length && ++r._f, 1)) || (l = 0))
143                                         while(r._d.push(0), n._d.push(0), ++n._f, n.compare(b) == -1);
144                         }
145                         if(f = 0, n.compare(b) == -1 && !(l = 0))
146                                 while(l ? r._d.push(0) : l = 1, n._d.push(0), ++n._f, n.compare(b) == -1);
147                         for(s = new Roo.util.Math(0, this.precision, this.roundType), j = 0; n.compare(y = s.add(b)) + 1 && ++j; s.set(y));
148                         n.set(n.subtract(s)), !l && r._f == r._d.length && ++r._f, r._d.push(j);
149                 }
150                 while((i < a.length || n != "0") && (r._d.length - r._f) <= r.precision);
151                 return r.round();
152         },
153         mod : function(n){
154                 return this.subtract(this.divide(n).intPart().multiply(n));
155         },
156         pow : function(n){
157                 var o = new Roo.util.Math(this), i;
158                 if((n = (new Roo.util.Math(n, this.precision, this.roundType)).intPart()) == 0) return o.set(1);
159                 for(i = Math.abs(n); --i; o.set(o.multiply(this)));
160                 return n < 0 ? o.set((new Roo.util.Math(1, this.precision, this.roundType)).divide(o)) : o;
161         },
162         set : function(n)
163     {
164                 this.constructor(n)
165         return this;
166         },
167         compare : function(n){
168                 var a = this, la = this._f, b = new Roo.util.Math(n, this.precision, this.roundType), lb = b._f, r = [-1, 1], i, l;
169                 if(a._s != b._s)
170                         return a._s ? -1 : 1;
171                 if(la != lb)
172                         return r[(la > lb) ^ a._s];
173                 for(la = (a = a._d).length, lb = (b = b._d).length, i = -1, l = Math.min(la, lb); ++i < l;)
174                         if(a[i] != b[i])
175                                 return r[(a[i] > b[i]) ^ a._s];
176                 return la != lb ? r[(la > lb) ^ a._s] : 0;
177         },
178         negate : function(){
179                 var n = new Roo.util.Math(this); 
180         return n._s ^= 1, n;
181         },
182         abs : function(){
183                 var n = new Roo.util.Math(this);
184         return n._s = 0, n;
185         },
186         intPart : function(){
187                 return new Roo.util.Math((this._s ? "-" : "", this.precision, this.roundType) + (this._d.slice(0, this._f).join("") || "0"));
188         },
189     valueOf : function() {
190         return this.toString();
191     },
192         toString : function(){
193                 var o = this;
194                 return (o._s ? "-" : "") + (o._d.slice(0, o._f).join("") || "0") + (o._f != o._d.length ? "." + o._d.slice(o._f).join("") : "");
195         },
196     toFixed : function(x) {
197         var n = new Roo.util.Math(this);
198         n.precision = x;
199         return n.round().toString();
200     },
201         _zeroes : function(n, l, t){
202                 var s = ["push", "unshift"][t || 0];
203                 for(++l; --l;  n[s](0));
204                 return n;
205         },
206         round : function()
207     {
208         var m = Roo.util.Math;
209                 
210         if (typeof(m._rounding) != 'undefined') return this; // stop recursion..
211         m._rounding = true;
212                 
213         var   r = this.roundType,
214             b = this._d, 
215             d, p, n, x;
216         //console.log(b.join(','));    
217                 while( this._f > 1 && !b[0]) {
218             --this._f;
219             b.shift();
220         }
221                 //console.log(b.join(','));    
222         for(d = this._f, 
223             p = this.precision + d, 
224             n = b[p];
225             
226             b.length > d && !b[b.length -1]; // condition... 
227          
228             b.pop()
229         );
230             
231                 x = (this._s ? "-" : "") + (p - d ? "0." + this._zeroes([], p - d - 1).join("") : "") + 1;
232         
233        
234
235   
236         if(b.length > p && n){
237             //console.log("rounding" +n + " Method? " + r);
238                         if (
239                 (r == m.ROUND_UP) ||
240                 (r == m.ROUND_CEIL && !this._s) ||
241                 (r == m.ROUND_FLOOR &&  this._s) ||
242                 (r == m.ROUND_HALF_UP &&   n >= 5) ||
243                 (r == m.ROUND_HALF_DOWN &&  n > 5)  ||
244                 (r == m.ROUND_HALF_EVEN && n >= 5 && (b[p - 1] & 1))
245                ) {
246                 //console.log("add" +x);
247                // this.precision++;
248                 var ret = this.add(x);
249                 this._d = ret._d;
250                 b=ret._d;
251                 //this.precision--;
252             }
253
254                         b.splice(p, b.length - p);
255                 }
256                 return delete m._rounding, this;
257         }
258 };