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