/** * * Addition function, used to get 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 Precision results **/function accadd (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 (Ten, Math.max (R1, r2)); if (C > 0) {var cm = Math.pow (ten, 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) {return Accadd (ARG, this);};
/** * * 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 Precision results **/function accsub (arg1, arg2) { var r1, 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 length n = (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) { return Accmul (ARG, this);};
/** * * 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 **/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 (m);} Adding a Mul method to the number type makes it more convenient to call. Number.prototype.mul = function (arg) { return Accmul (ARG, this);};
/** * * 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 **/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 (ten, t2-t1);} } Adding a Div method to the number type makes it easier to call. Number.prototype.div = function (arg) { return accdiv (this, arg);};
Solve the JS floating point (decimal) calculation subtraction bug