Js BigDecimal method of realizing precise subtraction operation

Source: Internet
Author: User

An addition function that is used to obtain accurate addition 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;
}

/** * * 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 The exact result of subtracting arg2 **/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);} /** * * 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);} /** * * Division LetterNumber, 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 (T2-T1); }}

Js BigDecimal How to achieve accurate subtraction operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.