haw-utils
Version:
一个基于业务场景的工具方法库
55 lines (54 loc) • 1.83 kB
JavaScript
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(["exports", "../utils/math.util", "./times"], factory);
} else if (typeof exports !== "undefined") {
factory(exports, require("../utils/math.util"), require("./times"));
} else {
var mod = {
exports: {}
};
factory(mod.exports, global.utilsMathUtil, global.times);
global.plus = mod.exports;
}
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _math, _times) {
"use strict";
Object.defineProperty(_exports, "__esModule", {
value: true
});
_exports["default"] = void 0;
_times = _interopRequireDefault(_times);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
/**
* 精确加法,支持多个数相加
*
* @static
* @alias module:Math.plus
* @since 3.1.0
* @param {Number} num1 相加的第一个数
* @param {Number} num2 相加的第二个数
* @param {Number} [others] 相加的其余数
* @returns {Number} 总和
* @example
*
* plus(0.1, 0.2);
* // => 0.3
*
* plus(0.1, 0.2, 0.3);
* // => 0.6
*
* plus(0.1, 0.2, 0.3, 0.4);
* // => 1
*/
function plus(num1, num2) {
for (var _len = arguments.length, others = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
others[_key - 2] = arguments[_key];
}
if (others.length > 0) {
return plus.apply(void 0, [plus(num1, num2)].concat(others));
}
var baseNum = Math.pow(10, Math.max((0, _math.digitLength)(num1), (0, _math.digitLength)(num2)));
return ((0, _times["default"])(num1, baseNum) + (0, _times["default"])(num2, baseNum)) / baseNum;
}
var _default = plus;
_exports["default"] = _default;
});