index.umd.js 5.9 KB

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