Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. // A 0+ integer of unlimited size
  2. function Int(p) { // Takes a number like 5, a string of numerals like "789", a bignumber.js BigNumber, or another Int
  3. if (isType(p, "Int")) return p; // Return the given Int instead of making a new one, the value inside an Int can't change
  4. var o = {};
  5. o.v = _3type(p); // Parse the given parameter, keeping together v.s() numerals, and v.n() number and v.b() BigNumber once we have them or they are necessary
  6. o.inside = o.v.inside; // Point to function, see which types v has built up with text like "bns" or "--s" for testing
  7.  
  8. o.add = function(q) { return _add(o.v, q); } // Math
  9. o.subtract = function(q) { return _sub(o.v, q); }
  10. o.multiply = function(q) { return _mul(o.v, q); }
  11. o.divide = function(q) { return _div(o.v, q); }
  12. o.modulo = function(q) { return _mod(o.v, q); }
  13.  
  14. o.equals = function(q) { return _equ(o.v, q); }
  15. o.greaterThan = function(q) { return _gth(o.v, q); }
  16. o.greaterThanOrEqualTo = function(q) { return _gte(o.v, q); }
  17. o.lessThan = function(q) { return _lth(o.v, q); }
  18. o.lessThanOrEqualTo = function(q) { return _lte(o.v, q); }
  19.  
  20. o.increment = function() { return _inc(o.v); }
  21. o.decrement = function() { return _dec(o.v); }
  22. o.is = function() { return _isp(o.v); }
  23.  
  24. o._ = function(c, q) { return _calculate(o.v, c, q); }
  25. o.text = o.v.s();
  26. o.hasNumber = function() { return o.v.fit; } // True if our value is small enough to fit in a number as an integer, not a floating point number
  27. o.toNumber = o.v.n; // Point to function, throws if too big
  28. o.type = "Int";
  29. return freeze(o);
  30. }
  31. function _3type(p) { // Parse the parameter given to Int or a method on Int, keeping the same integer value in up to 3 different types
  32. var type = getType(p);
  33. if (type == "Int") return p.v; // We got an Int, return the value inside instead of making a new one
  34.  
  35. // Hold the same integer value 1, 2 or 3 different ways, keeping the type we were given, only converting when necessary, and checking everything we can with the types we have
  36. var b = "none"; // Our integer value in a BigNumber object, or "none" before we have one
  37. var n = "none"; // Our integer value in a number type variable, or "none" before we have one, or if our value won't fit
  38. var s = "none"; // Our integer value as a string of numerals, we always have this type
  39.  
  40. if (hasMethod(p, "dividedToIntegerBy")) { b = p; s = b.toFixed(0); checkNumerals(s); } // Given a BigNumber, make and check numerals
  41. else if (type == "number") { n = p; checkNumberMath(n); s = n+""; checkNumerals(s); checkNumeralsFit(s); } // Given a number, check it, make numerals, and check them
  42. else if (type == "string") { s = p; checkNumerals(s); } // Given numerals, check them
  43. else { toss("type"); } // Int(p).method(p) only accepts p as an Int, BigNumber, number, or string
  44.  
  45. var o = {}; // Return, or make, check, keep, and return, our value in a BigNumber, number, or string ♫ There's three ways of saying, the very same thing
  46. o.b = function() { if (b !== "none") return b; b = new platformBigNumber(s); checkSame(s, b.toFixed(0)); return b; } // Make from numerals rather than number to avoid 15 digit limit
  47. o.n = function() { if (n !== "none") return n; n = numeralsToNumber(s); return n; }
  48. o.s = function() { return s; }
  49.  
  50. o.fit = numeralsFit(s); // Small enough to fit
  51. o.bs = function() { return b !== "none" ? b : s; } // Our value in a BigNumber if we have one, numerals otherwise
  52. o.inside = function() { return "###".fill(b === "none" ? "-" : "b", n === "none" ? "-" : "n", s === "none" ? "-" : "s"); } // Show which types we've built up
  53. return o;
  54. }
  55. // Small values use number for speed Potentially large values use BigNumber instead
  56. function _add(v, q) { var w = _3type(q); return _bothFitProduct(v, w) ? Int(v.n() + w.n()) : Int(v.b().plus( w.bs())); }
  57. function _sub(v, q) { var w = _3type(q); _checkSubtract(v, w); return _bothFit(v, w) ? Int(v.n() - w.n()) : Int(v.b().minus( w.bs())); }
  58. function _mul(v, q) { var w = _3type(q); return _bothFitProduct(v, w) ? Int(v.n() * w.n()) : Int(v.b().times( w.bs())); }
  59. function _div(v, q) { var w = _3type(q); _checkDivide(v, w); return _bothFit(v, w) ? Int(Math.floor(v.n() / w.n())) : Int(v.b().dividedToIntegerBy(w.bs())); }
  60. function _mod(v, q) { var w = _3type(q); _checkDivide(v, w); return _bothFit(v, w) ? Int(v.n() % w.n()) : Int(v.b().mod( w.bs())); }
  61.  
  62. function _equ(v, q) { var w = _3type(q); return _bothFit(v, w) ? v.n() == w.n() : v.b().equals( w.bs()); }
  63. function _gth(v, q) { var w = _3type(q); return _bothFit(v, w) ? v.n() > w.n() : v.b().greaterThan( w.bs()); }
  64. function _gte(v, q) { var w = _3type(q); return _bothFit(v, w) ? v.n() >= w.n() : v.b().greaterThanOrEqualTo( w.bs()); }
  65. function _lth(v, q) { var w = _3type(q); return _bothFit(v, w) ? v.n() < w.n() : v.b().lessThan( w.bs()); }
  66. function _lte(v, q) { var w = _3type(q); return _bothFit(v, w) ? v.n() <= w.n() : v.b().lessThanOrEqualTo( w.bs()); }
  67.  
  68. function _inc(v) { return _add(v, 1); }
  69. function _dec(v) { return _sub(v, 1); }
  70. function _isp(v) { return _gth(v, 0); }
  71.  
  72. function _checkSubtract(v, w) { if (compareCheckedNumerals(v.s(), w.s()) < 0) toss("bounds"); } // Make sure v - w will be 0+, as negative values aren't allowed
  73. function _checkDivide(v, w) { if (w.s() == "0") toss("math"); } // Who says you can't divide by zero? OH SHI-
  74. function _bothFit(v, w) { return v.fit && w.fit; } // True if both values will fit in numbers, so we can use minus, divide, and modulo
  75. function _bothFitProduct(v, w) { // True if adding or multipling the given two numbers can't produce an answer that's too big
  76. return _bothFit(v, w) && v.s().length + w.s().length < (Number.MAX_SAFE_INTEGER+"").length; // Even if v and w are all 9s, a*b will still be a digit shorter than max safe integer
  77. }
  78. function _calculate(v, c, q) { // Who says JavaScript can't do operator overloading?
  79. if (c == "+") { return _add(v, q); }
  80. else if (c == "-") { return _sub(v, q); }
  81. else if (c == "*") { return _mul(v, q); }
  82. else if (c == "/") { return _div(v, q); }
  83. else if (c == "%") { return _mod(v, q); }
  84.  
  85. else if (c == "==") { return _equ(v, q); }
  86. else if (c == ">") { return _gth(v, q); }
  87. else if (c == ">=") { return _gte(v, q); }
  88. else if (c == "<") { return _lth(v, q); }
  89. else if (c == "<=") { return _lte(v, q); }
  90.  
  91. else if (c == "++") { return _inc(v); }
  92. else if (c == "--") { return _dec(v); }
  93. else if (c == ">0") { return _isp(v); }
  94. else { toss("code"); }
  95. }
  96.  
  97. exports.Int = Int;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement