addition function
/** * * * addition function for accurate addition results * * Description: JavaScript addition results will be error, when the two floating-point number added will be more obvious. This function returns a more accurate addition result. * * Call: Accadd (ARG1,ARG2) * * return value: Arg1 plus arg2 exact result **/functionAccadd (arg1, arg2) {varR1, 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) { varCM = 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(Arg1 + arg2)/m;} //Adding an Add method to the number type makes it more convenient to call. Number.prototype.add =function(ARG) {returnAccadd (ARG, This);};
Subtraction function
/** * * subtraction function, used to get accurate subtraction results * * Description: JavaScript subtraction results will be error, when the two floating-point subtraction will be more obvious. This function returns a more accurate subtraction result. * * Call: Accsub (ARG1,ARG2) * * return value: Arg1 plus arg2 exact result **/functionaccsub (arg1, arg2) {varR1, R2, M, N; 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 (Ten, Math.max (R1, r2));//Last Modify by Deeka//dynamic control accuracy lengthn = (r1 >= r2)?R1:R2; return((ARG1 * M-ARG2 * m)/m). toFixed (n);} //Adding a Mul method to the number type makes it more convenient to call. Number.prototype.sub =function(ARG) {returnAccmul (ARG, This);};
multiplication function
/** * * * multiplication function, used to get accurate multiplication results * * Description: JavaScript multiplication results will be error, when the two floating point multiplied by the more obvious. This function returns a more accurate multiplication result. * * Call: Accmul (ARG1,ARG2) * * return value: Arg1 times the exact result of Arg2 **/functionAccmul (arg1, arg2) {varm = 0, S1 = arg1.tostring (), S2 =arg2.tostring (); Try{m+ = S1.split (".") [1].length; } Catch(e) {}Try{m+ = S2.split (".") [1].length; } Catch(e) {}returnNumber (S1.replace (".", "")) * Number (S2.replace (".", ""))/Math.pow (10, m);} //Adding a Mul method to the number type makes it more convenient to call. Number.prototype.mul =function(ARG) {returnAccmul (ARG, This);};
Division function
/** * * * Division function, used to get accurate division results * * Description: JavaScript division results will be error, when the two floating point number is more obvious when dividing. This function returns a more accurate division result. * * Call: Accdiv (ARG1,ARG2) * * return value: Arg1 divided by arg2 exact result **/functionAccdiv (arg1, arg2) {varT1 = 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 (T2-t1); }} //Adding a Div method to the number type makes it easier to call. Number.prototype.div =function(ARG) {returnAccdiv ( This, Arg);};
This bug ...
Solve the JS floating point (decimal) calculation subtraction bug