|
|
|
@ -29973,32 +29973,66 @@ function accMul (arg1, arg2) {
|
|
|
|
|
Number.prototype.mul = function (arg) { |
|
|
|
|
return accMul(arg, this); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
** 除法函数,用来得到精确的除法结果 |
|
|
|
|
** 说明:javascript的除法结果会有误差,在两个浮点数相除的时候会比较明显。这个函数返回较为精确的除法结果。 |
|
|
|
|
** 调用:accDiv(arg1,arg2) |
|
|
|
|
** 返回值:arg1除以arg2的精确结果 |
|
|
|
|
**/ |
|
|
|
|
function accDiv (arg1, arg2) { |
|
|
|
|
var t1 = 0, t2 = 0, r1, r2; |
|
|
|
|
try { |
|
|
|
|
t1 = arg1.toString().split(".")[1].length; |
|
|
|
|
} catch (e) { |
|
|
|
|
* Return digits length of a number |
|
|
|
|
* @param {*number} num Input number |
|
|
|
|
*/ |
|
|
|
|
function digitLength (num) { |
|
|
|
|
// Get digit length of e
|
|
|
|
|
var eSplit = num.toString().split(/[eE]/); |
|
|
|
|
var len = (eSplit[0].split(".")[1] || "").length - (+(eSplit[1] || 0)); |
|
|
|
|
return len > 0 ? len : 0; |
|
|
|
|
} |
|
|
|
|
/** |
|
|
|
|
* 把小数转成整数,支持科学计数法。如果是小数则放大成整数 |
|
|
|
|
* @param {*number} num 输入数 |
|
|
|
|
*/ |
|
|
|
|
function float2Fixed (num) { |
|
|
|
|
if (num.toString().indexOf("e") === -1) { |
|
|
|
|
return Number(num.toString().replace(".", "")); |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
t2 = arg2.toString().split(".")[1].length; |
|
|
|
|
} catch (e) { |
|
|
|
|
var dLen = digitLength(num); |
|
|
|
|
return dLen > 0 ? num * Math.pow(10, dLen) : num; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 精确乘法 |
|
|
|
|
*/ |
|
|
|
|
function times (num1, num2) { |
|
|
|
|
var others = []; |
|
|
|
|
for (var _i = 2; _i < arguments.length; _i++) { |
|
|
|
|
others[_i - 2] = arguments[_i]; |
|
|
|
|
} |
|
|
|
|
if (others.length > 0) { |
|
|
|
|
return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1))); |
|
|
|
|
} |
|
|
|
|
var num1Changed = float2Fixed(num1); |
|
|
|
|
var num2Changed = float2Fixed(num2); |
|
|
|
|
var baseNum = digitLength(num1) + digitLength(num2); |
|
|
|
|
var leftValue = num1Changed * num2Changed; |
|
|
|
|
return leftValue / Math.pow(10, baseNum); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 精确除法 |
|
|
|
|
*/ |
|
|
|
|
function accDivide (num1, num2) { |
|
|
|
|
var others = []; |
|
|
|
|
for (var _i = 2; _i < arguments.length; _i++) { |
|
|
|
|
others[_i - 2] = arguments[_i]; |
|
|
|
|
} |
|
|
|
|
with (Math) { |
|
|
|
|
r1 = Number(arg1.toString().replace(".", "")); |
|
|
|
|
r2 = Number(arg2.toString().replace(".", "")); |
|
|
|
|
return (t2 > t1) ? (r1 / r2) * pow(10, t2 - t1) : (r1 / r2) / pow(10, t1 - t2); |
|
|
|
|
if (others.length > 0) { |
|
|
|
|
return accDivide.apply(void 0, [accDivide(num1, num2), others[0]].concat(others.slice(1))); |
|
|
|
|
} |
|
|
|
|
var num1Changed = float2Fixed(num1); |
|
|
|
|
var num2Changed = float2Fixed(num2); |
|
|
|
|
return times((num1Changed / num2Changed), Math.pow(10, digitLength(num2) - digitLength(num1))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 给Number类型增加一个div方法,调用起来更加方便。
|
|
|
|
|
Number.prototype.div = function (arg) { |
|
|
|
|
return accDiv(this, arg); |
|
|
|
|
return accDivide(this, arg); |
|
|
|
|
};/** |
|
|
|
|
* 对字符串对象的扩展 |
|
|
|
|
* @class String |
|
|
|
|