Skip to content

Commit

Permalink
chore: bump 1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
camsong committed Jun 24, 2020
1 parent 843d343 commit 44f82b8
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 43 deletions.
80 changes: 70 additions & 10 deletions build/index.es.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,53 @@
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise */



























function __spreadArrays() {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
}

/**
* @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
* 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
Expand All @@ -7,8 +57,8 @@
* strip(0.09999999999999998)=0.1
*/
function strip(num, precision) {
if (precision === void 0) { precision = 12; }
return +parseFloat(num.toPrecision(precision));
if (precision === void 0) { precision = 15; }
return +parseFloat(Number(num).toPrecision(precision));
}
/**
* Return digits length of a number
Expand All @@ -17,7 +67,7 @@ function strip(num, precision) {
function digitLength(num) {
// Get digit length of e
var eSplit = num.toString().split(/[eE]/);
var len = (eSplit[0].split('.')[1] || '').length - (+(eSplit[1] || 0));
var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
return len > 0 ? len : 0;
}
/**
Expand All @@ -29,7 +79,7 @@ function float2Fixed(num) {
return Number(num.toString().replace('.', ''));
}
var dLen = digitLength(num);
return dLen > 0 ? strip(num * Math.pow(10, dLen)) : num;
return dLen > 0 ? strip(Number(num) * Math.pow(10, dLen)) : Number(num);
}
/**
* 检测数字是否越界,如果越界给出提示
Expand All @@ -51,7 +101,7 @@ function times(num1, num2) {
others[_i - 2] = arguments[_i];
}
if (others.length > 0) {
return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1)));
return times.apply(void 0, __spreadArrays([times(num1, num2), others[0]], others.slice(1)));
}
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
Expand All @@ -69,7 +119,7 @@ function plus(num1, num2) {
others[_i - 2] = arguments[_i];
}
if (others.length > 0) {
return plus.apply(void 0, [plus(num1, num2), others[0]].concat(others.slice(1)));
return plus.apply(void 0, __spreadArrays([plus(num1, num2), others[0]], others.slice(1)));
}
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
Expand All @@ -83,7 +133,7 @@ function minus(num1, num2) {
others[_i - 2] = arguments[_i];
}
if (others.length > 0) {
return minus.apply(void 0, [minus(num1, num2), others[0]].concat(others.slice(1)));
return minus.apply(void 0, __spreadArrays([minus(num1, num2), others[0]], others.slice(1)));
}
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
Expand All @@ -97,14 +147,14 @@ function divide(num1, num2) {
others[_i - 2] = arguments[_i];
}
if (others.length > 0) {
return divide.apply(void 0, [divide(num1, num2), others[0]].concat(others.slice(1)));
return divide.apply(void 0, __spreadArrays([divide(num1, num2), others[0]], others.slice(1)));
}
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
checkBoundary(num1Changed);
checkBoundary(num2Changed);
// fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正
return times((num1Changed / num2Changed), strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
}
/**
* 四舍五入
Expand All @@ -122,7 +172,17 @@ function enableBoundaryChecking(flag) {
if (flag === void 0) { flag = true; }
_boundaryCheckingState = flag;
}
var index = { strip: strip, plus: plus, minus: minus, times: times, divide: divide, round: round, digitLength: digitLength, float2Fixed: float2Fixed, enableBoundaryChecking: enableBoundaryChecking };
var index = {
strip: strip,
plus: plus,
minus: minus,
times: times,
divide: divide,
round: round,
digitLength: digitLength,
float2Fixed: float2Fixed,
enableBoundaryChecking: enableBoundaryChecking,
};

export { strip, plus, minus, times, divide, round, digitLength, float2Fixed, enableBoundaryChecking };
export default index;
80 changes: 70 additions & 10 deletions build/index.iife.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,56 @@
var NP = (function (exports) {
'use strict';

/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise */



























function __spreadArrays() {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
}

