Javascript 浮点数精确计算

时间:2023-01-08 09:50:09
浮点数计算精度问题是javascript自身存在的问题。

比较经典的例子为2.0-1.1, 计算的结果为0.8999999999999999。

精确计算的基本思路是将其全部转换为整数后计算,整数计算不存在误差,然后再除以之前乘的数。

如下代码1为测试html,引用的js见代码2。也可以下载附件,附件中已经都包含。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>浮点数精确计算测试</title>
<script type="text/javascript" src="js/FloatCumulateV1.js"></script>
<script type="text/javascript">
window.onload = function(){
document.getElementById("1").innerHTML = (0.01/0.1) ;
document.getElementById("2").innerHTML = (Number(0.01).div(Number(0.1))) ;

document.getElementById("3").innerHTML = (1.5*0.1) ;
document.getElementById("4").innerHTML = (Number(1.5).mul(Number(0.1))) ;

document.getElementById("5").innerHTML = (0.09999999+0.00000001) ;
document.getElementById("6").innerHTML = (Number(0.09999999).add(Number(0.00000001))) ;

document.getElementById("7").innerHTML = (2.0-1.1) ;
document.getElementById("8").innerHTML = (Number(2.0).sub(Number(1.1))) ;

document.getElementById("9").innerHTML = (Number(10).div(Number(0.1))) ;
document.getElementById("10").innerHTML = (Number(10).mul(Number(0.1))) ;
document.getElementById("11").innerHTML = (Number(10).add(Number(0.2))) ;
document.getElementById("12").innerHTML = (Number(10).sub(Number(1.1))) ;

}

</script>
</head>
<body>
<h1>浮点数精确计算测试</h1>
<p>普通js除法计算: 0.01/0.1 = <label id="1"> </label></p>
<p>当前js除法计算: 0.01/0.1 = <label id="2"> </label></p>

<p>普通js乘法计算: 1.5*0.1 = <label id="3"> </label></p>
<p>当前js乘法计算: 1.5*0.1 = <label id="4"> </label></p>

<p>普通js加法计算: 0.09999999+0.00000001 = <label id="5"> </label></p>
<p>当前js加法计算: 0.09999999+0.00000001 = <label id="6"> </label></p>

<p>普通js减法计算: 2.0-1.1 = <label id="7"> </label></p>
<p>当前js减法计算: 2.0-1.1 = <label id="8"> </label></p>


<p>当前js除法计算(不带小数,测试Exception的分支): 10/0.1 = <label id="9"> </label></p>
<p>当前js乘法计算(不带小数,测试Exception的分支): 10*0.1 = <label id="10"> </label></p>
<p>当前js加法计算(不带小数,测试Exception的分支): 10+0.2 = <label id="11"> </label></p>
<p>当前js减法计算(不带小数,测试Exception的分支): 10-1.1= <label id="12"> </label></p>
</body>
</html>


此html运行结果:
浮点数精确计算测试
普通js除法计算: 0.01/0.1 = 0.09999999999999999

当前js除法计算: 0.01/0.1 = 0.1

普通js乘法计算: 1.5*0.1 = 0.15000000000000002

当前js乘法计算: 1.5*0.1 = 0.15

普通js加法计算: 0.09999999+0.00000001 = 0.09999999999999999

当前js加法计算: 0.09999999+0.00000001 = 0.1

普通js减法计算: 2.0-1.1 = 0.8999999999999999

当前js减法计算: 2.0-1.1 = 0.9

当前js除法计算(不带小数,测试Exception的分支): 10/0.1 = 100

当前js乘法计算(不带小数,测试Exception的分支): 10*0.1 = 1

当前js加法计算(不带小数,测试Exception的分支): 10+0.2 = 10.2

当前js减法计算(不带小数,测试Exception的分支): 10-1.1= 8.9

计算的JS
function accDiv(arg1, arg2)
{
var tmp1 = 0;
var tmp2 = 0;
var result1 = 0;
var result2 = 0;

//获取异常的目的是防止存在整数的情况。
try
{
tmp1 = arg1.toString().split(".")[1].length;
}
catch(e)
{
tmp1 = 0;
}

try
{
tmp2 = arg2.toString().split(".")[1].length;
}
catch(e)
{
tmp2 = 0;
}

result1 = Number(arg1.toString().replace(".", ""));
result2 = Number(arg2.toString().replace(".", ""));

return (result1 / result2) * Math.pow(10, tmp2 - tmp1);
}

