nefe / number-precision Goto Github PK
View Code? Open in Web Editor NEW🚀1K tiny & fast lib for doing addition, subtraction, multiplication and division operations precisely
🚀1K tiny & fast lib for doing addition, subtraction, multiplication and division operations precisely
I'm trying to multiply 9.700000042 and 1000000000 using the times
method.
It should show 9700000042 as the final result. But it's returning 9700000041.999998.
please help.
NP.plus(2.018, 0.001)
2.0189999999999997
原因是 > 2.018 * 1000
2017.9999999999998
所以简单的乘以10的n次方不能覆盖所有情况..
RT
NP.plus(8.05463959, 9.90925221) is beyond boundary when transfer to integer, the results may not be accurate
NP.times('0.1', 100); // 10
NP.times('0.1 ', 100); // 1
第一种情况是正常的计算,第二种情况计算结果异常,原因是 '0.1 ' 中末尾包含空格。
调试发现 NP 在计算参数小数位数之前没有将其先转换为数字再计算,因此空格也算在小数位数之中并且空格在进行运算的时候不会报错,因此导致 NP 静默错误。
NP 是否需要处理参数为字符串的情况?
NP.times(9999999999999.46, 100) ==> 999999999999945.9
13位9
The result is not equal to 999999999999946
NP.plus(0.1, 0.2) > 0.3 期待可以配置默认转成 '0.30'
0.7*100 is not 7,is 7.000000000000001
describe('NP.minus', () => {
function check(num1, num2, result) {
assert.strictEqual(NP.minus(num1, num2), result);
}
it('can do minus operation', () => {
check(0.07, 0.01, 0.06);
});
});
期望
NP.plus(0.1, 0.2).times(3)// = 0.9 not 0.9000000000000001
NP.divide(33.3333, 100) = 0.33333300000000005
/**
* 精确乘法
*/
function times(num1, num2) {
// var num1Changed = Number(num1.toString().replace('.', ''));
// var num2Changed = Number(num2.toString().replace('.', ''));
// var baseNum = digitLength(num1) + digitLength(num2);
var dl1 = digitLength(num1);
var dl2 = digitLength(num2);
var num1Changed = num1 * Math.pow(10, dl1);
var num2Changed = num2 * Math.pow(10, dl2);
var baseNum = dl1 + dl2;
return num1Changed * num2Changed / Math.pow(10, baseNum);
}
/**
* 精确除法
*/
function divide(num1, num2) {
// var num1Changed = Number(num1.toString().replace('.', ''));
// var num2Changed = Number(num2.toString().replace('.', ''));
var dl1 = digitLength(num1);
var dl2 = digitLength(num2);
var num1Changed = num1 * Math.pow(10, dl1);
var num2Changed = num2 * Math.pow(10, dl2);
return times((num1Changed / num2Changed), Math.pow(10, dl2 - dl1));
}
const NP = require("number-precision");
console.log(0.151 - 0.145);// 0.006000000000000005
console.log(NP.strip(0.151-0.145)); //0.00600000000000001
divide(83.42894732749,100)
0.8342894732748999
add('-1234567890123456.00', 0)得出的结果为-1234567890123456.2
比如
roundMax(1.2344, 2) // 1.23
roundMax(1.23446, 3) // 1.234
roundMax(1.2, 3) // 1.2 NOT 1.200
打包产物中缺少 .d.ts
文件,在 ts 项目中直接引用的 src/index.ts
文件的类型定义,执行 tsc 会直接编译 node_module 中 number-precison 的 index.ts,可能会产生错误。
例如在使用 ts 4.x,且开启了 noUncheckedIndexedAccess
编译选项时会产生下面的报错:
node_modules/number-precision/src/index.ts:22:16 - error TS2532: Object is possibly 'undefined'.
22 const len = (eSplit[0].split('.')[1] || '').length - +(eSplit[1] || 0);
~~~~~~~~~
node_modules/number-precision/src/index.ts:55:23 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
55 let res = operation(num1, num2);
~~~~
node_modules/number-precision/src/index.ts:73:35 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
73 const num1Changed = float2Fixed(num1);
~~~~
node_modules/number-precision/src/index.ts:74:35 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
74 const num2Changed = float2Fixed(num2);
~~~~
node_modules/number-precision/src/index.ts:75:31 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
75 const baseNum = digitLength(num1) + digitLength(num2);
~~~~
node_modules/number-precision/src/index.ts:75:51 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
75 const baseNum = digitLength(num1) + digitLength(num2);
~~~~
node_modules/number-precision/src/index.ts:93:53 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
93 const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
~~~~
node_modules/number-precision/src/index.ts:93:72 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
93 const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
~~~~
node_modules/number-precision/src/index.ts:95:17 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
95 return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
~~~~
node_modules/number-precision/src/index.ts:95:40 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
95 return (times(num1, baseNum) + times(num2, baseNum)) / baseNum;
~~~~
node_modules/number-precision/src/index.ts:107:53 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
107 const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
~~~~
node_modules/number-precision/src/index.ts:107:72 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
107 const baseNum = Math.pow(10, Math.max(digitLength(num1), digitLength(num2)));
~~~~
node_modules/number-precision/src/index.ts:108:17 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
108 return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
~~~~
node_modules/number-precision/src/index.ts:108:40 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
108 return (times(num1, baseNum) - times(num2, baseNum)) / baseNum;
~~~~
node_modules/number-precision/src/index.ts:120:35 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
120 const num1Changed = float2Fixed(num1);
~~~~
node_modules/number-precision/src/index.ts:121:35 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
121 const num2Changed = float2Fixed(num2);
~~~~
node_modules/number-precision/src/index.ts:125:74 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
125 return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
~~~~
node_modules/number-precision/src/index.ts:125:94 - error TS2345: Argument of type 'string | number | undefined' is not assignable to parameter of type 'string | number'.
Type 'undefined' is not assignable to type 'string | number'.
125 return times(num1Changed / num2Changed, strip(Math.pow(10, digitLength(num2) - digitLength(num1))));
~~~~
Found 18 errors.
因为 .ts 文件一旦被引用总会被 tsc 编译,无法被忽略,.d.ts 类型声明文件则可以被排除,因此希望可以加上类型声明文件的生成
参考:
目前如果传入undefined/null数据,就会报错,是否可以校验传入数据的正确性
why do this use precision = 15 as default value for strip function?
With more and more projects making steps towards http 2 and using modules right in the browser (no bundling/webpack involved), an ES6 importable version (which does not rely on webpack to transpile the import) would be desirable.
NP.surplus(100, 3) // 100 % 3 ==> 1
times('100.12', '100.00') === 1,001.20 . Should be 10012.00
-3000000000000000500 is beyond boundary when transfer to integer, the results may not be accurate
能否打包时也打包打包.d.ts啊
我做了一下压力测试,感觉效率上有改进的余地:
describe('求余数运算', () => {
//Math.round 用时14ms
it('test Math', () => {
let total=0;
for (let i = 1; i <= 10000 * 10; i++) {
total += Math.round(i * 100) / 100;
}
console.log(total);
});
//Math.round 再加上求精度,转浮点数,用时 273ms
it('test Math2', () => {
let total=0;
for (let i = 1; i <= 10000 * 10; i++) {
total += Math.round(parseFloat((i * 100).toPrecision(14))) / 100;
}
console.log(total);
});
//NP.round 用时1sec963ms
it('test NP', () => {
let total=0;
for (let i = 1; i <= 10000 * 10; i++) {
total += NP.round(i ,2) ;
}
console.log(total);
});
})
NP.times(NP.times(1/3, 10), 3) // 9.999999999999998
NP.times(7/3, 3) // 7.000000000000001
这种有什么解决方案吗?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.