Solve the bug of JS floating point number (decimal) computing addition, subtraction, multiplication, division

Source: Internet
Author: User
1   /*  *  2   ** The addition function is used to obtain accurate addition results.  3   ** Note: The addition result of JavaScript has an error, which is obvious when two floating point numbers are added. This function returns a more accurate addition result.  4   ** Call: accadd (arg1, arg2)  5   ** Return value: exact result of adding arg1 to arg2  6   *  */  7   Function Accadd (arg1, arg2 ){  8       VaR  R1, R2, M, C;  9       Try  {  10 R1 = arg1.tostring (). Split (".") [1 ]. Length;  11   }  12       Catch  (E ){  13 R1 = 0 ; 14   }  15       Try  {  16 R2 = arg2.tostring (). Split (".") [1 ]. Length;  17   }  18       Catch  (E ){  19 R2 = 0 ;  20   } 21 C = math. Abs (R1- R2 );  22 M = math. Pow (10 , Math. Max (R1, R2 ));  23       If (C> 0 ){  24           VaR Cm = math. Pow (10 , C );  25           If (R1> R2 ){  26 Arg1 = Number (arg1.tostring (). Replace (".",""));  27 Arg2 = Number (arg2.tostring (). Replace (".",""))* Cm;  28 } Else  {  29 Arg1 = Number (arg1.tostring (). Replace (".",""))* Cm;  30 Arg2 = Number (arg2.tostring (). Replace (".","" ));  31   }  32 }Else  {  33 Arg1 = Number (arg1.tostring (). Replace (".","" ));  34 Arg2 = Number (arg2.tostring (). Replace (".","" ));  35   }  36       Return (Arg1 + arg2 )/ M;  37   }  38   39  //  Add an add method to the number type to facilitate calling.  40 Number. Prototype. Add = Function  (ARG ){  41       Return Accadd (ARG, This  );  42 };
 1   /*  *  2   ** Subtraction function, used to obtain accurate subtraction results  3  ** Note: javascript subtraction results in errors, which are obvious when two floating point numbers are subtracted. This function returns a more precise subtraction result.  4   ** Call: accsub (arg1, arg2)  5   ** Return value: exact result of adding arg1 to arg2  6   *  */  7   Function  Accsub (arg1, arg2 ){  8       VaR  R1, R2, M, N;  9       Try {  10 R1 = arg1.tostring (). Split (".") [1 ]. Length;  11   }  12       Catch  (E ){  13 R1 = 0 ;  14   }  15       Try  {  16 R2 = arg2.tostring (). Split (".") [1 ]. Length;  17   }  18       Catch  (E ){  19 R2 = 0 ;  20   }  21 M = math. Pow (10, math. Max (R1, R2 )); //  Last modify by deeka // dynamically control the Precision Length  22 N = (r1> = R2 )?R1: R2;  23       Return (Arg1 * m-arg2 * m )/ M). tofixed (N );  24   }  25   26   //  Add a mul Method to the number type to facilitate calling.  27 Number. Prototype. sub = Function  (ARG ){  28       Return Accmul (ARG,This  );  29 };
 1   /*  *  2   ** Multiplication function, used to obtain accurate multiplication results  3   ** 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.  4   ** Call: accmul (arg1, arg2)  5   ** Return value: exact result of multiplying arg1 by arg2  6   * */  7   Function  Accmul (arg1, arg2 ){  8       VaR M = 0, S1 = arg1.tostring (), S2 = Arg2.tostring ();  9       Try  {  10 M + = s1.split (".") [1 ]. Length;  11   }  12       Catch (E ){  13   }  14       Try  {  15 M + = s2.split (".") [1 ]. Length;  16   }  17       Catch  (E ){  18   }  19       Return Number (s1.replace (".", "") * Number (s2.replace (".", "")/Math. Pow (10 , M );  20   }  21   22   //  Add a mul Method to the number type to facilitate calling.  23 Number. Prototype. Mul = Function  (ARG ){  24       Return Accmul (ARG, This  );  25 };
 1   /*  *  2   ** Division function, used to obtain accurate division results  3   ** Note: there is an error in the division result of JavaScript, which is obvious when two floating point numbers are separated. This function returns a more precise division result.  4   ** Call: accdiv (arg1, arg2)  5   ** Return value: the exact result of dividing arg1 by arg2  6   *  */  7   Function Accdiv (arg1, arg2 ){  8       VaR T1 = 0, T2 = 0 , R1, R2;  9       Try  {  10 T1 = arg1.tostring (). Split (".") [1 ]. Length;  11   }  12       Catch  (E ){  13   } 14       Try  {  15 T2 = arg2.tostring (). Split (".") [1 ]. Length;  16   }  17       Catch  (E ){  18   }  19       With  (Math ){  20 R1 = Number (arg1.tostring (). Replace (".","" ));  21 R2 = Number (arg2.tostring (). Replace (".","" ));  22           Return (R1/R2) * POW (10, T2- T1 );  23   }  24   }  25   26   //  Add a div Method to the number type to facilitate calling. 27 Number. Prototype. DIV = Function  (ARG ){  28       Return Accdiv ( This  , ARG );  29 };

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.