Copy codeThe Code is as follows:
<SPAN style = "FONT-SIZE: 18px"> // Note: The javascript addition result has an error, which is obvious when the 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
} </SPAN>
Copy codeThe Code is as follows:
<SPAN style = "FONT-SIZE: 18px">
// Note: the subtraction result of javascript has an error, which is obvious when two floating point numbers are added. This function returns a more precise subtraction result.
// Call: accSub (arg1, arg2)
// Return value: the exact result of arg1 minus arg2
Function accSub (arg1, arg2 ){
Return accAdd (arg1,-arg2 );
} </SPAN>
Copy codeThe Code is as follows:
<SPAN style = "FONT-SIZE: 18px">
// 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)
} </SPAN>
Copy codeThe Code is as follows:
<SPAN style = "FONT-SIZE: 18px">
// 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 );
}
} </SPAN>