Floating point data operation accuracy Bug

Source: Internet
Author: User
Tags mul

/** * Check whether it is a number * @param arg * @return*/        functionCheckisnumber (ARG) {if(Arg! =NULL&& arg.tostring ()! = ""){                varRe =/^-? (0| [1-9]+\d*| [1-9]+\d*\.\d+|0\.\d+] $/; if(Re.test (arg.tostring ())) {return true; }            }            return false; }        /** * addition function for accurate addition results * 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. * Return value Note: Arg1 and arg2 may be integers, floating-point numbers, null, empty strings, other strings * @param arg1 * @param arg2 * @return*/        functionAccadd (arg1,arg2) {if(!checkisnumber (arg1) | |!Checkisnumber (ARG2)) {                return NULL; }            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;        }        //Adding an Add method to the number type makes it more convenient to call. Number.prototype.add =function(ARG) {returnAccadd ( This, ARG); }        /** subtraction function, used 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.         * Return value: ARG1-ARG2 values, arg1 and arg2 may be integers, floating-point numbers, null, empty strings, other strings * @param arg1 * @param arg2 * @return */        functionaccsub (arg1,arg2) {if(!checkisnumber (arg1) | |!Checkisnumber (ARG2)) {                return NULL; }            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)); //n= (R1>=R2)? R1:R2;//Control accuracy            //return ((arg1*m-arg2*m)/m). ToFixed (n);            return(arg1*m-arg2*m)/m;        }        //Adding a Subduct method to the number type makes it more convenient to call. Number.prototype.subduct =function(ARG) {returnAccsub ( This, ARG); }        /** division function, used to get the exact division result * 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. * Return value: Arg1 divided by arg2 exact result * @param arg1 * @param arg2 * @return*/        functionAccdiv (arg1,arg2) {if(!checkisnumber (arg1) | |!Checkisnumber (ARG2)) {                return NULL; }            varT1,T2,R1,R2; Try{t1=arg1.tostring (). Split (".") [1].length;}Catch(e) {t1=0;} Try{t2=arg2.tostring (). Split (".") [1].length;}Catch(e) {t2=0;}  with(Math) {R1=number (Arg1.tostring (). Replace (".", "")); R2=number (Arg2.tostring (). Replace (".", "")); return(R1/R2) *pow (10,T2-T1);            }         }         //Adding a Div method to the number type makes it easier to call. Number.prototype.div =function(ARG) {returnAccdiv ( This, ARG); }         /** multiplication function for accurate multiplication results * Description: JavaScript multiplication results are error-evident when multiplying two floating-point numbers.         This function returns a more accurate multiplication result. * Return value: Arg1 times the exact result of ARG2 * @param arg1 * @param arg2 * @return*/        functionAccmul (arg1,arg2) {if(!checkisnumber (arg1) | |!Checkisnumber (ARG2)) {                return NULL; }            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);        }         //Adding a Mul method to the number type makes it more convenient to call. Number.prototype.mul =function(ARG) {returnAccmul ( This, ARG); } console.log (Accadd (0.3,0.2) + "" +accsub (0.3,0.2))

Floating point data operation accuracy 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.