Ver Mensaje Individual
  #4 (permalink)  
Antiguo 11/10/2012, 08:33
Avatar de emprear
emprear
Colaborador
 
Fecha de Ingreso: junio-2007
Ubicación: me mudé
Mensajes: 8.388
Antigüedad: 16 años, 10 meses
Puntos: 1567
Respuesta: Error al restar enteros de mas de 17 caracteres

Encontré esta librería que parece más práctica de usar
Toda la documentación y otros métodos para implementar en
http://jsfromhell.com/classes/bignumber

En tu caso el ejemplo sería usando el método subtract

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2.    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>números largos</title>
  6.  
  7. <script type="text/javascript">
  8. //<![CDATA[
  9.  
  10. //+ Jonas Raoni Soares Silva
  11. //@ http://jsfromhell.com/classes/bignumber [rev. #4]
  12.  
  13. BigNumber = function(n, p, r){
  14. var o = this, i;
  15. if(n instanceof BigNumber){
  16.  for(i in {precision: 0, roundType: 0, _s: 0, _f: 0}) o[i] = n[i];
  17.  o._d = n._d.slice();
  18.  return;
  19. }
  20. o.precision = isNaN(p = Math.abs(p)) ? BigNumber.defaultPrecision : p;
  21. o.roundType = isNaN(r = Math.abs(r)) ? BigNumber.defaultRoundType : r;
  22. o._s = (n += "").charAt(0) == "-";
  23. o._f = ((n = n.replace(/[^\d.]/g, "").split(".", 2))[0] = n[0].replace(/^0+/, "") || "0").length;
  24. for(i = (n = o._d = (n.join("") || "0").split("")).length; i; n[--i] = +n[i]);
  25. o.round();
  26. };
  27. with({$: BigNumber, o: BigNumber.prototype}){
  28. $.ROUND_HALF_EVEN = ($.ROUND_HALF_DOWN = ($.ROUND_HALF_UP = ($.ROUND_FLOOR = ($.ROUND_CEIL = ($.ROUND_DOWN = ($.ROUND_UP = 0) + 1) + 1) + 1) + 1) + 1) + 1;
  29. $.defaultPrecision = 40;
  30. $.defaultRoundType = $.ROUND_HALF_UP;
  31. o.add = function(n){
  32.  if(this._s != (n = new BigNumber(n))._s)
  33.   return n._s ^= 1, this.subtract(n);
  34.  var o = new BigNumber(this), a = o._d, b = n._d, la = o._f,
  35.  lb = n._f, n = Math.max(la, lb), i, r;
  36.  la != lb && ((lb = la - lb) > 0 ? o._zeroes(b, lb, 1) : o._zeroes(a, -lb, 1));
  37.  i = (la = a.length) == (lb = b.length) ? a.length : ((lb = la - lb) > 0 ? o._zeroes(b, lb) : o._zeroes(a, -lb)).length;
  38.  for(r = 0; i; r = (a[--i] = a[i] + b[i] + r) / 10 >>> 0, a[i] %= 10);
  39.  return r && ++n && a.unshift(r), o._f = n, o.round();
  40. };
  41. o.subtract = function(n){
  42.  if(this._s != (n = new BigNumber(n))._s)
  43.   return n._s ^= 1, this.add(n);
  44.  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;
  45.  a = a._d, b = b._d, la != lb && ((lb = la - lb) > 0 ? o._zeroes(b, lb, 1) : o._zeroes(a, -lb, 1));
  46.  for(i = (la = a.length) == (lb = b.length) ? a.length : ((lb = la - lb) > 0 ? o._zeroes(b, lb) : o._zeroes(a, -lb)).length; i;){
  47.   if(a[--i] < b[i]){
  48.    for(j = i; j && !a[--j]; a[j] = 9);
  49.    --a[j], a[i] += 10;
  50.   }
  51.   b[i] = a[i] - b[i];
  52.  }
  53.  return c || (o._s ^= 1), o._f = d, o._d = b, o.round();
  54. };
  55. o.multiply = function(n){
  56.  var o = new BigNumber(this), r = o._d.length >= (n = new BigNumber(n))._d.length, a = (r ? o : n)._d,
  57.  b = (r ? n : o)._d, la = a.length, lb = b.length, x = new BigNumber, i, j, s;
  58.  for(i = lb; i; r && s.unshift(r), x.set(x.add(new BigNumber(s.join("")))))
  59.   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);
  60.  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();
  61. };
  62. o.divide = function(n){
  63.  if((n = new BigNumber(n)) == "0")
  64.   throw new Error("Division by 0");
  65.  else if(this == "0")
  66.   return new BigNumber;
  67.  var o = new BigNumber(this), a = o._d, b = n._d, la = a.length - o._f,
  68.  lb = b.length - n._f, r = new BigNumber, i = 0, j, s, l, f = 1, c = 0, e = 0;
  69.  r._s = o._s != n._s, r.precision = Math.max(o.precision, n.precision),
  70.  r._f = +r._d.pop(), la != lb && o._zeroes(la > lb ? b : a, Math.abs(la - lb));
  71.  n._f = b.length, b = n, b._s = false, b = b.round();
  72.  for(n = new BigNumber; a[0] == "0"; a.shift());
  73.  out:
  74.  do{
  75.   for(l = c = 0, n == "0" && (n._d = [], n._f = 0); i < a.length && n.compare(b) == -1; ++i){
  76.    (l = i + 1 == a.length, (!f && ++c > 1 || (e = l && n == "0" && a[i] == "0")))
  77.    && (r._f == r._d.length && ++r._f, r._d.push(0));
  78.    (a[i] == "0" && n == "0") || (n._d.push(a[i]), ++n._f);
  79.    if(e)
  80.     break out;
  81.    if((l && n.compare(b) == -1 && (r._f == r._d.length && ++r._f, 1)) || (l = 0))
  82.     while(r._d.push(0), n._d.push(0), ++n._f, n.compare(b) == -1);
  83.   }
  84.   if(f = 0, n.compare(b) == -1 && !(l = 0))
  85.    while(l ? r._d.push(0) : l = 1, n._d.push(0), ++n._f, n.compare(b) == -1);
  86.   for(s = new BigNumber, j = 0; n.compare(y = s.add(b)) + 1 && ++j; s.set(y));
  87.   n.set(n.subtract(s)), !l && r._f == r._d.length && ++r._f, r._d.push(j);
  88.  }
  89.  while((i < a.length || n != "0") && (r._d.length - r._f) <= r.precision);
  90.  return r.round();
  91. };
  92. o.mod = function(n){
  93.  return this.subtract(this.divide(n).intPart().multiply(n));
  94. };
  95. o.pow = function(n){
  96.  var o = new BigNumber(this), i;
  97.  if((n = (new BigNumber(n)).intPart()) == 0) return o.set(1);
  98.  for(i = Math.abs(n); --i; o.set(o.multiply(this)));
  99.  return n < 0 ? o.set((new BigNumber(1)).divide(o)) : o;
  100. };
  101. o.set = function(n){
  102.  return this.constructor(n), this;
  103. };
  104. o.compare = function(n){
  105.  var a = this, la = this._f, b = new BigNumber(n), lb = b._f, r = [-1, 1], i, l;
  106.  if(a._s != b._s)
  107.   return a._s ? -1 : 1;
  108.  if(la != lb)
  109.   return r[(la > lb) ^ a._s];
  110.  for(la = (a = a._d).length, lb = (b = b._d).length, i = -1, l = Math.min(la, lb); ++i < l;)
  111.   if(a[i] != b[i])
  112.    return r[(a[i] > b[i]) ^ a._s];
  113.  return la != lb ? r[(la > lb) ^ a._s] : 0;
  114. };
  115. o.negate = function(){
  116.  var n = new BigNumber(this); return n._s ^= 1, n;
  117. };
  118. o.abs = function(){
  119.  var n = new BigNumber(this); return n._s = 0, n;
  120. };
  121. o.intPart = function(){
  122.  return new BigNumber((this._s ? "-" : "") + (this._d.slice(0, this._f).join("") || "0"));
  123. };
  124. o.valueOf = o.toString = function(){
  125.  var o = this;
  126.  return (o._s ? "-" : "") + (o._d.slice(0, o._f).join("") || "0") + (o._f != o._d.length ? "." + o._d.slice(o._f).join("") : "");
  127. };
  128. o._zeroes = function(n, l, t){
  129.  var s = ["push", "unshift"][t || 0];
  130.  for(++l; --l;  n[s](0));
  131.  return n;
  132. };
  133. o.round = function(){
  134.  if("_rounding" in this) return this;
  135.  var $ = BigNumber, r = this.roundType, b = this._d, d, p, n, x;
  136.  for(this._rounding = true; this._f > 1 && !b[0]; --this._f, b.shift());
  137.  for(d = this._f, p = this.precision + d, n = b[p]; b.length > d && !b[b.length -1]; b.pop());
  138.  x = (this._s ? "-" : "") + (p - d ? "0." + this._zeroes([], p - d - 1).join("") : "") + 1;
  139.  if(b.length > p){
  140.   n && (r == $.DOWN ? false : r == $.UP ? true : r == $.CEIL ? !this._s
  141.   : r == $.FLOOR ? this._s : r == $.HALF_UP ? n >= 5 : r == $.HALF_DOWN ? n > 5
  142.   : r == $.HALF_EVEN ? n >= 5 && b[p - 1] & 1 : false) && this.add(x);
  143.   b.splice(p, b.length - p);
  144.  }
  145.  return delete this._rounding, this;
  146. };
  147.  
  148. }
  149. //]]>
  150. </head>
  151. <script type="text/javascript">
  152. //<![CDATA[
  153. var Desde=new BigNumber('185966666666645500');
  154. var Hasta =new BigNumber('185966666666645600');
  155. alert(Hasta.subtract(Desde));
  156. //]]>
  157. </body>
  158. </html>
Saludos
__________________
La voz de las antenas va, sustituyendo a Dios.
Cuando finalice la mutación, nueva edad media habrá
S.R.