nick4fake

Untitled

Feb 5th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.87 KB | None | 0 0
  1. BigNumber = function(n, p, r){
  2.     var o = this, i;
  3.     if(n instanceof BigNumber){
  4.         for(i in {precision: 0, roundType: 0, _s: 0, _f: 0}) o[i] = n[i];
  5.         o._d = n._d.slice();
  6.         return;
  7.     }
  8.     o.precision = isNaN(p = Math.abs(p)) ? BigNumber.defaultPrecision : p;
  9.     o.roundType = isNaN(r = Math.abs(r)) ? BigNumber.defaultRoundType : r;
  10.     o._s = (n += "").charAt(0) == "-";
  11.     o._f = ((n = n.replace(/[^\d.]/g, "").split(".", 2))[0] = n[0].replace(/^0+/, "") || "0").length;
  12.     for(i = (n = o._d = (n.join("") || "0").split("")).length; i; n[--i] = +n[i]);
  13.     o.round();
  14. };
  15. with({$: BigNumber, o: BigNumber.prototype}){
  16.     $.ROUND_HALF_EVEN = ($.ROUND_HALF_DOWN = ($.ROUND_HALF_UP = ($.ROUND_FLOOR = ($.ROUND_CEIL = ($.ROUND_DOWN = ($.ROUND_UP = 0) + 1) + 1) + 1) + 1) + 1) + 1;
  17.     $.defaultPrecision = 40;
  18.     $.defaultRoundType = $.ROUND_HALF_UP;
  19.     o.add = function(n){
  20.         if(this._s != (n = new BigNumber(n))._s)
  21.             return n._s ^= 1, this.subtract(n);
  22.         var o = new BigNumber(this), a = o._d, b = n._d, la = o._f,
  23.         lb = n._f, n = Math.max(la, lb), i, r;
  24.         la != lb && ((lb = la - lb) > 0 ? o._zeroes(b, lb, 1) : o._zeroes(a, -lb, 1));
  25.         i = (la = a.length) == (lb = b.length) ? a.length : ((lb = la - lb) > 0 ? o._zeroes(b, lb) : o._zeroes(a, -lb)).length;
  26.         for(r = 0; i; r = (a[--i] = a[i] + b[i] + r) / 10 >>> 0, a[i] %= 10);
  27.         return r && ++n && a.unshift(r), o._f = n, o.round();
  28.     };
  29.     o.subtract = function(n){
  30.         if(this._s != (n = new BigNumber(n))._s)
  31.             return n._s ^= 1, this.add(n);
  32.         var o = new BigNumber(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;
  33.         a = a._d, b = b._d, la != lb && ((lb = la - lb) > 0 ? o._zeroes(b, lb, 1) : o._zeroes(a, -lb, 1));
  34.         for(i = (la = a.length) == (lb = b.length) ? a.length : ((lb = la - lb) > 0 ? o._zeroes(b, lb) : o._zeroes(a, -lb)).length; i;){
  35.             if(a[--i] < b[i]){
  36.                 for(j = i; j && !a[--j]; a[j] = 9);
  37.                 --a[j], a[i] += 10;
  38.             }
  39.             b[i] = a[i] - b[i];
  40.         }
  41.         return c || (o._s ^= 1), o._f = d, o._d = b, o.round();
  42.     };
  43.     o.multiply = function(n){
  44.         var o = new BigNumber(this), r = o._d.length >= (n = new BigNumber(n))._d.length, a = (r ? o : n)._d,
  45.         b = (r ? n : o)._d, la = a.length, lb = b.length, x = new BigNumber, i, j, s;
  46.         for(i = lb; i; r && s.unshift(r), x.set(x.add(new BigNumber(s.join("")))))
  47.             for(s = (new Array(lb - --i)).join("0").split(""), r = 0, j = la; j; r += a[--j] * b[i], s.unshift(r % 10), r = (r / 10) >>> 0);
  48.         return o._s = o._s != n._s, o._f = ((r = la + lb - o._f - n._f) >= (j = (o._d = x._d).length) ? this._zeroes(o._d, r - j + 1, 1).length : j) - r, o.round();
  49.     };
  50.     o.divide = function(n){
  51.         if((n = new BigNumber(n)) == "0")
  52.             throw new Error("Division by 0");
  53.         else if(this == "0")
  54.             return new BigNumber;
  55.         var o = new BigNumber(this), a = o._d, b = n._d, la = a.length - o._f,
  56.         lb = b.length - n._f, r = new BigNumber, i = 0, j, s, l, f = 1, c = 0, e = 0;
  57.         r._s = o._s != n._s, r.precision = Math.max(o.precision, n.precision),
  58.         r._f = +r._d.pop(), la != lb && o._zeroes(la > lb ? b : a, Math.abs(la - lb));
  59.         n._f = b.length, b = n, b._s = false, b = b.round();
  60.         for(n = new BigNumber; a[0] == "0"; a.shift());
  61.         out:
  62.         do{
  63.             for(l = c = 0, n == "0" && (n._d = [], n._f = 0); i < a.length && n.compare(b) == -1; ++i){
  64.                 (l = i + 1 == a.length, (!f && ++c > 1 || (e = l && n == "0" && a[i] == "0")))
  65.                 && (r._f == r._d.length && ++r._f, r._d.push(0));
  66.                 (a[i] == "0" && n == "0") || (n._d.push(a[i]), ++n._f);
  67.                 if(e)
  68.                     break out;
  69.                 if((l && n.compare(b) == -1 && (r._f == r._d.length && ++r._f, 1)) || (l = 0))
  70.                     while(r._d.push(0), n._d.push(0), ++n._f, n.compare(b) == -1);
  71.             }
  72.             if(f = 0, n.compare(b) == -1 && !(l = 0))
  73.                 while(l ? r._d.push(0) : l = 1, n._d.push(0), ++n._f, n.compare(b) == -1);
  74.             for(s = new BigNumber, j = 0; n.compare(y = s.add(b)) + 1 && ++j; s.set(y));
  75.             n.set(n.subtract(s)), !l && r._f == r._d.length && ++r._f, r._d.push(j);
  76.         }
  77.         while((i < a.length || n != "0") && (r._d.length - r._f) <= r.precision);
  78.         return r.round();
  79.     };
  80.     o.mod = function(n){
  81.         return this.subtract(this.divide(n).intPart().multiply(n));
  82.     };
  83.     o.pow = function(n){
  84.         var o = new BigNumber(this), i;
  85.         if((n = (new BigNumber(n)).intPart()) == 0) return o.set(1);
  86.         for(i = Math.abs(n); --i; o.set(o.multiply(this)));
  87.         return n < 0 ? o.set((new BigNumber(1)).divide(o)) : o;
  88.     };
  89.     o.set = function(n){
  90.         return this.constructor(n), this;
  91.     };
  92.     o.compare = function(n){
  93.         var a = this, la = this._f, b = new BigNumber(n), lb = b._f, r = [-1, 1], i, l;
  94.         if(a._s != b._s)
  95.             return a._s ? -1 : 1;
  96.         if(la != lb)
  97.             return r[(la > lb) ^ a._s];
  98.         for(la = (a = a._d).length, lb = (b = b._d).length, i = -1, l = Math.min(la, lb); ++i < l;)
  99.             if(a[i] != b[i])
  100.                 return r[(a[i] > b[i]) ^ a._s];
  101.         return la != lb ? r[(la > lb) ^ a._s] : 0;
  102.     };
  103.     o.negate = function(){
  104.         var n = new BigNumber(this); return n._s ^= 1, n;
  105.     };
  106.     o.abs = function(){
  107.         var n = new BigNumber(this); return n._s = 0, n;
  108.     };
  109.     o.intPart = function(){
  110.         return new BigNumber((this._s ? "-" : "") + (this._d.slice(0, this._f).join("") || "0"));
  111.     };
  112.     o.valueOf = o.toString = function(){
  113.         var o = this;
  114.         return (o._s ? "-" : "") + (o._d.slice(0, o._f).join("") || "0") + (o._f != o._d.length ? "." + o._d.slice(o._f).join("") : "");
  115.     };
  116.     o._zeroes = function(n, l, t){
  117.         var s = ["push", "unshift"][t || 0];
  118.         for(++l; --l;  n[s](0));
  119.         return n;
  120.     };
  121.     o.round = function(){
  122.         if("_rounding" in this) return this;
  123.         var $ = BigNumber, r = this.roundType, b = this._d, d, p, n, x;
  124.         for(this._rounding = true; this._f > 1 && !b[0]; --this._f, b.shift());
  125.         for(d = this._f, p = this.precision + d, n = b[p]; b.length > d && !b[b.length -1]; b.pop());
  126.         x = (this._s ? "-" : "") + (p - d ? "0." + this._zeroes([], p - d - 1).join("") : "") + 1;
  127.         if(b.length > p){
  128.             n && (r == $.DOWN ? false : r == $.UP ? true : r == $.CEIL ? !this._s
  129.             : r == $.FLOOR ? this._s : r == $.HALF_UP ? n >= 5 : r == $.HALF_DOWN ? n > 5
  130.             : r == $.HALF_EVEN ? n >= 5 && b[p - 1] & 1 : false) && this.add(x);
  131.             b.splice(p, b.length - p);
  132.         }
  133.         return delete this._rounding, this;
  134.     };
  135. }
Advertisement
Add Comment
Please, Sign In to add comment