The solution of JS floating-point arithmetic bug "Turn"

Source: Internet
Author: User
Tags mul

37.5*5.5=206.08 (JS figure out is such a result, I rounded up to take two decimal places)
I first suspect that the problem is rounding, just use JS to forget a result: 206.08499999999998
How 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.8 JavaScript is calculated as: 5.6000000000000005

Some solutions have been found online, that is, to re-write some floating-point arithmetic functions or directly expand the multiplier operation .
Here are some excerpts from these methods for the reference of friends who encounter the same problem:

------------------------------------------------------------------------------------------------------

Program code

(Method one: override
functions for floating-point arithmetic )

//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 Arg2functionAccdiv (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 (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 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 Arg2functionAccmul (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)} //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 resultfunctionAccadd (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} //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//Description: JavaScript subtraction results are error-evident when the two floating-point numbers are added. This function returns a more accurate subtraction result. //Call: Accsubtr (ARG1,ARG2)//return value: Arg1 minus arg2 's exact resultfunctionACCSUBTR (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));//Dynamic Control accuracy lengthN= (R1>=R2)?r1:r2;return((arg1*m-arg2*m)/m). ToFixed (n);} //Adding a SUBTR method to the number type makes it more convenient to call. NUMBER.PROTOTYPE.SUBTR =function(ARG) {returnACCSUBTR (ARG, This); } 

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 (8)
Similar to other operations, you can get more accurate results.

------------------------------------------------------------------------------------------------------

(Method Two: a function that overrides a floating-point operation )

If you know the number of decimal place, you can consider by multiplying the floating-point number to the integer type (finally divided by the corresponding multiple), and then the operation, so that the correct results can be obtained

Alert (11* (22.9*10)/10);

The solution of JS floating-point arithmetic bug "Turn"

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.