Solve the problem of loss of computational precision in JavaScript

Source: Internet
Author: User
Tags mul pow

In the project before the teacher gave us a package of a JS file, to solve the loss of precision in the calculation of some functions, direct reference to the JS file can be used.

eg

var NumA = 0.1;
var NumB = 0.2;
Alert (NumA + NumB);

Results occurred: 0.1 + 0.2 = 0.30000000000000004

Why this problem occurs: the computer reads the binary, not the decimal, is the program in the conversion time lost precision.

To resolve the problem code:

    //The division function, which is used to get accurate division results.    //Description: The result of the division of JavaScript will be error, which will be obvious when dividing two floating-point numbers. This function returns a more accurate division result.     //Call: Accdiv (ARG1,ARG2)    //return value: Arg1 divided by the exact result of Arg2    functionAccdiv (arg1, arg2) {varT1 = 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); }    }    //multiplication function to get the exact multiplication result    //Description: JavaScript multiplication results are error-evident 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    functionAccmul (arg1, arg2) {varm = 0, S1 = arg1.tostring (), S2 =arg2.tostring (); Try{m + = S1.split (".") [1].length}Catch(e) {}Try{m + = S2.split (".") [1].length}Catch(e) {}returnNumber (S1.replace (".", "")) * Number (S2.replace (".", ""))/Math.pow (10, M); }    //An addition function that is used to obtain accurate addition results    //Note: The addition of JavaScript will have an error, and it will be more 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 's exact result    functionAccadd (arg1, arg2) {varR1, 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; }    //Subtraction Function    functionaccsub (arg1, arg2) {varR1, 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 lengthn = (r1 >= r2)?R1:R2; return((ARG2 * M-ARG1 * m)/m). toFixed (n); }            //Adding an Add method to the number type makes it more convenient to call. Number.prototype.add =function(ARG) {returnAccadd (ARG, This);    }; //Adding a sub method to the number class makes it easier to call.Number.prototype.sub =function(ARG) {returnAccsub (ARG, This);    }; //Add a Mul method to the number typeNumber.prototype.mul =function(ARG) {returnAccmul (ARG, This);    }; //extend a Div method to the number typeNumber.prototype.div =function(ARG) {returnAccdiv ( This, ARG); };

Solve the problem of loss of computational precision in JavaScript

Related Article

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.