Subtraction operation of JS floating-point number

Source: Internet
Author: User
Tags mul pow

Article source address: http://blog.csdn.net/lyd518/article/details/7236464 reprint Please indicate the source, respect the author's labor results, thank you!
The problem is this:37.5*5.5=206.08(JS figure out is such a result, I rounded to take two decimal places) I first suspected is rounding problem, directly with JS a result for:206.08499999999998How can this, two only one decimal number multiplication, how can more out of this decimal point out.  I Google a bit, found that this is a JavaScript floating point operation of a bug. For example:7*0.8JavaScript calculates that it is:5.6000000000000005Some solutions have been found online, which is to re-write some functions of floating-point arithmetic. Here are excerpts of these methods, for the same problem of Friends Reference: program 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 Arg2function Accdiv (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 (Ten, 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 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 Arg2function Accmul (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 (Ten, M)} //Adding a Mul method to the number type makes it more convenient to call. Number.prototype.mul=function (ARG) {returnAccmul (ARG, This); } //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 resultfunction Accadd (arg1, arg2) {varR1, R2, M, C; Try{r1 = arg1.tostring (). Split (".")[1].length}Catch(e) {r1 =0 } Try{r2 = arg2.tostring (). Split (".")[1].length}Catch(e) {r2 =0} C= Math.Abs (R1-R2); M= Math.pow (Ten, Math.max (R1, r2))if(C >0) { varCM = Math.pow (Ten, c); if(R1 >R2) {Arg1= Number (arg1.tostring (). Replace (".","")); Arg2= Number (arg2.tostring (). Replace (".","")) *cm; } Else{arg1= Number (arg1.tostring (). Replace (".","")) *cm; Arg2= Number (arg2.tostring (). Replace (".","")); } } Else{arg1= Number (arg1.tostring (). Replace (".","")); Arg2= Number (arg2.tostring (). Replace (".","")); } return(Arg1 + arg2)/m}//Adding an Add method to the number type makes it more convenient to call. Number.prototype.add=function (ARG) {returnAccadd (ARG, This); }//subtraction function, which is used to get the exact subtraction result//Note: JavaScript subtraction results are error-evident when subtracting two floating-point numbers. This function returns a more accurate subtraction result. //Call: Accsub (ARG1,ARG2)//return value: Arg1 plus arg2 's exact resultfunction Accsub (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 (Ten, Math.max (R1,R2)); //Last modify by Deeka//Dynamic Control accuracy lengthN= (R1>=R2)?R1:R2; return((arg1*m-arg2*m)/m). toFixed (n);}  Include these functions where you want them, and then call it to calculate. For example you want to calculate:7*0.8, then change to (7). Mul (8Other operations are similar, so that more accurate results can be obtained.

Subtraction operation of JS floating-point number

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.