Number.prototype.div = function(arg) {
return accDiv(this, arg);
}

function accMul(arg1, arg2)
{
var tmp1 = 0;
var tmp2 = 0;
var result1 = 0;
var result2 = 0;

try
{
tmp1 = arg1.toString().split(".")[1].length;
}
catch (e)
{
tmp1 = 0;
}

try
{
tmp2 = arg2.toString().split(".")[1].length;
}
catch (e)
{
tmp2 = 0;
}

result1 = Number(arg1.toString().replace(".", ""));
result2 = Number(arg2.toString().replace(".", ""));

return (result1 * result2) / Math.pow(10, tmp1 + tmp2);
}

Number.prototype.mul = function(arg) {
return accMul(this, arg);
}

function accAdd(arg1, arg2)
{
var tmp1 = 0;
var tmp2 = 0;
var tmp = 0;
var result1 = 0;
var result2 = 0;
try
{
tmp1 = arg1.toString().split(".")[1].length;
}
catch (e)
{
tmp1 = 0;
}

try
{
tmp2 = arg2.toString().split(".")[1].length;
}
catch (e)
{
tmp2 = 0;
}

//arg1 + arg2 = ( result1/Math.pow(10,tmp1) ) + ( result2/Math.pow(10,tmp2) )
//1.5+0.02 = 15/10 + 2/100
//1.5+0.02 = 1.5*100/100 + 2*100/100
tmp = Math.max(tmp1, tmp2);

/**
* 解决 319500-301418.09
* 38890.91+116672.73+38890.91
* 2.0-1.1
* 对于javascript计算,直接*10或者*100也会出现精度错误,因此采用字符串的方式解决
*/

result1 = arg1;
result2 = arg2;

var tmpStr = '';
if (tmp>0)
{
tmpStr = arg1.toString().replace('.','');
for (var i=0;i<tmp-tmp1;i++)
{
tmpStr += '0';
}
result1 = Number(tmpStr);

tmpStr = arg2.toString().replace('.','');
for (var i=0;i<tmp-tmp2;i++)
{
tmpStr += '0';
}
result2 = Number(tmpStr);
}

//return (arg1 * Math.pow(10, tmp) + arg2 * Math.pow(10, tmp)) / Math.pow(10, tmp);
return (result1 + result2) / Math.pow(10, tmp);

}

Number.prototype.add = function(arg) {
return accAdd(this, arg);
}

//减法的思路可以参照加法的思路。
function accSub(arg1, arg2)
{
var tmp1 = 0;
var tmp2 = 0;
var tmp = 0;
var result1 = 0;
var result2 = 0;

try
{
tmp1 = arg1.toString().split(".")[1].length;
}
catch (e)
{
tmp1 = 0;
}

try
{
tmp2 = arg2.toString().split(".")[1].length;
}
catch (e)
{
tmp2 = 0;
}

tmp = Math.max(tmp1, tmp2);

/**
* 解决 319500-301418.09
* 38890.91+116672.73+38890.91
* 2.0-1.1
* 对于javascript计算,直接*10或者*100也会出现精度错误,因此采用字符串的方式解决
*/
result1 = arg1;
result2 = arg2;

var tmpStr = '';
if (tmp>0)
{

tmpStr = arg1.toString().replace('.','');
for (var i=0;i<tmp-tmp1;i++)
{
tmpStr += '0';
}
result1 = Number(tmpStr);

tmpStr = arg2.toString().replace('.','');
for (var i=0;i<tmp-tmp2;i++)
{
tmpStr += '0';
}
result2 = Number(tmpStr);
}

//return (arg1 * Math.pow(10, tmp) - arg2 * Math.pow(10, tmp)) / Math.pow(10, tmp);
return (result1 - result2) / Math.pow(10, tmp);
}

Number.prototype.sub = function(arg) {
return accSub(this, arg);
}