//乘法函数,用来得到精确的乘法结果 //说明:javascript的乘法结果会有误差,在两个浮点数相乘的时候会比较明显。这个函数返回较为精确的乘法结果。 //调用:accMul(arg1,arg2) //返回值:arg1乘以arg2的精确结果 function accMul(arg1,arg2) { var m=0,s1=arg1.toString(),s2=arg2.toString(); try{ if(s1.split(".")[1] != undefined ) m+=s1.split(".")[1].length }catch(e){} try{ if(s2.split(".")[1] != undefined ) m+=s2.split(".")[1].length }catch(e){} return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m) } //浮点数除法运算 function accDiv(arg1,arg2){ var r1=0,r2=0,m,s1=arg1.toString(),s2=arg2.toString(); try{ if(s1.split(".")[1] != undefined ) r1=s1.split(".")[1].length; }catch(e){} try{ if(s2.split(".")[1] != undefined ) r2=s2.split(".")[1].length; }catch(e){} m=Math.pow(10,Math.max(r1,r2)); return (accMul(arg1,m))/(accMul(arg2,m)); } function accAdd(arg1,arg2){ var r1=0,r2=0,m,s1=arg1.toString(),s2=arg2.toString(); try{ if(s1.split(".")[1] != undefined ) r1=s1.split(".")[1].length; }catch(e){} try{ if(s2.split(".")[1] != undefined ) r2=s2.split(".")[1].length; }catch(e){} m=Math.pow(10,Math.max(r1,r2)); return (accMul(arg1,m)+accMul(arg2,m))/m; } function Subtr(arg1,arg2){ var r1=0,r2=0,m,n,s1=arg1.toString(),s2=arg2.toString(); try{ if(s1.split(".")[1] != undefined ) r1=s1.split(".")[1].length; }catch(e){} try{ if(s2.split(".")[1] != undefined ) r2=s2.split(".")[1].length; }catch(e){} m=Math.pow(10,Math.max(r1,r2)); //last modify by deeka //动态控制精度长度 n=(r1>=r2)?r1:r2; return (accMul(arg1,m)-accMul(arg2,m))/m; }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/industrynews/13289.html