visit http://qinshenxue.com/article.aspx?id=1 View latest Articles
Description
As we all know, JS in the calculation of floating-point numbers, the results may not be accurate. For example: (the result of the operation in Chrome)
2.2 + 2.1 = 4.300000000000001
2.2-1.9 = 0.30000000000000027
2.2 * 2.2 = 4.840000000000001
2.1/0.3 = 7.000000000000001 Code circulated online (with Bugs)
Online after the optimized code is as follows (the problematic code, do not use)
function Add (A, b) {var c, D, E; try {c = a.tostring (). Split (".")
[1].length;
catch (f) {c = 0; try {d = b.tostring (). Split (".")
[1].length;
catch (f) {d = 0;
return e = Math.pow (Math.max (c, D)), (A * e + b * e)/E;
function Sub (A, b) {var c, D, E; try {c = a.tostring (). Split (".")
[1].length;
catch (f) {c = 0; try {d = b.tostring (). Split (".")
[1].length;
catch (f) {d = 0;
return e = Math.pow (Math.max (c, D)), (A * e-b * e)/E;
function Mul (A, b) {var c = 0, d = a.tostring (), E = b.tostring (); try {c + = D.split (".")
[1].length; catch (f) {} try {c = e.split (".")
[1].length;
catch (f) {} return Number (D.replace (".", "")) * Number (E.replace (".", ""))/Math.pow (c);}
function Div (A, b) {var c, d, E = 0, f = 0; try {e = a.tostring (). sPlit (".")
[1].length; catch (g) {} try {f = b.tostring (). Split (".")
[1].length; catch (g) {} return c = Number (a.tostring (). Replace (".", "")), d = number (b.tostring (). Replace (".", "")), C/D * Ma
Th.pow (F-E); }
The principle is to convert floating-point numbers to integers to compute. Problem Code Test
But the above optimization method really solves the problem, we can simply do the test.
The test code is as follows: (addition in the test operation)
function test () {
var a = (Math.random () *). ToFixed (2)-0;
var B = (Math.random () * 1000). toFixed (2)-0;
var result = Add (a, b);
if (Result + '). Length > Ten {
console.error (' summand: ' +a, ' addends: ' +b, ' results: ' +result);
return;
}
settimeout (function () {
test ();
}, ten);
Test ();
problem code Test results
The browser console quickly prints the results, indicating that the addition operation code being tested has problems with inaccurate operation.
Test Run Results:
Problem code Error reason
Since there is a problem with the code above, the error point is where we debug the code with the wrong number.
by addends: 19.36 addends: 601.19 results: 620.5500000000001
The process and results of the commissioning are as follows:
We found that the multiplication calculation error was the original one. Correction Method
There are some versions on the Web where you'll find that when you finally return the results, you add tofixed, which is a solution.
Also, since it's a multiplication error, why don't we replace the multiplication with the optimized multiplication. For example, the modified addition is as follows:
function Add (A, b) {
var c, D, E;
try {
c = a.tostring (). Split (".") [1].length;
} catch (f) {
c = 0;
}
try {
d = b.tostring (). Split (".") [1].length;
} catch (f) {
d = 0;
}
return e = Math.pow (Math.max (c, D)), (Mul (A, E) + mul (b, E))/E;
}
Then use the above method to test, wait for "one day", the console also did not print. It means that this is really solving the problem. final version (correct version)
function Add (A, b) {var c, D, E; try {c = a.tostring (). Split (".")
[1].length;
catch (f) {c = 0; try {d = b.tostring (). Split (".")
[1].length;
catch (f) {d = 0;
return e = Math.pow (Math.max (c, D)), (Mul (A, E) + mul (b, E))/E;
function Sub (A, b) {var c, D, E; try {c = a.tostring (). Split (".")
[1].length;
catch (f) {c = 0; try {d = b.tostring (). Split (".")
[1].length;
catch (f) {d = 0;
return e = Math.pow (Math.max (c, D)), (Mul (A, E)-Mul (b, E))/E;
function Mul (A, b) {var c = 0, d = a.tostring (), E = b.tostring (); try {c + = D.split (".")
[1].length; catch (f) {} try {c = e.split (".")
[1].length;
catch (f) {} return Number (D.replace (".", "")) * Number (E.replace (".", ""))/Math.pow (c);}
function Div (A, b) {var c, d, E = 0, f = 0; try {E= A.tostring (). Split (".")
[1].length; catch (g) {} try {f = b.tostring (). Split (".")
[1].length; catch (g) {} return c = Number (a.tostring (). Replace (".", "")), d = number (b.tostring (). Replace (".", "")), Mul (C/D,
Math.pow (F-E)); }