JavaScript accurate calculation

Source: Internet
Author: User
Tags mul

An article:

4 JavaScript libraries to perform advanced math calculations

Numbers.js

Numeric Javascript

Accounting.js

Tangle

Sometimes only the addition and subtraction of the multiplication can be accurate, there is no need to use the library. First look at the JS floating point arithmetic problem.


JS in the multiplication of small numbers will appear floating point error, specifically can be tested:

<script>
Alert (11*22.9)
</script>

The result is 251.89999999999998, not 251.9.

There must be a lot of headaches for this problem. So how to solve it? The solution is given here.
1.

<script>
Alert (11* (22.9*10)/10);
</script>

The general idea to solve the problem is to enlarge the factor into an integer and then divide it by the corresponding multiple so that the correct result can be obtained.
2.

<script defer>
Number.prototype.rate=function () {
var ostr=this.tostring ();
if (Ostr.indexof (".") ==-1)
return 1;
Else
Return Math.pow (10,parseint (Ostr.length-ostr.indexof (".") -1));
}

Function Tran () {
args=tran.arguments;
var temp=1;
for (i=0;i<args.length;i++)
temp*=args[i]*args[i].rate ();
for (i=0;i<args.length;i++)
temp/=args[i].rate ();
Return Temp
}

Alert (Tran (11,22.9));

</script>

The solution is a tricky one, but it gives you an idea of the actual process of solving the problem.

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);
}

Description: Turn from Baidu space Xiao Yu blog.

Transferred from: http://blog.sina.com.cn/s/blog_6fef4b860100p2n0.html

Reference: http://www.cnblogs.com/konooo/archive/2010/01/23/1654617.html

JavaScript accurate calculation

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.