index.es.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*! *****************************************************************************
  2. Copyright (c) Microsoft Corporation.
  3. Permission to use, copy, modify, and/or distribute this software for any
  4. purpose with or without fee is hereby granted.
  5. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  6. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  7. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  8. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  9. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  10. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  11. PERFORMANCE OF THIS SOFTWARE.
  12. ***************************************************************************** */
  13. /* global Reflect, Promise */
  14. function __spreadArrays() {
  15. for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
  16. for (var r = Array(s), k = 0, i = 0; i < il; i++)
  17. for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
  18. r[k] = a[j];
  19. return r;
  20. }
  21. /**
  22. * @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
  23. * 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
  24. */
  25. /**
  26. * 把错误的数据转正
  27. * strip(0.09999999999999998)=0.1
  28. */
  29. function strip(num, precision) {
  30. if (precision === void 0) { precision = 15; }
  31. return +parseFloat(Number(num).toPrecision(precision));
  32. }
  33. /**
  34. * Return digits length of a number
  35. * @param {*number} num Input number
  36. */
  37. function digitLength(num) {
  38. // Get digit length of e
  39. var eSplit = num.toString().split(/[eE]/);
  40. var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
  41. return len > 0 ? len : 0;
  42. }
  43. /**
  44. * 把小数转成整数,支持科学计数法。如果是小数则放大成整数
  45. * @param {*number} num 输入数
  46. */
  47. function float2Fixed(num) {
  48. if (num.toString().indexOf('e') === -1) {
  49. return Number(num.toString().replace('.', ''));
  50. }
  51. var dLen = digitLength(num);
  52. return dLen > 0 ? strip(Number(num) * Math.pow(10, dLen)) : Number(num);
  53. }
  54. /**
  55. * 检测数字是否越界,如果越界给出提示
  56. * @param {*number} num 输入数
  57. */
  58. function checkBoundary(num) {
  59. if (_boundaryCheckingState) {
  60. if (num > Number.MAX_SAFE_INTEGER || num < Number.MIN_SAFE_INTEGER) {
  61. console.warn(num + " is beyond boundary when transfer to integer, the results may not be accurate");
  62. }
  63. }
  64. }
  65. /**
  66. * 精确乘法
  67. */
  68. function times(num1, num2) {
  69. var others = [];
  70. for (var _i = 2; _i < arguments.length; _i++) {
  71. others[_i - 2] = arguments[_i];
  72. }
  73. if (others.length > 0) {
  74. return times.apply(void 0, __spreadArrays([times(num1, num2), others[0]], others.slice(1)));
  75. }
  76. var num1Changed = float2Fixed(num1);
  77. var num2Changed = float2Fixed(num2);
  78. var baseNum = digitLength(num1) + digitLength(num2);
  79. var leftValue = num1Changed * num2Changed;
  80. checkBoundary(leftValue);
  81. return leftValue / Math.pow(10, baseNum);
  82. }
  83. /**
  84. * 精确加法
  85. */
  86. function plus(num1, num2) {
  87. var others = [];
  88. for (var _i = 2; _i < arguments.length; _i++) {
  89. others[_i - 2] = arguments[_i];
  90. }
  91. if (others.length > 0) {
  92. return plus.apply(void 0, __spreadArrays([plus(num1, num2), others[0]], others.slice(1)));
  93. }
  94. var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
  95. return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
  96. }
  97. /**
  98. * 精确减法
  99. */
  100. function minus(num1, num2) {
  101. var others = [];
  102. for (var _i = 2; _i < arguments.length; _i++) {
  103. others[_i - 2] = arguments[_i];
  104. }
  105. if (others.length > 0) {
  106. return minus.apply(void 0, __spreadArrays([minus(num1, num2), others[0]], others.slice(1)));
  107. }
  108. var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
  109. return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
  110. }
  111. /**
  112. * 精确除法
  113. */
  114. function divide(num1, num2) {
  115. var others = [];
  116. for (var _i = 2; _i < arguments.length; _i++) {
  117. others[_i - 2] = arguments[_i];
  118. }
  119. if (others.length > 0) {
  120. return divide.apply(void 0, __spreadArrays([divide(num1, num2), others[0]], others.slice(1)));
  121. }
  122. var num1Changed = float2Fixed(num1);
  123. var num2Changed = float2Fixed(num2);
  124. checkBoundary(num1Changed);
  125. checkBoundary(num2Changed);
  126. // fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正
  127. return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
  128. }
  129. /**
  130. * 四舍五入
  131. */
  132. function round(num, ratio) {
  133. var base = Math.pow(10, ratio);
  134. return divide(Math.round(times(num, base)), base);
  135. }
  136. var _boundaryCheckingState = true;
  137. /**
  138. * 是否进行边界检查,默认开启
  139. * @param flag 标记开关,true 为开启,false 为关闭,默认为 true
  140. */
  141. function enableBoundaryChecking(flag) {
  142. if (flag === void 0) { flag = true; }
  143. _boundaryCheckingState = flag;
  144. }
  145. var index = {
  146. strip: strip,
  147. plus: plus,
  148. minus: minus,
  149. times: times,
  150. divide: divide,
  151. round: round,
  152. digitLength: digitLength,
  153. float2Fixed: float2Fixed,
  154. enableBoundaryChecking: enableBoundaryChecking,
  155. };
  156. export { strip, plus, minus, times, divide, round, digitLength, float2Fixed, enableBoundaryChecking };
  157. export default index;