overriding JavaScript floating-point arithmetic

Source: Internet
Author: User
Tags pow

Variables stored in JavaScript do not distinguish between number and float types, and are stored in the same way as float;

JavaScript uses the 64bit floating-point format defined by the IEEE 754-2008 standard to store number,decimal64 corresponding to a portion length of 10 and a small tree part length of 16;

JavaScript decimal operations can appear imprecise.

//addition Operation        varAccadd =function(num1,num2) {varn1,n2,m; Try{N1= Num1.tostring (). Split (".") [1].length; }Catch(e) {N1= 0; }            Try{n2= Num2.tostring (). Split (".") [1].length; }Catch(e) {n2= 0; } m= Math.pow (10, Math.max (N1,N2)); return(Num1*m + num2*m)/m;        }        //Subtraction Operations        varSuntr =function(num1,num2) {varN1,n2,m,n; Try{N1= Num1.tostring (). Split (".") [1].length; }Catch(e) {N1= 0; }            Try{n2= Num2.tostring (). Split (".") [1].length; }Catch(e) {n2= 0; } m= Math.pow (10, Math.max (N1,N2)); N= (N1 > N2)?n1:n2; return((num1*m-num2*m)/m). ToFixed (n);        }        //Multiplication Operations        varAccmul =function(num1,num2) {varm = 0, n1,n2, S1=num1.tostring (), S2=num2.tostring (); Try{m+ = S1.split (".") [1].length; }Catch(e) {}Try{m+ = S2.split (".") [1].length; }Catch(e) {} n1= Number (S1.replace (".", "")); N2= Number (S2.replace (".", "")); returnN1 * N2/MATH.POW (10, M); }        //Division Operation        varAccdivi =function(num1,num2) {varS1 = 0,S2 = 0, n1,n2; Try{S1= Num1.tostring (). Split (".") [1].length; }Catch(e) {}Try{s2= Num2.tostring (). Split (".") [1].length; }Catch(e) {} with(Math) {N1= Number (num1.tostring (). Replace (".", "")); N2= Number (num2.tostring (). Replace (".", "")); return(N1/N2) *math.pow (10,S2-S1);            }        }

overriding JavaScript floating-point arithmetic

Related Article

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.