JavaScript floating point arithmetic

Source: Internet
Author: User

<script type= "Text/javascript" >//Two floating-point number summation function Accadd (num1,num2) {var r1,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;    Return Math.Round (num1*m+num2*m)/m;       }//Two floating-point subtraction function accsub (num1,num2) {var r1,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);       }//Two number divide function Accdiv (num1,num2) {var t1,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);     } function Accmul (num1,num2) {var m=0,s1=num1.tostring (), s2=num2.tostring (); Try{m+=s1.split (".")    [1].length}catch (e) {}; Try{m+=s2.split (".")    [1].length}catch (e) {};    Return number (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/>

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.