Method of solving floating-point errors in multiplication in JS

Source: Internet
Author: User
Tags mul pow tostring

  This article mainly introduces the method of solving floating-point errors of multiplication in JS. Need friends can come to the reference, I hope to help you.

JS small number of multiplication operations will appear floating-point errors, can be tested:     <script>  alert (11*22.9)   </script>    The result is 251.89999999999998 instead of 251.9     The problem must have been a lot of headaches for many people. So how to solve it? A solution is given here.     1,     code is as follows: <script>  alert (11* (22.9*10)/10);  </script>    & nbsp The general idea of solving the problem is to enlarge the factor into an integer, and then divide it by the corresponding multiples, so that the correct result can be obtained.   2,   code as follows: <script defer>  number.prototype.rate=function () {  var ostr=this.tostring ();   if (Ostr.indexof (".") ==-1)   return 1;  else  return Math.pow (10,parseint (".") -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.     can also use four homesFive-in method,  js can use Math.Round to achieve rounding integers, if you need to implement accurate to the number of decimal points, you need to write a function.   Code as follows: function Fordight (dight,how) {   dight = Math.Round (Dight*math.pow (10,how))/math.pow (10,how); nbsp    return dight; }     //Division function, for Accurate division results//Description: JavaScript division results will be error, it will be more obvious when dividing two floating-point numbers. This function returns a more precise division result. Call: Accdiv (ARG1,ARG2)//return value: Arg1 divided by arg2 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); }  //Add a Div method to the number type, which is more convenient to call. Number.prototype.div = function (ARG) {return Accdiv (this, arg);}  //multiplication function, to get the exact result of multiplication//description: JavaScript multiplication results will be error, It is more noticeable when multiplying two floating-point numbers. 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);}  //addition function, used to get exact addition result//description: JavaScript addition result will be error, in two Floating-point numbers are added more clearly. This function returns a more precise addition result. Call: Accadd (ARG1,ARG2)//return value: Arg1 plus arg2 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}  //Add an Add method to the number type. More convenient to call up. Number.prototype.add = function (ARG) {return accadd (arg,this);}    

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.