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