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