Solve the JS floating point (decimal) calculation subtraction bug

Source: Internet
Author: User
Tags mul

The/** **  addition function, which is used to obtain the exact addition  **  explanation: the addition result of JavaScript will be error, which will be obvious when the two floating-point numbers are added. This function returns a more accurate addition result.  **  Call: Accadd (ARG1,ARG2)  **  return value: Arg1 plus arg2 precise 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 (10, MATH.MAx (R1, R2));    if  (c > 0)  {         var cm = 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 {   &Nbsp;    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 the exact subtraction result  **  explanation: JavaScript subtraction results will be error, when the two floating-point number subtraction will be more obvious. This function returns a more accurate subtraction result.  **  Call: Accsub (ARG1,ARG2)  **  return value: Arg1 plus arg2 precise 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 (10, 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);}   Adds a Mul method to the number type, which is more convenient to call. number.prototype.sub = function  (ARG)  {    return accmul (ARG,  this);};
 /** **  multiplication function to get accurate multiplication results  **  description: JavaScript multiplication results in errors, which are more noticeable when multiplying two floating-point numbers. 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 (10, m);}   Adds a Mul method to the number type, which is more convenient to call. number.prototype.mul = function  (ARG)  {    return accmul (arg, this);}; 
/**  **  division function, which is used to get the exact division result  **  Description: The division result of JavaScript will be error, it will be more obvious when the two floating-point numbers divide. 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 (10, T2 - T1);     }}//adds a Div method to the number type, More convenient to call. number.prototype.div = function  (ARG)  {    return accdiv (this,  ARG);};

Solve the JS floating point (decimal) calculation subtraction bug

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.