_javascript techniques for JavaScript floating-point arithmetic precision problem

Source: Internet
Author: User
Tags arithmetic mul pow
JS Code
Copy Code code as follows:

<script type= "Text/javascript" language= "JavaScript" >
alert (1/3);//Eject: 0.3333333333333333
Alert (0.09999999 + 0.00000001);//Eject: 0.09999999999999999
alert ( -0.09999999-0.00000001);//Eject:-0.09999999999999999
Alert (0.012345 * 0.000001);//Eject: 1.2344999999999999e-8
alert (0.000001/0.0001);//Eject: 0.009999999999999998
</script>
<script type= "Text/javascript" language= "JavaScript" >
alert (1/3);//Eject: 0.3333333333333333
Alert (0.09999999 + 0.00000001);//Eject: 0.09999999999999999
alert ( -0.09999999-0.00000001);//Eject:-0.09999999999999999
Alert (0.012345 * 0.000001);//Eject: 1.2344999999999999e-8
alert (0.000001/0.0001);//Eject: 0.009999999999999998
</script>
[Code]
According to normal calculation, except the first line (because it can not be removed), the other should be accurate results, from the pop-up results we find that is not the correct result we want. In order to solve the problem of inaccurate floating-point operation, we will first upgrade the number of participating operations (the second side of 10) to integers, and then demote (0.1 of the second side of X) before the operation. It is collected and collated and put on it for later use.
Addition
JS Code
[Code]
Description: JavaScript addition results will be error, in two floating-point number added when it is more obvious. This function returns a more precise 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 is more convenient to call.
Number.prototype.add = function (ARG) {
Return Accadd (Arg,this);
}
Description: JavaScript addition results will be error, in two floating-point number added when it is more obvious. This function returns a more precise 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 is more convenient to call.
Number.prototype.add = function (ARG) {
Return Accadd (Arg,this);
}
Subtraction
JS Code
[Code]
Description: JavaScript subtraction results will be error, in two floating-point numbers added when it is more obvious. This function returns a more accurate subtraction result.
Call: Accsub (ARG1,ARG2)
Return value: arg1 The exact result of reducing ARG2
function Accsub (ARG1,ARG2) {
Return Accadd (ARG1,-ARG2);
}
Adding a sub method to the number type is more convenient to call.
Number.prototype.sub = function (ARG) {
Return Accsub (THIS,ARG);
}
Description: JavaScript subtraction results will be error, in two floating-point numbers added when it is more obvious. This function returns a more accurate subtraction result.
Call: Accsub (ARG1,ARG2)
Return value: arg1 The exact result of reducing ARG2
function Accsub (ARG1,ARG2) {
Return Accadd (ARG1,-ARG2);
}
Adding a sub method to the number type is more convenient to call.
Number.prototype.sub = function (ARG) {
Return Accsub (THIS,ARG);
}

Multiplication
JS Code
Copy Code code as follows:

Description: JavaScript multiplication results will be error, in two floating-point numbers are more obvious when multiplying. This function returns a more accurate result of the multiplication.
Call: Accmul (ARG1,ARG2)
return value: Arg1 times Arg2 's exact result
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 is more convenient to call.
Number.prototype.mul = function (ARG) {
Return Accmul (ARG, this);
}
Description: JavaScript multiplication results will be error, in two floating-point numbers are more obvious when multiplying. This function returns a more accurate result of the multiplication.
Call: Accmul (ARG1,ARG2)
return value: Arg1 times Arg2 's exact result
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 is more convenient to call.
Number.prototype.mul = function (ARG) {
Return Accmul (ARG, this);
} Division
JS Code
Description: JavaScript division results will be error, the two floating-point numbers are more obvious when dividing. This function returns a more precise division result.
Call: Accdiv (ARG1,ARG2)
Return value: Arg1 divided by Arg2 's exact result
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 is more convenient to call.
Number.prototype.div = function (ARG) {
Return Accdiv (this, ARG);
}
Description: JavaScript division results will be error, the two floating-point numbers are more obvious when dividing. This function returns a more precise division result.
Call: Accdiv (ARG1,ARG2)
Return value: Arg1 divided by Arg2 's exact result
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 is more convenient to call.
Number.prototype.div = function (ARG) {
Return Accdiv (this, ARG);
}

To test a
JS Code
Copy Code code as follows:

<script type= "Text/javascript" language= "JavaScript" >
/*
Al ERT (0.09999999 + 0.00000001);//eject: 0.09999999999999999
Alert ( -0.09999999-0.00000001);//Eject:-0.09999999999999999
Alert (0.012345 * 0.000001);//eject: 1.2344999999999999e-8
Alert (0.000001/0.0001);//eject: 0.009999999999999998 */
Alert (0.09999999.add (0.00000001));//Eject: 0.1
Alert ( -0.09999999.sub (0.00000001));/eject: -0.09999998
A Lert (0.012345.mul (0.000001));//eject: 1.2345e-8
Alert (0.000001.div (0.0001));//eject: 0.01
</script>
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.