The accuracy of JS floating-point number

Source: Internet
Author: User

Most languages encounter precision problems when dealing with floating-point numbers, but in JS it seems particularly serious to look at an example

alert(45.6*13);

It turned out to be 592.800000000001, and of course the addition would have this problem.

So this is JS's mistake?

Of course not, your computer is doing the correct binary floating-point arithmetic, but the problem is that you enter the decimal number, the computer is binary operation, the two are not always converted so good, sometimes get the right results, but sometimes not so lucky

alert(0.7+0.1);//输出0.7999999999999999alert(0.6+0.2);//输出0.8

You enter two decimal numbers, converted to binary operations and then converted back, there will be a natural loss in the conversion process

But the general loss is often in the multiplication operation more, and JS in the simple addition and subtraction will also appear in this kind of problem, you see, this error is very small, but it should not appear

How to solve it, ECMA4 seems to give a solution, but now is not so practical

One method, such as 0.7+0.1, is to multiply 0.1 and 0.7 by 10 before adding 10.

In addition, you can write some functions to solve this problem, Baidu Google should have a lot of their own, but it is best not to use JS to do some complex floating-point operations, after all, JS more role is not in this

<script type= "Text/javascript" >//sum of two floating-point numbers    functionAccadd (num1,num2) {varr1,r2,m; Try{R1= Num1.tostring (). Split ('. ') [1].length; }Catch(e) {R1= 0; }       Try{R2=num2.tostring (). Split (".") [1].length; }Catch(e) {R2=0; } m=math.pow (10, Math.max (R1,R2)); //return (num1*m+num2*m)/m;       returnMath.Round (num1*m+num2*m)/m;    }        //two floating-point number subtraction    functionaccsub (num1,num2) {varr1,r2,m; Try{R1= Num1.tostring (). Split ('. ') [1].length; }Catch(e) {R1= 0; }       Try{R2=num2.tostring (). Split (".") [1].length; }Catch(e) {R2=0; } m=math.pow (10, Math.max (R1,R2)); N= (R1&GT;=R2)?R1:R2; return(Math.Round (num1*m-num2*m)/m). ToFixed (n);    }    //divide two numbers    functionAccdiv (num1,num2) {varT1,T2,R1,R2; Try{T1= Num1.tostring (). Split ('. ') [1].length; }Catch(e) {T1= 0; }       Try{T2=num2.tostring (). Split (".") [1].length; }Catch(e) {T2=0; } R1=number (Num1.tostring (). Replace (".", "")); R2=number (Num2.tostring (). Replace (".", "")); return(R1/R2) *math.pow (10,T2-T1);    }        functionAccmul (num1,num2) {varM=0,s1=num1.tostring (), s2=num2.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);    }      </script> <script>document.write ("Using JS Primitive Ecological Method"); document.write ("<br/> 1.01 + 1.02 =" + (1.01 + 1.02)); document.write ("<br/> 1.01-1.02 =" + (1.01-1.02)); document.write ("<br/> 0.000001/0.0001 =" + (0.000001/0.0001)); document.write ("<br/> 0.012345 * 0.000001 =" + (0.012345 * 0.000001)); document.write ("<br/>); document.write ("<br/> Using custom Methods"); document.write ("<br/> 1.01 + 1.02 =" +accadd (1.01,1.02)); document.write ("<br/> 1.01-1.02 =" +accsub (1.01,1.02)); document.write ("<br/> 0.000001/0.0001 =" +accdiv (0.000001,0.0001)); document.write ("<br/> 0.012345 * 0.000001 =" +accmul (0.012345,0.000001)); </script>

The accuracy 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.