Because the software that develops now is retail, food and beverage project of this respect is in the majority, natural meeting calculates to amount this request will be higher. Js on the accuracy of the calculation has been defective, so we recommend a class library -math.js
JS addition, subtraction, multiplication and division
1, addition arithmetic: 0.1+0.2
console.log(0.1+0.2); The result is:0.30000000000000004
Copy the code
2. Subtraction arithmetic: 0.8-0.7
console.log(0.8-0.7) The result:0.10000000000000009
Copy the code
3, multiplication arithmetic: 5.10*100
console.log(5.10*100) The result:509.99999999999994
Copy the code
4, division method arithmetic 6.10/0.1
console.log(6.10/0.1) The result:60.99999999999999
Copy the code
JavaScript floating-point arithmetic results are incorrect because of floating-point storage problems.
Math.js
Math.js is an extended Math library for JavaScript and Node.js. It has a flexible expression parser that supports symbolic computation, a large number of built-in functions and constants, and provides an integrated solution for handling different data types such as numbers, large numbers, complex numbers, fractions, units and matrices, powerful and easy to use.
- Code base: github.com/josdejong/m…
- Documents:Mathjs.org/docs/index….
Characteristics of the
- Supports numbers, large numbers, complex numbers, fractions, units, strings, arrays, and matrices.
- Compatible with JavaScript’s built-in Math library, Math usage, same low barrier
- Includes a flexible expression parser.
- Do the symbolic calculation.
- Lots of built-in functions and constants.
- Can also be used as a command line application.
- Run on any JavaScript engine.
- It’s easy to expand.
- Open source.
Help to use tutorials:
1. Traditional use, introducing math.js
<! DOCTYPE HTML><html>
<head>
<script src="https://unpkg.com/[email protected]/dist/math.min.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
const ans = math.add(0.1.0.2) // 0.30000000000000004
console.log(math.format(ans, {precision: 14})) / / '0.3'
console.log(math.sqrt(4).toString()) / / 2
</script>
</body>
</html>
Copy the code
2, es module
NPM install
npm install mathjs
Copy the code
use
import { create, all } from 'mathjs'
const config = {
number: 'BigNumber'.precision: 20
}
const math = create(all, config);
export default {
methods: {
/ / prescribing
numberSqrt: function(arg1){
return math math.sqrt(arg1)
},
/ / in addition to
numberExcept: function (arg1, arg2) {
return math.divide(arg1, arg2);
},
/ / by
numberRide: function (arg1, arg2) {
return math.multiply(arg1, arg2);
},
/ / add
numberAdd:function (arg1,arg2) {
return math.add(arg1, arg2);
}
/ /
numberSub:function (arg1,arg2) {
returnmath.add(arg1, -arg2); }}}Copy the code
Math.js common math functions
<script type="text/javascript">
// expressions
console.log(math.evaluate('1.2 * (2 + 4.5)')) / / 7.8
console.log(math.evaluate('12.7 cm to inch')) // 5 inch
console.log(math.evaluate('sin(45 deg) ^ 2')) / / 0.5
console.log(math.evaluate('9 / 3 + 2i')) // 3 + 2i
console.log(math.evaluate('det([-1, 2; 3, 1])')) / / - 7
// mixed use of different data types in functions
console.log(math.add(5[9.6])) // number + Array, [14, 11]
console.log(math.multiply(math.unit('5 mm'), 3)) // Unit * number, 15 mm
console.log(math.subtract([2.3.4].10)) // Array - number, [-8, -7, -6]
console.log(math.add(math.matrix([4.1]), [2.9])) // Matrix + Array, [6, 10]
// chain operator
console.log(math.chain(3)
.add(4)
.subtract(2)
.done()) / / 5
console.log(math.chain( [[1.2], [3.4]] )
.subset(math.index(0.0), 8)
.multiply(3)
.done()) // [[24, 6], [9, 12]
</script>
Copy the code