How to solve the JS floating point calculation bug

Source: Internet
Author: User

This article from: http://hi.baidu.com/bing2liuliu/item/6b201a48ea51c40b6dc2f0b6

37.5*5.5 = 206.08 (JS calculates this result, and I rounded it to two decimal places.) I first suspected it was A Rounding Problem. Then I used js to calculate the result as follows: 206.08499999999998 How can this happen? How can we multiply two digits with only one decimal number. I Googled and found that this was a bug in Javascript floating point operations. For example: 7*0.8 JavaScript code: 5.6000000000000005 Some solutions have been found on the internet, namely re-writing some floating point functions or directly expanding the multiples. The following is an excerpt from these methods for your reference: Bytes ------------------------------------------------------------------------------------------------------ ProgramCode(Method 1: rewrite the floating point function)  //  Division function, used to obtain accurate division results  // Note: There will be an error in the division result of JavaScript, which will be obvious when two floating point numbers are separated. This function returns a more precise division result.  //  Call: accdiv (arg1, arg2)  //  Returned value: the exact result of dividing arg1 by arg2  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 ); }}  //  Add a div Method to the number type to facilitate calling. Number. Prototype. DIV = Function  (ARG ){  Return Accdiv (This  , ARG );}  //  Multiplication function, used to obtain accurate multiplication results  //  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.  //  Call: accmul (arg1, arg2)  //  Returned value: exact result of multiplying arg1 by 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) }  //  Add a mul Method to the number type to facilitate calling. Number. Prototype. Mul = Function  (ARG ){  Return Accmul (ARG, This );}  //  The addition function is used to obtain accurate addition results.  //  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.  //  Call: accadd (arg1, arg2)  //  Returned value: Exact Results of adding arg2 to arg1  Function  Accadd (arg1, arg2 ){  VaR  R1, R2, M;  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 ))  Return (Arg1 * m + arg2 * m)/m }  //  Add an add method to the number type to facilitate calling. Number. Prototype. Add = Function  (ARG ){  Return Accadd (ARG, This );}  //  Subtraction function, used to obtain accurate subtraction results  //  Note: There will be errors in the result of JavaScript subtraction, which will be obvious when two floating point numbers are added. This function returns a more precise subtraction result.  //  Call: accsubtr (arg1, arg2)  //  Returned value: the exact result of arg1 minus arg2  Function  Accsubtr (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 ));  //  Dynamic control precision length N = (r1> = R2 )? R1: R2;  Return (Arg1 * m-arg2 * m)/m). tofixed (N ); }  //  Add a subtr Method to the number type to facilitate calling. Number. Prototype. subtr =Function  (ARG ){  Return Accsubtr (ARG, This  );} Include these functions where you want to use them, and then call them for computation. For example, you want to calculate: Change 7*0.8 to (7). Mul (8 ) Similar to other operations, you can get more accurate results. Bytes ------------------------------------------------------------------------------------------------------ (Method 2: rewrite the floating point function)  //  If you know the number of decimal places, you can consider increasing the number of floating point numbers to an integer (and then dividing it by the corresponding multiples) Before performing operations, so that you can get the correct result. <SCRIPT> Alert ( 11 * (22.9*10)/10); </SCRIPT>

 

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.