calculate-asmd
Advanced tools
Comparing version 1.0.0 to 2.0.0
104
index.js
@@ -1,28 +0,23 @@ | ||
'use strict'; | ||
/** | ||
除法 | ||
"use strict"; | ||
/** | ||
加法 | ||
**/ | ||
function acctDiv(arg1, arg2){ | ||
var r1 = Number(arg1.toString().replace(".", "")); | ||
var r2 = Number(arg2.toString().replace(".", "")); | ||
var l1 = 0; | ||
var l2 = 0; | ||
function add(arg1, arg2) { | ||
var r1 = 0; | ||
var r2 = 0; | ||
var m = 0; | ||
try { | ||
l1 = Number(arg1.toString().split(".")[1].length); | ||
r1 = arg1.toString().split(".")[1].length; | ||
} catch (e) { | ||
l1 = 0; | ||
r1 = 0; | ||
} | ||
try { | ||
l2 = Number(arg2.toString().split(".")[1].length); | ||
r2 = arg2.toString().split(".")[1].length; | ||
} catch (e) { | ||
l2 = 0; | ||
r2 = 0; | ||
} | ||
var pow = Math.pow(10, Math.max(l1, l2)); | ||
//var result = (Number(r1) / Number(r2)) * Math.pow(10, l2 - l1); 这样也会出现精度问题 | ||
//先把结果放大一定倍数,再去移动相应位数 | ||
var result = r1 / r2; | ||
result = result * pow * Math.pow(10, l2 - l1) / pow; | ||
m = Math.pow(10, Math.max(r1, r2)); | ||
var result = (arg1 * m + arg2 * m) / m; | ||
return result; | ||
@@ -32,21 +27,22 @@ } | ||
/** | ||
乘法 | ||
减法 | ||
**/ | ||
function acctMul(arg1, arg2){ | ||
var r1 = Number(arg1.toString().replace(".", "")); | ||
var r2 = Number(arg2.toString().replace(".", "")); | ||
function sub(arg1, arg2) { | ||
var r1 = 0; | ||
var r2 = 0; | ||
var m = 0; | ||
try { | ||
m += arg1.toString().split(".")[1].length; | ||
r1 = arg1.toString().split(".")[1].length; | ||
} catch (e) { | ||
m += 0; | ||
r1 = 0; | ||
} | ||
try { | ||
m += arg2.toString().split(".")[1].length; | ||
r2 = arg2.toString().split(".")[1].length; | ||
} catch (e) { | ||
m += 0; | ||
r2 = 0; | ||
} | ||
var result = (r1 * r2) / Math.pow(10, m); | ||
m = Math.pow(10, Math.max(r1, r2)); | ||
var result = (arg1 * m - arg2 * m) / m; | ||
return result; | ||
@@ -56,49 +52,53 @@ } | ||
/** | ||
加法 | ||
乘法 | ||
**/ | ||
function acctAdd(arg1, arg2){ | ||
var r1 = 0; | ||
var r2 = 0; | ||
function mul(arg1, arg2) { | ||
var r1 = Number(arg1.toString().replace(".", "")); | ||
var r2 = Number(arg2.toString().replace(".", "")); | ||
var m = 0; | ||
try { | ||
r1 = arg1.toString().split(".")[1].length; | ||
m += arg1.toString().split(".")[1].length; | ||
} catch (e) { | ||
r1 = 0; | ||
m += 0; | ||
} | ||
try { | ||
r2 = arg2.toString().split(".")[1].length; | ||
m += arg2.toString().split(".")[1].length; | ||
} catch (e) { | ||
r2 = 0; | ||
m += 0; | ||
} | ||
m = Math.pow(10, Math.max(r1, r2)); | ||
var result = (arg1 * m + arg2 * m) / m; | ||
var result = (r1 * r2) / Math.pow(10, m); | ||
return result; | ||
} | ||
/** | ||
减法 | ||
/** | ||
除法 | ||
**/ | ||
function acctSub(arg1, arg2){ | ||
var r1 = 0; | ||
var r2 = 0; | ||
var m = 0; | ||
function div(arg1, arg2) { | ||
var r1 = Number(arg1.toString().replace(".", "")); | ||
var r2 = Number(arg2.toString().replace(".", "")); | ||
var l1 = 0; | ||
var l2 = 0; | ||
try { | ||
r1 = arg1.toString().split(".")[1].length; | ||
l1 = Number(arg1.toString().split(".")[1].length); | ||
} catch (e) { | ||
r1 = 0; | ||
l1 = 0; | ||
} | ||
try { | ||
r2 = arg2.toString().split(".")[1].length; | ||
l2 = Number(arg2.toString().split(".")[1].length); | ||
} catch (e) { | ||
r2 = 0; | ||
l2 = 0; | ||
} | ||
m = Math.pow(10, Math.max(r1, r2)); | ||
var result = (arg1 * m - arg2 * m) / m; | ||
var pow = Math.pow(10, Math.max(l1, l2)); | ||
//var result = (Number(r1) / Number(r2)) * Math.pow(10, l2 - l1); 这样也会出现精度问题 | ||
//先把结果放大一定倍数,再去移动相应位数 | ||
var result = r1 / r2; | ||
result = (result * pow * Math.pow(10, l2 - l1)) / pow; | ||
return result; | ||
} | ||
module.exports = {acctDiv,acctMul,acctAdd,acctSub} | ||
module.exports = { add, sub, mul, div }; |
{ | ||
"name": "calculate-asmd", | ||
"version": "1.0.0", | ||
"description": "加减乘除四则运算", | ||
"version": "2.0.0", | ||
"description": "加减乘除四则运算(calculate/arithmetic)", | ||
"main": "index.js", | ||
@@ -9,8 +9,16 @@ "author": "sanshuiwang", | ||
"scripts": { | ||
"start": "http-server -a 127.0.0.1 -p 7070", | ||
"test": "node ./test/test.js" | ||
}, | ||
"devDependencies": { | ||
"http-server": "^0.11.1" | ||
} | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/sanshuiwang/calculate-asmd.git" | ||
}, | ||
"keywords": [ | ||
"calculate", | ||
"arithmetic" | ||
], | ||
"bugs": { | ||
"url": "https://github.com/sanshuiwang/calculate-asmd/issues" | ||
}, | ||
"homepage": "https://github.com/sanshuiwang/calculate-asmd#readme" | ||
} |
# calculate-asmd | ||
javascript精确四则运算 | ||
<h3>javascript 精确四则运算</h3> | ||
> 生产原因 | ||
1. 浮点数值的最高进度是 17 位小数,但在进行运算的时候其精确度却远远不如整数;整数在进行运算的时候都会转成 10 进制。 | ||
2. Java 和 JavaScript 中计算小数运算时,都会先将十进制的小数换算到对应的二进制,一部分小数并不能完整的换算为二进制,这里就出现了第一次的误差。 | ||
3. 待小数都换算为二进制后,再进行二进制间的运算,得到二进制结果。然后再将二进制结果换算为十进制,这里通常会出现第二次的误差。 | ||
> 使用 | ||
<h4>安装</h4> | ||
`yarn add calculate-asmd --dev` | ||
or | ||
`npm install calculate-asmd --save-dev` | ||
<h4>Demo</h4> | ||
``` | ||
import { add, sub, mul, div } from 'calculate-asmd' | ||
/** 加法 */ | ||
const addNum = add(9.9, 0.2); | ||
console.log("add: ", addNum); | ||
/** 减法 */ | ||
const subNum = sub(9.9, 0.2); | ||
console.log("sub: ", subNum); | ||
/** 乘法 */ | ||
const mulNum = mul(9.9, 0.2); | ||
console.log("mul: ", mulNum); | ||
/** 除法 */ | ||
const divNum = div(9.9, 0.2); | ||
console.log("div: ", divNum); | ||
``` | ||
> 参考 | ||
[精确四则运算](https://blog.csdn.net/u010004317/article/details/79009172) | ||
[js 小数运算出现多位小数如何解决 | ||
](http://www.cnblogs.com/zdz8207/p/js-number-tofixed.html) |
@@ -1,8 +0,15 @@ | ||
var calculate = require('../index.js'); | ||
const { add, sub, mul, div } = require("../index.js"); | ||
try { | ||
var s = calculate.acctAdd(1,2) | ||
console.log(1000,s); | ||
const addNum = add(9.9, 0.2); | ||
console.log("add: ", addNum); | ||
const subNum = sub(9.9, 0.2); | ||
console.log("sub: ", subNum); | ||
const mulNum = mul(9.9, 0.2); | ||
console.log("mul: ", mulNum); | ||
const divNum = div(9.9, 0.2); | ||
console.log("div: ", divNum); | ||
} catch (error) { | ||
console.log(4000, error) | ||
throw new Error('The test does not pass...'); | ||
console.error("error: ", error); | ||
throw new Error("The test does not pass..."); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
4172
0
103
0
0
50
0