JS中准確的浮點運算庫


將如下代碼,拷貝到文件myMath.js文件中,在相應的頁面進行引用:

<script type="text/javascript" src="../js/myMath.js"></script>

即可調用相應的加減乘除運算了.

示例js代碼(浮點數減法運算):

    

<script type="text/javascript">
var a=2.2,b=1.1;
document.write(a.sub(b));
</script>

myMath.js代碼如下:

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

/**
* Created by Jerry Tong on 2016/7/4.
*/
Math.add = function(v1, v2)
{
///<summary>精確計算加法。語法:Math.add(v1, v2)</summary>
///<param name="v1" type="number">操作數。</param>
///<param name="v2" type="number">操作數。</param>
///<returns type="number">計算結果。</returns>
var r1, r2, m;
try
{
r1 = v1.toString().split(".")[1].length;//1個加數的小數的位數
}
catch (e)
{
r1 = 0;
}
try
{
r2 = v2.toString().split(".")[1].length;//2個加數的小數的位數
}
catch (e)
{
r2 = 0;
}
m = Math.pow(10, Math.max(r1, r2));//小數位數最多的

return (v1 * m + v2 * m) / m;//整數運算再還原
}


Number.prototype.add = function(v)
{
///<summary>精確計算加法。語法:number1.add(v)</summary>
///<param name="v" type="number">操作數。</param>
///<returns type="number">計算結果。</returns>
return Math.add(v, this);
}


Math.sub = function(v1, v2)
{
///<summary>精確計算減法。語法:Math.sub(v1, v2)</summary>
///<param name="v1" type="number">操作數。</param>
///<param name="v2" type="number">操作數。</param>
///<returns type="number">計算結果。</returns>
return Math.add(v1, -v2);//減法是帶負號的加法運算
}


Number.prototype.sub = function(v)
{
///<summary>精確計算減法。語法:number1.sub(v)</summary>
///<param name="v" type="number">操作數。</param>
///<returns type="number">計算結果。</returns>
return Math.sub(this, v);
}


Math.mul = function(v1, v2)
{
///<summary>精確計算乘法。語法:Math.mul(v1, v2)</summary>
///<param name="v1" type="number">操作數。</param>
///<param name="v2" type="number">操作數。</param>
///<returns type="number">計算結果。</returns>
var m = 0;
var s1 = v1.toString();
var s2 = v2.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);
}


Number.prototype.mul = function(v)
{
///<summary>精確計算乘法。語法:number1.mul(v)</summary>
///<param name="v" type="number">操作數。</param>
///<returns type="number">計算結果。</returns>
return Math.mul(v, this);
}


Math.div = function(v1, v2)
{
///<summary>精確計算除法。語法:Math.div(v1, v2)</summary>
///<param name="v1" type="number">操作數。</param>
///<param name="v2" type="number">操作數。</param>
///<returns type="number">計算結果。</returns>
var t1 = 0;
var t2 = 0;
var r1, r2;
try
{
t1 = v1.toString().split(".")[1].length;
}
catch (e)
{
}
try
{
t2 = v2.toString().split(".")[1].length;
}
catch (e)
{
}

with (Math)
{
r1 = Number(v1.toString().replace(".", ""));
r2 = Number(v2.toString().replace(".", ""));
return (r1 / r2) * pow(10, t2 - t1);
}
}


Number.prototype.div = function(v)
{
///<summary>精確計算除法。語法:number1.div(v)</summary>
///<param name="v" type="number">操作數。</param>
///<returns type="number">計算結果。</returns>
return Math.div(this, v);
}

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


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com