I. js computing
What are the results of 12.32*7? Answer: 86.24000000000001
Why is this problem? How can this problem be solved?
Js has a bug in processing the multiplication and division of decimals. The solution can be to convert decimals into integers.
The above calculation can be changed:
12.32x100x7/100
The result is: 86.24, correct.
In addition, calculate the following:
8.80x100x12/100
Result: 105.60000000000002
38.80 will also have similar problems.
Precision increased by 10 times:
8.80x1000x12/1000
Result: 105.6
Normal.
16.40x1000000x6/1000000
The result is also incorrect.
In order to make javascript execution more accurate, the value will be directly increased by 10000 times in subsequent JavaScript decimal calculations, and then divided by 10000 to solve the problem.
Var num = 38.80;
Var num2 = 13;
Alert (num x 10000*12/10000 );
The number to be multiplied and divided is the most suitable for tests of 10000. If the number is smaller, there is a problem. If the number is larger (1000000), there are also problems.
II,
Copy codeThe Code is as follows:
<Script defer>
Number. prototype. rate = function (){
VaroStr = this. toString ();
If (oStr. indexOf (".") =-1)
Return1;
Else
ReturnMath. pow (10, parseInt (oStr. length-oStr.indexOf (".")-1 ));
}
Functiontran (){
Args = tran. arguments;
Vartemp = 1;
For (I = 0; I <args. length; I ++)
Temp * = args [I] * args [I]. rate ();
For (I = 0; I <args. length; I ++)
Temp/= args [I]. rate ();
Returntemp
}
Alert (tran (11, 22.9 ));
</Script>
This solution is troublesome, but it gives you a general idea about the actual process of solving this problem.
Copy codeThe Code is as follows:
// Division function, used to obtain accurate division results
// Note: the division result of javascript has an error, which is obvious when two floating point numbers are separated. This function returns a more precise division result.
// Call: accDiv (arg1, arg2)
// Return value: the exact result of dividing arg1 by 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 (r1/r2) * pow (10, t2-t1 );
}
}
// Add a div Method to the Number type to facilitate calling.
Number. prototype. div = function (arg ){
Return accDiv (this, arg );
}
// Multiplication function, used to obtain accurate multiplication results
// Note: there is an error in the javascript multiplication result, which is obvious when two floating point numbers are multiplied. This function returns a more accurate multiplication result.
// Call: accMul (arg1, arg2)
// Return value: the exact result of multiplying arg1 by arg2
Function accMul (arg1, arg2)
{
Var 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)
}
// Add a mul Method to the Number type to facilitate calling.
Number. prototype. mul = function (arg ){
Return accMul (arg, this );
}
// Addition function, used to obtain accurate addition results
// Note: The addition result of javascript has an error, which is obvious when two floating point numbers are added. This function returns a more accurate addition result.
// Call: accAdd (arg1, arg2)
// Return value: the exact result of adding arg2 to arg1
Function accAdd (arg1, arg2 ){
Var r1, r2, m;
Try {r1 = arg1.toString (). split (".") [1]. length} catch (e) {r1 = 0}
Try {r2 = arg2.toString (). split (".") [1]. length} catch (e) {r2 = 0}
M = Math. pow (10, Math. max (r1, r2 ))
Return (arg1 * m + arg2 * m)/m
}
// Add an add method to the Number type to facilitate calling.
Number. prototype. add = function (arg ){
Return accAdd (arg, this );
}