/**
* @desc 解决浮动运算问题,避免小数点后产生多位数和计算精度损失。
* 问题示例:2.3 + 2.4 = 4.699999999999999,1.0 - 0.9 = 0.09999999999999998
Expand All @@ -10,8 +60,8 @@ var NP = (function (exports) {
* strip(0.09999999999999998)=0.1
*/
function strip(num, precision) {
if (precision === void 0) { precision = 12; }
return +parseFloat(num.toPrecision(precision));
if (precision === void 0) { precision = 15; }
return +parseFloat(Number(num).toPrecision(precision));
}
/**
* Return digits length of a number
Expand All @@ -20,7 +70,7 @@ function strip(num, precision) {
function digitLength(num) {
// Get digit length of e
var eSplit = num.toString().split(/[eE]/);
var len = (eSplit[0].split('.')[1] || '').length - (+(eSplit[1] || 0));
var len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
return len > 0 ? len : 0;
}
/**
Expand All @@ -32,7 +82,7 @@ function float2Fixed(num) {
return Number(num.toString().replace('.', ''));
}
var dLen = digitLength(num);
return dLen > 0 ? strip(num * Math.pow(10, dLen)) : num;
return dLen > 0 ? strip(Number(num) * Math.pow(10, dLen)) : Number(num);
}
/**
* 检测数字是否越界,如果越界给出提示
Expand All @@ -54,7 +104,7 @@ function times(num1, num2) {
others[_i - 2] = arguments[_i];
}
if (others.length > 0) {
return times.apply(void 0, [times(num1, num2), others[0]].concat(others.slice(1)));
return times.apply(void 0, __spreadArrays([times(num1, num2), others[0]], others.slice(1)));
}
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
Expand All @@ -72,7 +122,7 @@ function plus(num1, num2) {
others[_i - 2] = arguments[_i];
}
if (others.length > 0) {
return plus.apply(void 0, [plus(num1, num2), others[0]].concat(others.slice(1)));
return plus.apply(void 0, __spreadArrays([plus(num1, num2), others[0]], others.slice(1)));
}
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
Expand All @@ -86,7 +136,7 @@ function minus(num1, num2) {
others[_i - 2] = arguments[_i];
}
if (others.length > 0) {
return minus.apply(void 0, [minus(num1, num2), others[0]].concat(others.slice(1)));
return minus.apply(void 0, __spreadArrays([minus(num1, num2), others[0]], others.slice(1)));
}
var baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
Expand All @@ -100,14 +150,14 @@ function divide(num1, num2) {
others[_i - 2] = arguments[_i];
}
if (others.length > 0) {
return divide.apply(void 0, [divide(num1, num2), others[0]].concat(others.slice(1)));
return divide.apply(void 0, __spreadArrays([divide(num1, num2), others[0]], others.slice(1)));
}
var num1Changed = float2Fixed(num1);
var num2Changed = float2Fixed(num2);
checkBoundary(num1Changed);
checkBoundary(num2Changed);
// fix: 类似 10 ** -4 为 0.00009999999999999999,strip 修正
return times((num1Changed / num2Changed), strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
}
/**
* 四舍五入
Expand All @@ -125,7 +175,17 @@ function enableBoundaryChecking(flag) {
if (flag === void 0) { flag = true; }
_boundaryCheckingState = flag;
}
var index = { strip: strip, plus: plus, minus: minus, times: times, divide: divide, round: round, digitLength: digitLength, float2Fixed: float2Fixed, enableBoundaryChecking: enableBoundaryChecking };
var index = {
strip: strip,
plus: plus,
minus: minus,
times: times,
divide: divide,
round: round,
digitLength: digitLength,
float2Fixed: float2Fixed,
enableBoundaryChecking: enableBoundaryChecking,
};

exports.strip = strip;
exports.plus = plus;
Expand Down
Loading

0 comments on commit 44f82b8

Please sign in to comment.