The processing of JS numerical calculation to prevent accuracy error

Source: Internet
Author: User
Tags mul pow

(Workaround one: a function that overrides a floating-point operation)


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 arg2


function Accdiv (ARG1,ARG2) {

var t1=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) {

Return Accdiv (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 arg2

function Accmul (ARG1,ARG2)

{

var m=0,s1=arg1.tostring (), s2=arg2.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)

}

Adding a Mul method to the number type makes it more convenient to call.

Number.prototype.mul = function (ARG) {

Return Accmul (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 result

function Accadd (ARG1,ARG2) {

var r1,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) {

Return Accadd (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 result

function Accsubtr (ARG1,ARG2) {

var r1,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 length

N= (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) {

Return ACCSUBTR (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

<script>

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

</script>

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

Everyone Verification verification!!


The processing of JS numerical calculation to prevent accuracy error

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.