1 //<script type="text/javascript">
3 //+ based on.. Jonas Raoni Soares Silva
4 //@ http://jsfromhell.com/classes/bignumber [rev. #4]
7 Roo = typeof(Roo) != 'undefined' ? Roo : { util : { }};
10 * @class Roo.util.Math
11 * based on.. Jonas Raoni Soares Silva
12 * http://jsfromhell.com/classes/bignumber [rev. #4]
14 * @param {Object|Number} number to wrap
15 * @param {number} precision to use when doing calculations
16 * @param {number} rounding type.
19 Roo.util.Math = function(num, precision, roundType){
20 this.ctor(num,precision, roundType);
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;
27 Roo.util.Math.defaultPrecision = 40;
28 Roo.util.Math.defaultRoundType = Roo.util.Math.ROUND_HALF_UP;
31 Roo.util.Math.prototype = {
38 ctor : function (num, precision, roundType) {
40 if(num instanceof Roo.util.Math){
42 for(i in {precision: 0, roundType: 0, _s: 0, _f: 0}) o[i] = num[i];
43 this._d = num._d.slice();
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;
50 this._s = (num += "").charAt(0) == "-";
52 (num = num.replace(/[^\d.]/g, "").split(".", 2))[0] = num[0].replace(/^0+/, "") || "0"
54 for(i = (num = this._d = (num.join("") || "0").split("")).length; i; num[--i] = +num[i]);
59 * @param {Object|Number} value to add
60 * @return {Object} The result
64 num = new Roo.util.Math(num, this.precision, this.roundType);
66 if (this._s != num._s) { //netagive...
67 return num._s ^= 1, this.subtract(num);
70 var o = new Roo.util.Math(this),
75 num = Math.max(la, lb),
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) ?
81 (lb = la - lb) > 0 ? o._zeroes(b, lb) : o._zeroes(a, -lb)
85 r = (a[--i] = a[i] + b[i] + r) / 10 >>> 0,
88 r && ++num && a.unshift(r);
96 * @param {Object|Number} value to subtract
97 * @return {Object} The result
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;){
107 for(j = i; j && !a[--j]; a[j] = 9);
112 return c || (o._s ^= 1), o._f = d, o._d = b, o.round();
118 * @param {Object|Number} value to mutiply
119 * @return {Object} The result
121 multiply : function(n)
123 var o = new Roo.util.Math(this),
124 r = o._d.length >= (n = new Roo.util.Math(n, this.precision, this.roundType))._d.length,
129 x = new Roo.util.Math(0,this.precision, this.roundType),
134 x.set(x.add(new Roo.util.Math(s.join(""), this.precision, this.roundType)))
137 s = (new Array(lb - --i)).join("0").split(""); // pads 000...
139 for( r = 0 ; j ; r += a[--j] * b[i], s.unshift(r % 10), r = (r / 10) >>> 0);
148 (r = la + lb - o._f - n._f) >= (j = (o._d = x._d).length) ?
149 this._zeroes(o._d, r - j + 1, 1).length :
156 divide : function(n){
157 if((n = new Roo.util.Math(n, this.precision, this.roundType)) == "0")
158 throw new Error("Division by 0");
160 return new Roo.util.Math(0, this.precision, this.roundType);
161 var o = new Roo.util.Math(this), a = o._d, b = n._d, la = a.length - o._f,
162 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;
163 r._s = o._s != n._s, r.precision = Math.max(o.precision, n.precision),
164 r._f = +r._d.pop(), la != lb && o._zeroes(la > lb ? b : a, Math.abs(la - lb));
165 n._f = b.length, b = n, b._s = false, b = b.round();
166 for(n = new Roo.util.Math(0, this.precision, this.roundType); a[0] == "0"; a.shift());
169 for(l = c = 0, n == "0" && (n._d = [], n._f = 0); i < a.length && n.compare(b) == -1; ++i){
170 (l = i + 1 == a.length, (!f && ++c > 1 || (e = l && n == "0" && a[i] == "0")))
171 && (r._f == r._d.length && ++r._f, r._d.push(0));
172 (a[i] == "0" && n == "0") || (n._d.push(a[i]), ++n._f);
175 if((l && n.compare(b) == -1 && (r._f == r._d.length && ++r._f, 1)) || (l = 0))
176 while(r._d.push(0), n._d.push(0), ++n._f, n.compare(b) == -1);
178 if(f = 0, n.compare(b) == -1 && !(l = 0))
179 while(l ? r._d.push(0) : l = 1, n._d.push(0), ++n._f, n.compare(b) == -1);
180 for(s = new Roo.util.Math(0, this.precision, this.roundType), j = 0; n.compare(y = s.add(b)) + 1 && ++j; s.set(y));
181 n.set(n.subtract(s)), !l && r._f == r._d.length && ++r._f, r._d.push(j);
183 while((i < a.length || n != "0") && (r._d.length - r._f) <= r.precision);
189 * @param {Object|Number} value to modulus by
190 * @return {Object} The result
194 return this.subtract(this.divide(n).intPart().multiply(n));
199 * @param {Object|Number} value to power by
200 * @return {Object} The result
203 var o = new Roo.util.Math(this), i;
204 if((n = (new Roo.util.Math(n, this.precision, this.roundType)).intPart()) == 0) return o.set(1);
205 for(i = Math.abs(n); --i; o.set(o.multiply(this)));
206 return n < 0 ? o.set((new Roo.util.Math(1, this.precision, this.roundType)).divide(o)) : o;
210 * @param {Object|Number} value to set object to
211 * @return {Object} This
221 * @param {Object|Number} value to compare to
222 * @return {boolean} true if the same
225 compare : function(n){
226 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 return a._s ? -1 : 1;
230 return r[(la > lb) ^ a._s];
231 for(la = (a = a._d).length, lb = (b = b._d).length, i = -1, l = Math.min(la, lb); ++i < l;)
233 return r[(a[i] > b[i]) ^ a._s];
234 return la != lb ? r[(la > lb) ^ a._s] : 0;
238 * @return {Object} the result
241 var n = new Roo.util.Math(this);
246 * @return {Object} the result
249 var n = new Roo.util.Math(this);
253 * integer part of number
254 * @return {Object} the result
256 intPart : function(){
257 return new Roo.util.Math((this._s ? "-" : "", this.precision, this.roundType) + (this._d.slice(0, this._f).join("") || "0"));
261 * @return {String} the result
263 valueOf : function() {
264 return this.toString();
267 * value of the number
268 * @return {String} the result
270 toString : function(){
272 return (o._s ? "-" : "") + (o._d.slice(0, o._f).join("") || "0") + (o._f != o._d.length ? "." + o._d.slice(o._f).join("") : "");
275 * value of the number at a fixed precission
276 * @return {String} the result
278 toFixed : function(x) {
279 var n = new Roo.util.Math(this);
281 var ret= n.round().toString();
282 var lr =ret.split('.');
287 var r = lr.length ? lr[0] : '';
288 for (var i = r.length; i < x; i++) {
295 _zeroes : function(n, l, t){
296 var s = ["push", "unshift"][t || 0];
297 for(++l; --l; n[s](0));
303 var m = Roo.util.Math;
305 if (typeof(m._rounding) != 'undefined') return this; // stop recursion..
308 var r = this.roundType,
311 //console.log(b.join(','));
312 while( this._f > 1 && !b[0]) {
316 //console.log(b.join(','));
318 p = this.precision + d,
321 b.length > d && !b[b.length -1]; // condition...
326 x = (this._s ? "-" : "") + (p - d ? "0." + this._zeroes([], p - d - 1).join("") : "") + 1;
330 if(b.length > p && typeof(n) != 'undefined') {
331 //if(b.length > p && n){
332 //console.log("rounding" +n + " Method? " + r);
335 (r == m.ROUND_CEIL && !this._s) ||
336 (r == m.ROUND_FLOOR && this._s) ||
337 (r == m.ROUND_HALF_UP && n >= 5) ||
338 (r == m.ROUND_HALF_DOWN && n > 5) ||
339 (r == m.ROUND_HALF_EVEN && n >= 5 && (b[p - 1] & 1))
341 //console.log("add" +x);
343 var ret = this.add(x);
349 b.splice(p, b.length - p);
351 return delete m._rounding, this;