I used to find this code on the Internet before, but under specific conditions, the division and additional operation will still appear.
Copy code code as follows:
// Except the method to get the precise method of removing the method
// Description: The results of JavaScript will have an error, and it will be obvious when the two floating -point numbers are removed. This function returns a more accurate removal result.
// Call: ACCDIV (ARG1, ARG2)
// Return value: ARG1 Except the accurate results of ARG2
Function accdiv (ARG1, ARG2) {{
var T1 = 0, T2 = 0, R1, R2;
try {t1 = arg1.tostring (). split (".") [1] .Length} Catch (e) {}
try {t2 = arg2.tostring (). split (".") [1] .Length} catch (e) {}
with (math) {
R1 = Number (arg1.tostring (). Replace (".", ""))
r2 = number (arg2.tostring (). Replace (".", ""))
Return accmul ((R1 / R2), POW (10, T2 -T1));
}
}
Copy code code as follows:
// multiplication function to obtain accurate multiplication results
// Description: The results of the JavaScript multiplication results will have an error, and it will be obvious when the two floating -point numbers are multiplied. This function returns a more accurate multiplication result.
// Call: ACCMul (ARG1, ARG2)
// Return value: ARG1 is multiplied by ARG2's accurate results
Function accmul (ARG1, ARG2) {{
varm m = 0, s1 = arg1.tostring (), s2 = arg2.tostring ();
TRY {m += s1.split (".") [1] .Length} Catch (e) {}
TRY {m += s2.split (".") [1] .Length} catch (e) {}
Return number (s1.replace (".", "") * number (s2.replace (".", "") / math.pow (10, m)
}
Copy code code as follows:
// Add method to calculate intermediate solutions
Function acadd (ARG1, ARG2) {{
var r1, r2, m, c;
try {r1 = arg1.tostring (). split (".") [1] .Length} catch (e) {R1 = 0}
try {r2 = arg2.tostring (). split (".") [1] .Length} catch (e) {R2 = 0}
c = math.abs (R1 -R2);
m = math.pow (10, math.max (R1, R2)))
if (c> 0) {{
var cm = math.pow (10, c);
if (R1> R2) {
arg1 = number (arg1.tostring (). Replace ("". "," ");
arg2 = number (arg2.tostring (). Replace (".", "") * cm;
}
else {
arg1 = number (arg1.tostring (). Replace (".", "") * cm;
arg2 = number (arg2.tostring (). Replace ("". "," ");
}
}
else {
arg1 = number (arg1.tostring (). Replace ("". "," ");
arg2 = number (arg2.tostring (). Replace ("". "," ");
}
Return accdiv ((ARG1 + ARG2), M); M);
}