Accuracy problem in JS floating point operation

Source: Internet
Author: User
Tags mul

Remember in a project, the use of JS for a series of arithmetic operations, the calculation will have floating-point type, the simple calculation, and finally in the test process, the supervisor in the verification of the data when the results of the calculation is a problem, and then very puzzled, found in the Internet search answer,http/ www.css88.com/archives/7340

Cause: The decimal number in the calculation is converted to binary, calculated, and the result is converted to decimal (often some floating-point type values are converted to binary is infinite), so it can often lead to precision problems

So how to solve it?

The following options are available online:

1 /**2 * * Additive function for accurate addition results3 * * 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. 4 * * Call: Accadd (ARG1,ARG2)5 * * return value: Arg1 plus arg2 accurate results6  **/7 functionAccadd (arg1, arg2) {8     varR1, R2, M, C;9     Try {TenR1 = Arg1.tostring (). Split (".") [1].length; One     } A     Catch(e) { -R1 = 0; -     } the     Try { -r2 = arg2.tostring (). Split (".") [1].length; -     } -     Catch(e) { +r2 = 0; -     } +c = Math.Abs (R1-r2); Am = Math.pow (10, Math.max (R1, r2)); at     if(C > 0) { -         varCM = Math.pow (10, c); -         if(R1 >R2) { -Arg1 = number (arg1.tostring (). Replace (".", "")); -arg2 = number (arg2.tostring (). Replace (".", "")) *cm; -}Else { inArg1 = number (arg1.tostring (). Replace (".", "")) *cm; -arg2 = number (arg2.tostring (). Replace (".", "")); to         } +}Else { -Arg1 = number (arg1.tostring (). Replace (".", "")); thearg2 = number (arg2.tostring (). Replace (".", "")); *     } $     return(Arg1 + arg2)/m;Panax Notoginseng } -   the //Adding an Add method to the number type makes it more convenient to call.  +Number.prototype.add =function(ARG) { A     returnAccadd (ARG, This); the }; +  - /** $ * * Subtraction function 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 accurate results the  **/ - functionaccsub (arg1, arg2) {Wuyi     varR1, R2, M, N; the     Try { -R1 = Arg1.tostring (). Split (".") [1].length; Wu     } -     Catch(e) { AboutR1 = 0; $     } -     Try { -r2 = arg2.tostring (). Split (".") [1].length; -     } A     Catch(e) { +r2 = 0; the     } -m = Math.pow (Ten, Math.max (R1, r2));//Last Modify by Deeka//dynamic control accuracy length $n = (r1 >= r2)?r1:r2; the     return((ARG1 * M-ARG2 * m)/m). toFixed (n); the } the   the //Adding a Mul method to the number type makes it more convenient to call.  -Number.prototype.sub =function(ARG) { in     returnAccmul (ARG, This); the }; the  About  the /** the * * Multiplication function to get accurate multiplication results the * * Note: The multiplication result of JavaScript will be error, it will be more obvious when two floating-point numbers are multiplied. This function returns a more accurate multiplication result.  + * * Call: Accmul (ARG1,ARG2) - * * return value: Arg1 times the exact result of Arg2 the  **/Bayi functionAccmul (arg1, arg2) { the     varm = 0, S1 = arg1.tostring (), S2 =arg2.tostring (); the     Try { -M + = S1.split (".") [1].length; -     } the     Catch(e) { the     } the     Try { theM + = S2.split (".") [1].length; -     } the     Catch(e) { the     } the     returnNumber (S1.replace (".", "")) * Number (S2.replace (".", ""))/Math.pow (10, m);94 } the   the //Adding a Mul method to the number type makes it more convenient to call.  theNumber.prototype.mul =function(ARG) {98     returnAccmul (ARG, This); About }; - 101 102 /** 103 * * Division function to get accurate division results104 * * 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.  the * * Call: Accdiv (ARG1,ARG2)106 * * return value: Arg1 divided by the exact result of Arg2107  **/108 functionAccdiv (arg1, arg2) {109     varT1 = 0, t2 = 0, R1, R2; the     Try {111T1 = arg1.tostring (). Split (".") [1].length; the     }113     Catch(e) { the     } the     Try { theT2 = arg2.tostring (). Split (".") [1].length;117     }118     Catch(e) {119     } -      with(Math) {121R1 = number (arg1.tostring (). Replace (".", ""));122r2 = number (arg2.tostring (). Replace (".", ""));123         return(R1/R2) * POW (T2-t1);124     } the }126  127 //Adding a Div method to the number type makes it easier to call.  -Number.prototype.div =function(ARG) {129     returnAccdiv ( This, ARG); the};

Accuracy problem in JS floating point operation

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.