人民医院前端

int10.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Big integer base-10 printing library
  2. // Copyright (c) 2014 Lapo Luchini <lapo@lapo.it>
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. // ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. // ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. /*jshint browser: true, strict: true, immed: true, latedef: true, undef: true, regexdash: false */
  15. var max = 10000000000000; // biggest integer that can still fit 2^53 when multiplied by 256
  16. var Int10 = /** @class */ (function () {
  17. function Int10(value) {
  18. this.buf = [+value || 0];
  19. }
  20. Int10.prototype.mulAdd = function (m, c) {
  21. // assert(m <= 256)
  22. var b = this.buf;
  23. var l = b.length;
  24. var i;
  25. var t;
  26. for (i = 0; i < l; ++i) {
  27. t = b[i] * m + c;
  28. if (t < max) {
  29. c = 0;
  30. }
  31. else {
  32. c = 0 | (t / max);
  33. t -= c * max;
  34. }
  35. b[i] = t;
  36. }
  37. if (c > 0) {
  38. b[i] = c;
  39. }
  40. };
  41. Int10.prototype.sub = function (c) {
  42. // assert(m <= 256)
  43. var b = this.buf;
  44. var l = b.length;
  45. var i;
  46. var t;
  47. for (i = 0; i < l; ++i) {
  48. t = b[i] - c;
  49. if (t < 0) {
  50. t += max;
  51. c = 1;
  52. }
  53. else {
  54. c = 0;
  55. }
  56. b[i] = t;
  57. }
  58. while (b[b.length - 1] === 0) {
  59. b.pop();
  60. }
  61. };
  62. Int10.prototype.toString = function (base) {
  63. if ((base || 10) != 10) {
  64. throw new Error("only base 10 is supported");
  65. }
  66. var b = this.buf;
  67. var s = b[b.length - 1].toString();
  68. for (var i = b.length - 2; i >= 0; --i) {
  69. s += (max + b[i]).toString().substring(1);
  70. }
  71. return s;
  72. };
  73. Int10.prototype.valueOf = function () {
  74. var b = this.buf;
  75. var v = 0;
  76. for (var i = b.length - 1; i >= 0; --i) {
  77. v = v * max + b[i];
  78. }
  79. return v;
  80. };
  81. Int10.prototype.simplify = function () {
  82. var b = this.buf;
  83. return (b.length == 1) ? b[0] : this;
  84. };
  85. return Int10;
  86. }());
  87. export { Int10 };